vglmer_predict: Predict after vglmer

vglmer_predictR 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 (2022).

Usage

## S3 method for class 'vglmer'
predict(
  object,
  newdata,
  type = "link",
  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 vglmer.

newdata

Dataset to use for predictions. It cannot be missing.

type

Default ("link") returns the linear predictor; "terms" returns the predicted value for each random effect (or spline) separately as well as one that collects all fixed effects. At the moment, other options are not enabled.

samples

Number of samples to draw. Using 0 (default) gives the expectation of the linear predictor. A positive integer draws samples samples from the variational distributions and calculates the linear predictor.

samples_only

Default (FALSE) returns the samples from the variational distributions, not the prediction. Each row is a sample and each column is a parameter.

summary

Default (TRUE) returns the mean and variance of the samples for each observation. FALSE returns a matrix of the sampled linear predictor for each observation. Each row is a sample and each column is an observation.

allow_missing_levels

Default (FALSE) does not allow prediction for levels not observed in the original data. TRUE allows for prediction on unseen levels; the value of 0 (with no uncertainty) is used for the corresponding random effect.

...

Not used; included to maintain compatibility with existing methods.

var_px

Variance of working prior for marginal augmentation. Default (Inf) is a flat, improper, prior.

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 (2022) 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
)

vglmer documentation built on April 3, 2025, 6:07 p.m.