R: how to specify predictors in mboost model

Viewed 40

I have the following dataset with 3 columns of covariates, and 1 outcome column:

data <- structure(list(V1 = c(0.368203440103238, 0.324519532540959, -0.267369607029419, 
-0.551350850969297, 0.12599748535452), V2 = c(-0.685091020879978, 
0.0302665318913346, 0.38152909685676, -0.741473194305708, 1.01476858643759
), V3 = c(-1.11459785962843, -0.012932271762972, 2.02715929057818, 
0.118419126609398, -1.01804828579617), y = c(-1.95083653823476, 
-0.50091658480941, 3.74423248124182, -0.0459478421882341, -1.24653151600941
)), class = "data.frame", row.names = c("X1", "X2", "X3", "X4", 
"X5"))

> head(data)
           V1          V2          V3           y
X1  0.3682034 -0.68509102 -1.11459786 -1.95083654
X2  0.3245195  0.03026653 -0.01293227 -0.50091658
X3 -0.2673696  0.38152910  2.02715929  3.74423248
X4 -0.5513509 -0.74147319  0.11841913 -0.04594784
X5  0.1259975  1.01476859 -1.01804829 -1.24653152

I want to fit the following model:

library(mboost)
model <- mboost(y ~ bols(V1, intercept = FALSE) +
                         bols(V2, intercept = FALSE) + bols(V3, intercept = FALSE),
                         data = data)

However, it is very tedious to type out bols(covariate, intercept = FALSE) for every single column in the model. Is there a way to automate this for an arbitrary number of covariates? For example, I currently have 3 covariates named V1, V2, V3. But what if I had 10 that are named V1-V10? I would like to avoid having to type out 10 bols() statements.

1 Answers

We can create a formula expression with paste

fmla <- as.formula(paste0('y ~ ', paste0('bols(', setdiff(names(data), 
      'y'), ', intercept = FALSE)', collapse= " + ")))
model <- mboost(fmla, data = data)
model$call[[2]] <- fmla
model   

#    Model-based Boosting

#Call:
#mboost(formula = y ~ bols(V1, intercept = FALSE) + bols(V2, intercept = FALSE) +     bols(V3, intercept = FALSE), data = data)


#    Squared Error (Regression) 

#Loss function: (y - f)^2 
 

#Number of boosting iterations: mstop = 100 
#Step size:  0.1 
#Offset:  1.157408e-15 
#Number of baselearners:  3 
Related