i seem have namespace or scope issue when calling cv.tree() inside function:
library(tree) library(islr) carseats$high = ifelse(sales <= 8, "no", "yes") mytreecv = function(formula, mydata) { set.seed(2) tree.carseats = tree(formula, mydata) cv.carseats = cv.tree(tree.carseats, fun=prune.misclass) } when run mytreecv() error:
> mytreecv(high ~ . - sales, carseats) error in is.data.frame(data) (from #5) : object 'mydata' not found the cv.tree() call model.frame(object) fails. same function code works when call each line r prompt.
not sure why happens can fixed creating tree argument model = t. using example above:
mytreecv = function(formula, mydata) { set.seed(2) tree.carseats = tree(formula, mydata, model = t) cv.carseats = cv.tree(tree.carseats, fun=prune.misclass) } answer found here: http://florence.acadiau.ca/collab/hugh_public/index.php?title=r:putting_cv.tree_inside_a_function&redirect=no
Comments
Post a Comment