vglmer_predict {vglmer} | R Documentation |
Predict after vglmer
Description
These functions calculate the estimated linear predictor using
the variational distributions. predict.vglmer
draws predictions
using the estimated variational distributions; predict_MAVB
does so
using the MAVB procedure described in Goplerud (2022a).
Usage
## S3 method for class 'vglmer'
predict(
object,
newdata,
samples = 0,
samples_only = FALSE,
summary = TRUE,
allow_missing_levels = FALSE,
...
)
predict_MAVB(
object,
newdata,
samples = 0,
samples_only = FALSE,
var_px = Inf,
summary = TRUE,
allow_missing_levels = FALSE
)
Arguments
object |
Model fit using |
newdata |
Dataset to use for predictions. It cannot be missing. |
samples |
Number of samples to draw. Using |
samples_only |
Default ( |
summary |
Default ( |
allow_missing_levels |
Default ( |
... |
Not used; included to maintain compatibility with existing methods. |
var_px |
Variance of working prior for marginal augmentation. Default
( |
Value
This function returns an estimate of the linear predictor. The
default returns the expected mean, i.e. E_{q(\alpha,\beta)}[x_i^T
\beta + z_i^T\alpha]
. If samples > 0
, these functions return a
summary of the prediction for each observation, i.e. the estimated mean and
variance. If summary = FALSE
, the sampled values of the linear
predictor are returned as a matrix. predict_MAVB
performs MAVB as
described in Goplerud (2022a) before returning the linear predictor.
If allow_missing_levels = TRUE
, then observations with a new
(unseen) level for the random effect are given a value of zero for that
term of the prediction.
Examples
set.seed(123)
sim_data <- data.frame(
x = rnorm(100),
y = rbinom(100, 1, 0.5),
g = sample(letters, 100, replace = TRUE)
)
# Run with defaults
est_vglmer <- vglmer(y ~ x + (x | g), data = sim_data, family = "binomial")
# Simple prediction
predict(est_vglmer, newdata = sim_data)
# Return 10 posterior draws of the linear predictor for each observation.
predict_MAVB(est_vglmer, newdata = sim_data, summary = FALSE, samples = 10)
# Predict with a new level; note this would fail if
# allow_missing_levels = FALSE (the default)
predict(est_vglmer,
newdata = data.frame(g = "AB", x = 0),
allow_missing_levels = TRUE
)