data("bodyfat", package = "mboost")
tbodyfat <- bodyfat
indep <- names(tbodyfat)[-2]
tbodyfat[indep] <- lapply(bodyfat[indep], function(x)
x <- x - mean(x)
)
### fit linear model to data
model <- glmboost(DEXfat ~ ., data = tbodyfat,
control = boost_control(mstop = 100))
### AIC-based selection of number of boosting iterations
AIC(model)
### 10-fold cross-validation
n <- nrow(tbodyfat)
k <- 10
ntest <- floor(n / k)
cv10f <- matrix(c(rep(c(rep(0, ntest), rep(1, n)), k - 1),
rep(0, n * k - (k - 1) * (n + ntest))), nrow = n)
cvm <- cvrisk(model, folds = cv10f)
print(cvm)
mstop(cvm)
plot(cvm)
### 25 bootstrap iterations
bs25 <- rmultinom(25, n, rep(1, n)/n)
cvm <- cvrisk(model, folds = bs25)
print(cvm)
mstop(cvm)
layout(matrix(1:2, ncol = 2))
plot(cvm)
### there seems to be some nonlinearity involved ...
blackbox <- blackboost(DEXfat ~ ., data = bodyfat)
cvtree <- cvrisk(blackbox, folds = bs25)
plot(cvtree)
Run the code above in your browser using DataCamp Workspace