ecpolley / SuperLearner

Current version of the SuperLearner R package
272 stars 72 forks source link

Possibility to use SuperLearner function without attaching the package #140

Closed JiayiJi closed 2 years ago

JiayiJi commented 2 years ago

I was trying to run the example code from the help page for the SuperLearner function and it seems that the SuperLearner function could only run after I've attached the package in the global environment using library(SuperLearner). I tried calling the SuperLearner function directly using SuperLearner::SuperLearner(). However, an error pops up. The following is my code to reproduce the error (The code is from the Examples in ?SuperLearner).

## simulate data
set.seed(23432)
## training set
n <- 500
p <- 50
X <- matrix(rnorm(n*p), nrow = n, ncol = p)
colnames(X) <- paste("X", 1:p, sep="")
X <- data.frame(X)
Y <- X[, 1] + sqrt(abs(X[, 2] * X[, 3])) + X[, 2] - X[, 3] + rnorm(n)

## test set
m <- 1000
newX <- matrix(rnorm(m*p), nrow = m, ncol = p)
colnames(newX) <- paste("X", 1:p, sep="")
newX <- data.frame(newX)
newY <- newX[, 1] + sqrt(abs(newX[, 2] * newX[, 3])) + newX[, 2] -
  newX[, 3] + rnorm(m)

# generate Library and run Super Learner
SL.library <- c("SL.glm", "SL.randomForest", "SL.gam",
  "SL.polymars", "SL.mean")
test <- SuperLearner::SuperLearner(Y = Y, X = X, newX = newX, SL.library = SL.library,
  verbose = TRUE, method = "method.NNLS")

The error message was

Error in get(library$screenAlgorithm[s], envir = env) : 
  object 'All' not found

The following is my R version.

platform       x86_64-apple-darwin17.0     
arch           x86_64                      
os             darwin17.0                  
system         x86_64, darwin17.0          
status                                     
major          4                           
minor          1.2                         
year           2021                        
month          11                          
day            01                          
svn rev        81115                       
language       R                           
version.string R version 4.1.2 (2021-11-01)
nickname       Bird Hippie            
ecpolley commented 2 years ago

With the current version, this is expected behavior. The SuperLearner function references a number of functions, so even if you are using your own candidate algorithm functions (e.g. the All() is an included variable screening function) and your own method function for the meta learner, the code still uses exported functions for the cross-validation creation.

ngreifer commented 2 years ago

For future reference, user can get around this supplying env = environment(SuperLearner::SuperLearner) in the call to SuperLearner(). Maybe this should be the default option, with the parent.frame() searched next if the supplied learner is not found there.