prediction

# S3 method for glmmTMB
predict(
  object,
  newdata = NULL,
  newparams = NULL,
  se.fit = FALSE,
  cov.fit = FALSE,
  re.form = NULL,
  allow.new.levels = FALSE,
  type = c("link", "response", "conditional", "zprob", "zlink", "disp"),
  zitype = NULL,
  na.action = na.pass,
  fast = NULL,
  debug = FALSE,
  ...
)

Arguments

object

a glmmTMB object

newdata

new data for prediction

newparams

new parameters for prediction

se.fit

return the standard errors of the predicted values?

cov.fit

return the covariance matrix of the predicted values?

re.form

NULL to specify individual-level predictions; ~0 or NA to specify population-level predictions (i.e., setting all random effects to zero)

allow.new.levels

allow previously unobserved levels in random-effects variables? see details.

type

Denoting \(mu\) as the mean of the conditional distribution and p as the zero-inflation probability, the possible choices are:

"link"

conditional mean on the scale of the link function, or equivalently the linear predictor of the conditional model

"response"

expected value; this is \(mu*(1-p)\) for zero-inflated models and mu otherwise

"conditional"

mean of the conditional response; mu for all models (i.e., synonymous with "response" in the absence of zero-inflation

"zprob"

the probability of a structural zero (returns 0 for non-zero-inflated models)

"zlink"

predicted zero-inflation probability on the scale of the logit link function (returns -Inf for non-zero-inflated models)

"disp"

dispersion parameter however it is defined for that particular family as described in sigma.glmmTMB

zitype

deprecated: formerly used to specify type of zero-inflation probability. Now synonymous with type

na.action

how to handle missing values in newdata (see na.action); the default (na.pass) is to predict NA

fast

predict without expanding memory (default is TRUE if newdata and newparams are NULL and population-level prediction is not being done)

debug

(logical) return the TMBStruc object that will be used internally for debugging?

...

unused - for method compatibility

Details

  • To compute population-level predictions for a given grouping variable (i.e., setting all random effects for that grouping variable to zero), set the grouping variable values to NA. Finer-scale control of conditioning (e.g. allowing variation among groups in intercepts but not slopes when predicting from a random-slopes model) is not currently possible.

  • Prediction of new random effect levels is possible as long as the model specification (fixed effects and parameters) is kept constant. However, to ensure intentional usage, a warning is triggered if allow.new.levels=FALSE (the default).

  • Prediction using "data-dependent bases" (variables whose scaling or transformation depends on the original data, e.g. poly, ns, or poly) should work properly; however, users are advised to check results extra-carefully when using such variables. Models with different versions of the same data-dependent basis type in different components (e.g. formula= y ~ poly(x,3), dispformula= ~poly(x,2)) will probably not produce correct predictions.

Examples

data(sleepstudy,package="lme4")
g0 <- glmmTMB(Reaction~Days+(Days|Subject),sleepstudy)
predict(g0, sleepstudy)
#>   [1] 254.2209 273.7637 293.3065 312.8493 332.3921 351.9349 371.4777 391.0205
#>   [9] 410.5633 430.1061 211.3566 213.1798 215.0030 216.8263 218.6495 220.4727
#>  [17] 222.2959 224.1191 225.9424 227.7656 212.9720 217.9259 222.8798 227.8337
#>  [25] 232.7876 237.7415 242.6954 247.6493 252.6032 257.5571 274.2373 280.0459
#>  [33] 285.8544 291.6630 297.4715 303.2801 309.0886 314.8972 320.7057 326.5143
#>  [41] 272.9551 280.4778 288.0006 295.5234 303.0462 310.5689 318.0917 325.6145
#>  [49] 333.1372 340.6600 260.2207 270.4528 280.6848 290.9169 301.1490 311.3811
#>  [57] 321.6132 331.8452 342.0773 352.3094 267.8471 278.1556 288.4640 298.7725
#>  [65] 309.0809 319.3894 329.6979 340.0063 350.3148 360.6233 244.4084 255.9084
#>  [73] 267.4085 278.9085 290.4085 301.9085 313.4085 324.9085 336.4085 347.9086
#>  [81] 250.3676 250.2355 250.1033 249.9712 249.8390 249.7069 249.5747 249.4426
#>  [89] 249.3105 249.1783 286.0715 305.1711 324.2708 343.3704 362.4701 381.5697
#>  [97] 400.6694 419.7691 438.8687 457.9684 226.8470 238.3787 249.9104 261.4420
#> [105] 272.9737 284.5054 296.0371 307.5687 319.1004 330.6321 239.0706 256.0095
#> [113] 272.9485 289.8875 306.8265 323.7654 340.7044 357.6434 374.5823 391.5213
#> [121] 255.6792 263.1911 270.7030 278.2150 285.7269 293.2389 300.7508 308.2628
#> [129] 315.7747 323.2867 272.0274 286.0563 300.0853 314.1143 328.1433 342.1723
#> [137] 356.2013 370.2303 384.2592 398.2882 254.6637 266.0027 277.3416 288.6806
#> [145] 300.0196 311.3586 322.6976 334.0366 345.3756 356.7146 226.6949 241.8219
#> [153] 256.9489 272.0759 287.2029 302.3299 317.4569 332.5839 347.7109 362.8379
#> [161] 252.1284 261.6246 271.1209 280.6171 290.1133 299.6095 309.1058 318.6020
#> [169] 328.0982 337.5945 263.5241 275.3020 287.0800 298.8580 310.6360 322.4139
#> [177] 334.1919 345.9699 357.7479 369.5258
## Predict new Subject
nd <- sleepstudy[1,]
nd$Subject <- "new"
predict(g0, newdata=nd, allow.new.levels=TRUE)
#> [1] 251.4051
## population-level prediction
nd_pop <- data.frame(Days=unique(sleepstudy$Days),
                     Subject=NA)
predict(g0, newdata=nd_pop)
#>  [1] 251.4051 261.8724 272.3397 282.8070 293.2742 303.7415 314.2088 324.6760
#>  [9] 335.1433 345.6106