Skip to content

predict with newdata for bols() called with a matrix #22

Open
@sbrockhaus

Description

@sbrockhaus

This is about a bols(x)-base-learner when x is a matrix. I fitted the model without problems but then I was not able to use predict() with newdata. I asume that similar to buser it is not possible to predict with newdata in this case. If so, I think it would be helpful to comment on it in the help page and/or throw a more informative error.

MWE

library(mboost)
set.seed(290875)
n <- 100
x1 <- rnorm(n)
x1 <- x1 - mean(x1)
x2 <- rnorm(n) + 0.25 * x1
x2 <- x2 - mean(x2)
y <- 3 * x1 + x2 

# save x1 and x2 together in a matrix 
# x12 <- cbind(x1, x2)
x12 <- matrix(c(x1, x2), ncol=2)
colnames(x12)

###########################
## look at the base-learner 
bl1 <- bols(x12)
# gives back the colnames of mf, which are NULL for the matrix
bl1$get_names()  

if(FALSE){ 
  # set the colnames 
  colnames(x12) <- c("x1", "x2")
  bl1 <- bols(x12)
  bl1$get_names() 
  ### predict does not work either  
}

###########################
### set up a dataframe with x12 contained as matrix 
temp <- data.frame(y = y, x12 = I(x12))
mod2 <- mboost(y ~ bols(x12), data = temp)
coef(mod2)

## predict() with newdata, try some differnt versions of newdata 
## does not work as the dataset does not contain x1 and x2
predict(mod2, newdata=temp ) 

temp2 <- data.frame(x1 = x1, x2 = x2)
predict(mod2, newdata=temp2 ) 

temp3 <- data.frame(x1 = I(x12), x2 = I(x12))
predict(mod2, newdata = temp3 )

On the other hand it is easy to get the same model using bols(x1, x2) which can be predicted without problems.

## use bols(x1, x2)
mydata <- data.frame(y=y, x1=x1, x2=x2)
mod3 <- mboost(y ~ bols(x1, x2, intercept=FALSE), data = mydata)
coef(mod3)

## predic works with newdata
head( predict(mod3, newdata = mydata ) )

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions