prediction
# S3 method for class 'glmmTMB'
predict(
object,
newdata = NULL,
newparams = NULL,
se.fit = FALSE,
cov.fit = FALSE,
re.form = NULL,
allow.new.levels = NULL,
type = c("link", "response", "conditional", "zprob", "zlink", "disp", "latent"),
zitype = NULL,
na.action = na.pass,
fast = NULL,
debug = FALSE,
aggregate = NULL,
do.bias.correct = FALSE,
bias.correct.control = list(sd = TRUE),
...
)a glmmTMB object
new data for prediction
new parameters for prediction
return the standard errors of the predicted values?
return the covariance matrix of the predicted values?
NULL to specify individual-level predictions; ~0 or NA to specify population-level predictions (i.e., setting all random effects to zero)
allow previously unobserved levels in random-effects variables? see details.
Denoting \(mu\) as the mean of the conditional distribution and
p as the zero-inflation probability,
the possible choices are:
the linear predictor of the conditional model, or
equivalently the conditional mean on the scale of the link function
(this equivalence does not hold for truncated distributions, where
the link-scaled value is not adjusted for the effect of truncation on the mean; to get the corrected value of the conditional mean on the linear predictor
scale, use family(m)$linkfun(predict(m, type = "conditional")))
expected value; this is \(mu*(1-p)\) for zero-inflated models
and mu otherwise
mean of the conditional response; mu for all models
(i.e., synonymous with "response" in the absence of zero-inflation
the probability of a structural zero (returns 0 for non-zero-inflated models)
predicted zero-inflation probability on the scale of
the logit link function (returns -Inf for non-zero-inflated models)
dispersion parameter, however it is defined for that particular family (as described in sigma.glmmTMB)
return latent variables
deprecated: formerly used to specify type of zero-inflation probability. Now synonymous with type
how to handle missing values in newdata (see na.action);
the default (na.pass) is to predict NA
predict without expanding memory (default is TRUE if newdata and newparams are NULL and population-level prediction is not being done)
(logical) return the TMBStruc object that will be
used internally for debugging?
(optional factor vector) sum the elements with matching factor levels
(logical) should aggregated predictions use Taylor expanded estimate of nonlinear contribution of random effects (see details)
a list sent to TMB's function sdreport(). See documentation there.
unused - for method compatibility
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 is NULL (the default) and re.form is not NA, or if allow.new.levels is explicitly set to TRUE.
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.
Bias corrected predictions are based on the method described in Thorson J.T. & Kristensen (2016). These should be checked carefully by the user and are not extensively tested.
Thorson J.T. & Kristensen K. (2016) Implementing a generic method for bias correction in statistical models using random effects, with spatial and population dynamics examples. Fish. Res. 175, 66-74.
data(sleepstudy,package="lme4")
g0 <- glmmTMB(Reaction~Days+(Days|Subject),sleepstudy)
predict(g0, sleepstudy)
#> [1] 254.2209 273.7637 293.3064 312.8492 332.3920 351.9348 371.4775 391.0203
#> [9] 410.5631 430.1059 211.3566 213.1798 215.0031 216.8263 218.6496 220.4728
#> [17] 222.2961 224.1193 225.9425 227.7658 212.9720 217.9259 222.8798 227.8337
#> [25] 232.7876 237.7416 242.6955 247.6494 252.6033 257.5572 274.2369 280.0455
#> [33] 285.8541 291.6628 297.4714 303.2800 309.0886 314.8972 320.7059 326.5145
#> [41] 272.9546 280.4775 288.0003 295.5232 303.0460 310.5688 318.0917 325.6145
#> [49] 333.1373 340.6602 260.2204 270.4526 280.6847 290.9168 301.1489 311.3810
#> [57] 321.6131 331.8452 342.0773 352.3094 267.8468 278.1553 288.4638 298.7723
#> [65] 309.0808 319.3893 329.6978 340.0063 350.3148 360.6233 244.4083 255.9084
#> [73] 267.4084 278.9084 290.4084 301.9084 313.4085 324.9085 336.4085 347.9085
#> [81] 250.3672 250.2352 250.1031 249.9710 249.8390 249.7069 249.5748 249.4428
#> [89] 249.3107 249.1787 286.0712 305.1709 324.2705 343.3702 362.4699 381.5695
#> [97] 400.6692 419.7689 438.8685 457.9682 226.8471 238.3787 249.9104 261.4420
#> [105] 272.9737 284.5054 296.0370 307.5687 319.1004 330.6320 239.0706 256.0096
#> [113] 272.9485 289.8875 306.8264 323.7654 340.7043 357.6432 374.5822 391.5211
#> [121] 255.6789 263.1909 270.7029 278.2149 285.7268 293.2388 300.7508 308.2628
#> [129] 315.7748 323.2868 272.0271 286.0561 300.0851 314.1141 328.1431 342.1721
#> [137] 356.2012 370.2302 384.2592 398.2882 254.6635 266.0025 277.3415 288.6805
#> [145] 300.0195 311.3586 322.6976 334.0366 345.3756 356.7146 226.6950 241.8220
#> [153] 256.9489 272.0759 287.2029 302.3299 317.4568 332.5838 347.7108 362.8377
#> [161] 252.1282 261.6245 271.1207 280.6170 290.1132 299.6095 309.1057 318.6020
#> [169] 328.0983 337.5945 263.5238 275.3018 287.0798 298.8578 310.6358 322.4138
#> [177] 334.1918 345.9698 357.7479 369.5259
## Predict new Subject
nd <- sleepstudy[1,]
nd$Subject <- "new"
predict(g0, newdata=nd, allow.new.levels=TRUE)
#> [1] 251.4043
## population-level prediction
nd_pop <- data.frame(Days=unique(sleepstudy$Days),
Subject=NA)
predict(g0, newdata=nd_pop)
#> [1] 251.4043 261.8716 272.3388 282.8060 293.2733 303.7405 314.2077 324.6749
#> [9] 335.1422 345.6094
## return latent variables (BLUPs/conditional modes/etc. ) with standard errors
## (actually conditional standard deviations)
predict(g0, type = "latent", se.fit = TRUE)
#> $fit
#> b b b b b b
#> 2.8165246 9.0755528 -40.0477582 -8.6439822 -38.4323471 -5.5133140
#> b b b b b b
#> 22.8325393 -4.6586039 21.5502964 -2.9443878 8.8160954 -0.2351147
#> b b b b b b
#> 16.4424255 -0.1587189 -6.9960224 1.0327990 -1.0371210 -10.5992887
#> b b b b b b
#> 34.6668373 8.6324449 -24.5572911 1.0644384 -12.3337220 6.4717196
#> b b b b b b
#> 4.2745389 -2.9552360 20.6227274 3.5617882 3.2591311 0.8717889
#> b b b b b b
#> -24.7093589 4.6597469 0.7238462 -0.9709677 12.1194660 1.3107790
#>
#> $se.fit
#> b b b b b b b b
#> 13.653608 2.741354 13.828671 2.735007 13.733299 2.713093 13.948480 2.769399
#> b b b b b b b b
#> 13.600862 2.708624 12.911063 2.598204 13.095826 2.625148 12.915181 2.599763
#> b b b b b b b b
#> 14.019642 2.809209 13.637415 2.710907 13.506342 2.687946 13.736801 2.744890
#> b b b b b b b b
#> 12.994152 2.615336 13.091185 2.623266 12.835453 2.587091 14.058868 2.786453
#> b b b b
#> 12.841761 2.588429 12.925989 2.599464
#>