
R version 4.6.1 (2026-06-24) -- "Happy Hop"
Copyright (C) 2026 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> 
> options(digits = 3)
> 
> require("mboost")
Loading required package: mboost
Loading required package: parallel
Loading required package: stabs
> require("lattice")
Loading required package: lattice
> 
> set.seed(2102)
> 
> 
> ### tests for variable importance functions
> ### a simple two-dimensional example
> data("cars")
> 
> # gamboost with only one baselearner
> gam <- gamboost(dist ~ bols(speed), 
+   data = cars, control = boost_control(mstop = 50))
> 
> # risk reduction should completely be assigned to the only baselearner
> print( round((gam$family@risk(y = gam$response, f = gam$offset) - 
+   gam$risk()[length(gam$risk())]) / length(gam$response), 
+   digits = 4 ) == round(varimp(gam), digits = 4) )
bols(speed) 
       TRUE 
> # check plots
> plot(varimp(gam))
> plot(varimp(gam), percent = FALSE, type = "blearner")
> 
> # glmboost with only one variable / without intercept
> glm <- glmboost(dist ~ 0 + speed, data = cars, center = FALSE,
+   control = boost_control(mstop = 50))
> # check plot
> plot(varimp(glm))
> 
> # glmboost with only one variable / with intercept
> glm <- glmboost(dist ~ speed, data = cars, center = FALSE,
+   control = boost_control(mstop = 50))
> # check plot
> plot(varimp(glm), percent = FALSE)
> 
> 
> ### examples with multiple baselearners
> data("iris")
> iris$target <- factor(ifelse(iris$Species == "setosa", 1, 0))
> # separate intercept
> iris$int <- rep(1, nrow(iris)) 
> 
> # gamboost with multiple variables
> gam <- gamboost(target ~ bols(int, intercept = FALSE) +
+                         bols(Sepal.Width, intercept = FALSE) +
+                         bbs(Sepal.Width, center = TRUE) +
+                         bols(Sepal.Length, intercept = FALSE) +
+                         bbs(Sepal.Length, center = TRUE) + 
+                         bbs(Petal.Width, center = TRUE) + 
+                         bbs(Petal.Length, center = TRUE),
+   data = iris, control = boost_control(mstop = 100), 
+   family = Binomial(link = c("logit")))
Warning messages:
1: In bols(Sepal.Width, intercept = FALSE) :
  covariates should be (mean-) centered if 'intercept = FALSE'
2: In bols(Sepal.Length, intercept = FALSE) :
  covariates should be (mean-) centered if 'intercept = FALSE'
> # check distinct baselearners and variables
> print( length(varimp(gam)) == 7 ) # 7 baselearner
[1] TRUE
> print( length(unique(attr(varimp(gam), "variable_names"))) == 5 ) # 5 vars
[1] TRUE
> # check plotting
> plot(varimp(gam), type = "blearner")
> plot(varimp(gam), type = "variable")
> plot(varimp(gam), type = "variable", nbars = 3)
> plot(varimp(gam), type = "variable", nbars = 1)
> 
> # glmboost with multiple variables and intercept
> glm <- glmboost(target ~ Sepal.Width + Sepal.Length + Petal.Width +Petal.Length,
+   data = iris, control = boost_control(mstop = 100), 
+   family = Binomial(link = c("logit")))
> # check plotting
> plot(varimp(glm))
> plot(varimp(glm), type = "blearner", percent = FALSE)
> 
> 
> ### some more tests for plotting
> # show only limited number of bars, aggregate rest
> plot(varimp(glm), nbars = 4)
> plot(varimp(glm), nbars = 3)
> plot(varimp(glm), nbars = 2)
> plot(varimp(glm), nbars = 1) # other <-> 100%
> 
> # change number of characters in bar labels
> plot(varimp(gam), maxchar = 3)
Warning message:
In plot.varimp(varimp(gam), maxchar = 3) :
  'maxchar' set too small to distinguish variable labels. Different variables are probably summed up. Value for argument 'maxchar' should be incremented.
> plot(varimp(gam), maxchar = 100)
> 
> # change xlim
> plot(varimp(gam), xlim = c(0,5))
> plot(varimp(gam), xlim = c(0,0.2))
> 
> # change auto.key
> plot(varimp(gam), auto.key = FALSE)
> 
> # change order of baselearners
> plot(varimp(gam), blorder = "alphabetical")
> plot(varimp(gam), blorder = "rev_alphabetical")
> plot(varimp(gam), blorder = "formula")
> 
> # with type 'blearner'
> plot(varimp(gam), type = "blearner")
> plot(varimp(gam), type = "blearner", blorder = "alphabetical")
> plot(varimp(gam), type = "blearner", blorder = "rev_alphabetical")
> plot(varimp(gam), type = "blearner", blorder = "formula")
> 
> 
> ### again multiple baselearners (with simulated data)
> mydata = as.data.frame(matrix(nrow = 100, ncol = 100, data = rnorm(n = 10000)))
> mydata$target = factor(rep(c(1,0), each = 50))
> glm = glmboost(target ~ ., data = mydata, control = boost_control(mstop = 100), 
+   family = Binomial(link = c("logit")))
> # check plot
> # show ten bars only (default setting)
> plot(varimp(glm))
> # other larger than single variables, but still displayed at the bottom
> plot(varimp(glm), nbars = 5)
> 
> proc.time()
   user  system elapsed 
  1.072   0.071   1.135 
