Use predict in an lme4 style on gam/bam objects from mgcv.

predict_gamm(
  model,
  newdata,
  re_form = NULL,
  se = FALSE,
  include = NULL,
  exclude = NULL,
  keep_prediction_data = FALSE,
  ...
)

Arguments

model

A gam class model from the mgcv package.

newdata

Data on which to predict on. Empty by default.

re_form

NULL, NA, or character string. If NULL (default), all random effects will be used. If NA, no random effects will be used. If character, must be of the form "s(varname)", where varname would be the name of the grouping variable pertaining to the random effect. Appended to include.

se

Logical. Include standard errors or not. Default is FALSE.

include

Which random effects to include in prediction. See predict.gam for details.

exclude

Which random effects to exclude in prediction. See predict.gam for details.

keep_prediction_data

Keep the model frame or newdata that was used in the prediction in final output? Default is FALSE.

...

Other arguments for predict.gam.

Value

A data frame of predictions and possibly standard errors.

Details

This is a wrapper for predict.gam. The goal is to have similar functionality with predict function in lme4, which makes it easy to drop all random effects or include specific ones. Some of this functionality is not yet available for class bam.

See also

Examples

library(lme4) library(mgcv) lmer_model <- lmer(Reaction ~ Days + (Days || Subject), data = sleepstudy) ga_model <- gam(Reaction ~ Days + s(Subject, bs = "re") + s(Days, Subject, bs = "re"), data = sleepstudy, method = "REML" ) head( data.frame( lmer = predict(lmer_model), gam = predict_gamm(ga_model) ) )
#> lmer prediction #> 1 252.9178 252.9178 #> 2 272.7086 272.7086 #> 3 292.4993 292.4994 #> 4 312.2901 312.2901 #> 5 332.0809 332.0809 #> 6 351.8717 351.8717
head( cbind( lmer = predict(lmer_model, re.form = NA), gam1 = predict_gamm(ga_model, re_form = NA), gam2 = predict_gamm(ga_model, exclude = c("s(Subject)", "s(Days,Subject)") ) ) )
#> lmer prediction prediction #> 1 251.4051 251.4051 251.4051 #> 2 261.8724 261.8724 261.8724 #> 3 272.3397 272.3397 272.3397 #> 4 282.8070 282.8070 282.8070 #> 5 293.2742 293.2742 293.2742 #> 6 303.7415 303.7415 303.7415
head(predict_gamm(ga_model, se = TRUE))
#> prediction se #> 1 252.9178 12.410220 #> 2 272.7086 10.660891 #> 3 292.4994 9.191224 #> 4 312.2901 8.153871 #> 5 332.0809 7.724998 #> 6 351.8717 8.003034