SuperLearner() Example

library(SuperLearner)
set.seed(27)
n<-500

##Generate simulated data##

data <- data.frame(W1=runif(n, min = .5, max = 1),
W2=runif(n, min = 0, max = 1),
W3=runif(n, min = .25, max = .75),
W4=runif(n, min = 0, max = 1))
data <- transform(data, #add W5 dependent on W2, W3
W5=rbinom(n, 1, 1/(1+exp(1.5*W2-W3))))
data <- transform(data, #add Y
Y=rbinom(n, 1,1/(1+exp(-(-.2*W5-2*W1+4*W5*W1-1.5*W2+sin(W4))))))

summary(data)

##Specify a library of algorithms##

SL.library <- c("SL.nnet", "SL.glm", "SL.mean", "SL.randomForest")

##Run the super learner to obtain final predicted values for the super learner as well as CV risk for algorithms in the library##

fit.data.SL<-SuperLearner(Y=data[,6],X=data[,1:5],SL.library=SL.library, family=binomial(),method="method.NNLS", verbose=TRUE)

fit.data.SL #CV risks for algorithms in the library

##Run the cross-validated super learner to obtain its CV risk##

fitSL.data.CV <- CV.SuperLearner(Y=data[,6],X=data[,1:5], V=10, SL.library=SL.library, verbose = TRUE, method = "method.NNLS", family = binomial())

mean((data[,6]-fitSL.data.CV$SL.predict)^2) #CV risk for super learner