
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)
> 
> library("mboost")
Loading required package: parallel
Loading required package: stabs
> 
> set.seed(290875)
> 
> df <- function(n = 100) {
+     x <- matrix(runif(n * 10), ncol = 10)
+     y <- x %*% c(1:3, rep(0, 7)) + rnorm(n)
+     data.frame(y = y, x)
+ }
> 
> mydf <- df()
> 
> w <- c(rep(0, 50), rep(1, 50))
> mod1 <- glmboost(y ~ ., data = mydf, weights = w, center = FALSE)
> cf1 <- coef(mod1)
> 
> ### hat matrix: fast for linear models
> H <- attr(hatvalues(mod1), "hatmatrix")
> print(max(abs(H %*% (mydf$y - weighted.mean(mydf$y, w)) - fitted(mod1) + weighted.mean(mydf$y, w))) <
+           sqrt(.Machine$double.eps))
[1] TRUE
> 
> mb <- function(x) bols(x, intercept = FALSE)
> mydf$int <- rep(1, nrow(mydf))
> ## used to be: mod2 <- gamboost(y ~ ., data = mydf, weights = w, baselearner = mb) ## but doesn't work as .mboost functions deprecated.
> mod2 <- gamboost(y ~ ., data = mydf, weights = w, baselearner = mb)
Warning messages:
1: In bols(x, intercept = FALSE) :
  covariates should be (mean-) centered if 'intercept = FALSE'
2: In bols(x, intercept = FALSE) :
  covariates should be (mean-) centered if 'intercept = FALSE'
3: In bols(x, intercept = FALSE) :
  covariates should be (mean-) centered if 'intercept = FALSE'
4: In bols(x, intercept = FALSE) :
  covariates should be (mean-) centered if 'intercept = FALSE'
5: In bols(x, intercept = FALSE) :
  covariates should be (mean-) centered if 'intercept = FALSE'
6: In bols(x, intercept = FALSE) :
  covariates should be (mean-) centered if 'intercept = FALSE'
7: In bols(x, intercept = FALSE) :
  covariates should be (mean-) centered if 'intercept = FALSE'
8: In bols(x, intercept = FALSE) :
  covariates should be (mean-) centered if 'intercept = FALSE'
9: In bols(x, intercept = FALSE) :
  covariates should be (mean-) centered if 'intercept = FALSE'
10: In bols(x, intercept = FALSE) :
  covariates should be (mean-) centered if 'intercept = FALSE'
> cf2 <- coef(mod2)
> print(all(round(unlist(cf2), 5) %in% round(cf1, 5)))
[1] TRUE
> 
> ### hat matrix: less faster but in C
> H <- attr(hatvalues(mod2), "hatmatrix")
> print(max(abs(H %*% (mydf$y - weighted.mean(mydf$y, w)) - fitted(mod2) + weighted.mean(mydf$y, w))) <
+           sqrt(.Machine$double.eps))
[1] TRUE
> 
> ### hat matrix: directly in R
> # <FIXME> this is non-sense </FIXME>
> #mod2$family <- Laplace()
> #H <- attr(hatvalues(mod2), "hatmatrix")
> #print(max(abs(H %*% (mydf$y - weighted.mean(mydf$y, w)) - fitted(mod2) + weighted.mean(mydf$y, w))) <
> #          sqrt(.Machine$double.eps))
> 
> proc.time()
   user  system elapsed 
  0.660   0.068   0.714 
