Search code examples
gammgcv

Bam predict warning in mgcv: variable xxx is absent


When I use the bam function in mgcv and use the option discrete = T to obtain fast inference, and then try to predict on fitted data, I get the following warning:

2: In model.matrix.default(fm, data, contrasts.arg) :

variable 'XXX' is absent, its contrast will be ignored

However, I have fitted the model using this variable.

When using predict(model,type="terms") the variable is present and the effect of the variable differs for different observations.

Next I will try to compare gam with bam, but it will take more time to run the model.

Anyway, I would still like to know what the error means


Solution

  • The warning is spurious. From R 3.6.0 'model.matrix' started warning about 'invalid' contrasts. Unfortunately 'invalid' includes contrasts for variables that have been omitted from the model.matrix call, which in my view are simply redundant, not invalid. bam is just calling model.matrix internally without some of the variables in your model, but without stripping out the extra contrasts. I might write a work around at some point. Simon W