I developed a model from a dataset where zmean and zpcum5 are continuous values, and V6 is categorical (evergreen, mixed, deciduous).

Lm3c.ranslope <- lmer(plotVol_sqrt ~ zmean + zpcum5 + I(zmean * zpcum5) + (1 + plotVol_sqrt|V6),
                      data = lidarDataSubset_B)

My SpatRaster stdMetricMerge_wVI_mcDif_Class is comprised of 3 layers (zmean, zpcum5, and V6). Like above, only V6 has levels: evergreen, mixed, deciduous. The other two are continuous (FLT4S)). When I try to run predict, I get an error.

VOL_w2w_ran.slope <- terra::predict(stdMetricMerge_wVI_mcDif_Class,
                                    model = Lm3c.ranslope,
                                    fun = predict,
                                    const = NULL)
Error in model.matrix.default(eval(substitute(~foo, list(foo = x[[2]]))),  : 
  model frame and formula mismatch in model.matrix()

Can I use predict for a mixed effects model that has continuous and factor, or is this a syntax error I'm missing?

  • 1
    Make sure that your categorical parameter is represented by a raster that is a factor. Take a look at the terra::as.factor function. Jan 30, 2023 at 22:55
  • thanks @JeffreyEvans, I just checked using is.factor(). V6 is a factor in both lidarDataSubset_B and stdMetricMerge_wVI_mcDif_Class. (and of course zmean and zpcum5 are num).
    – Ray J
    Jan 31, 2023 at 11:31
  • @JeffreyEvans I just saw the error in my model... my apologies but still thank you for your time.
    – Ray J
    Jan 31, 2023 at 11:58

1 Answer 1


My model isn't set up correctly. This made it impossible for predict to work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.