Is.data.frame (data) object ... not found in function context

There is a strange problem using the package function tree cv.treefrom a 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 results in an 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 call the same code directly from the script, it works 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?

+4
source share
2 answers

The output of this particular package requires the dataset to be a global variable! How about this as an argument for using general-purpose programming languages? In any case, the code works:

library(tree)

train_global <- NA
func <- function(train) {
  train_global <<- train
  t <- tree(formula=log10(perf) ~ syct+mmin+mmax+cach+chmin+chmax, data=train_global, split = "gini")    
  cv.tree(t, ,FUN=prune.tree, K = 4)    
  return (t)    
}    
data(cpus, package="MASS")    
cpus <- cpus    
result <- func(cpus)
plot(result)
0
source

cv.tree(). update: cv.tree model.frame, eval, train .

.... model.frame data "" "" "cpus", eval .

: , . .
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)
}

, "" , cv.tree, , . :-); .

+1

All Articles