Search code examples
rmachine-learningpredictionboosting

R: how to make predictions using gamboost


library(mboost)
### a simple two-dimensional example: cars data
cars.gb <- gamboost(dist ~ speed, data = cars, dfbase = 4,
                    control = boost_control(mstop = 50))
set.seed(1)
cars_new <- cars + rnorm(nrow(cars))
> predict(cars.gb, newdata = cars_new$speed)
Error in check_newdata(newdata, blg, mf) : 
  ‘newdata’ must contain all predictor variables, which were used to specify the model.

I fit a model using the example on the help(gamboost) page. I want to use this model to predict on a new dataset, cars_new, but encountered the above error. How can I fix this?


Solution

  • predict function looks for a variable called speed but when you subset it with $ sign it has no name anymore.

    so, this variant of prediction works;

    predict(cars.gb, newdata = data.frame(speed = cars_new$speed))
    

    or keep the original name as is;

    predict(cars.gb, newdata = cars_new['speed'])