Have a strange problem using package tree
cv.tree
function from user defined function in R:
func <- function(train) {
classification.tree <- tree(log10(perf) ~ syct+mmin+mmax+cach+chmin+chmax, train, split = "gini")
cv.tree(classification.tree, ,FUN=prune.tree, K = 4)
return (classification.tree)
}
data(cpus, package="MASS")
result <- func(cpus)
plot(result)
This produces the error:
Error in is.data.frame(data) : object 'train' not found
16 is.data.frame(data)
15 model.frame.default(formula = log10(perf) ~ syct + mmin + mmax +
cach + chmin + chmax, data = train, subset = c("1", "2",
"3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14",
"15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", ...
14 eval(expr, envir, enclos)
13 eval(expr, p)
12 eval.parent(m)
11 tree(formula = log10(perf) ~ syct + mmin + mmax + cach + chmin +
chmax, data = train, split = "gini", subset = c("1", "2",
"3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14",
"15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", ...
10 eval(expr, envir, enclos)
9 eval(oc)
8 model.frame.tree(object)
7 model.frame(object)
6 cv.tree(classification.tree, , FUN = prune.tree, K = 4) at .active-rstudio-document#4
5 func(cpus) at .active-rstudio-document#9
4 eval(expr, envir, enclos)
3 eval(ei, envir)
2 withVisible(eval(ei, envir))
1 source("~/.active-rstudio-document", echo = TRUE)
At the same time if I invoke same code right from the script it works perfectly fine:
data(cpus, package="MASS")
classification.tree <- tree(log10(perf) ~ syct+mmin+mmax+cach+chmin+chmax, cpus, split = "gini")
cv.tree(classification.tree, ,FUN=prune.tree, K = 4)
plot(classification.tree)
What am I missing?
The crash is in the cv.tree()
call.
update: cv.tree
calls model.frame
, and inside that function there's an eval
, but the variable train
does not exist in that function's environment.
I'll keep digging.... If I go deep into debug mode for model.frame
and change the data
list element of the 'object' from 'train' to 'cpus', then eval
finds the object and executes.
Annnnd: I'm back where I started. It is an environment and lazy evaluation problem.
The fix is to use force
:
func <- function(train) {
force(train)
classification.tree <- tree(log10(perf) ~ syct+mmin+mmax+cach+chmin+chmax, train, split = "gini")
cv.tree(classification.tree, FUN=prune.tree, K = 4)
return (classification.tree)
}
This makes "train" exist in the environments available to cv.tree
and the functions it calls. Environments can get weird :-) ; this is one example thereof.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With