predict: Posterior Predictions for INLAvaan Models

predictR Documentation

Posterior Predictions for INLAvaan Models

Description

Compute posterior predictions from a fitted INLAvaan model, including latent variable scores, predicted observed values, and imputed missing data.

Usage

## S4 method for signature 'INLAvaan'
predict(
  object,
  type = c("lv", "yhat", "ov", "ypred", "ydist", "ymis", "ovmis"),
  newdata = NULL,
  level = 1L,
  nsamp = 1000,
  ymis_only = FALSE,
  summary = FALSE,
  ...
)

Arguments

object

An object of class INLAvaan.

type

Character string specifying the type of prediction:

"lv"

(default) Posterior draws of latent variable scores \eta | y, \theta. A composite (<~) score is the weighted sum of its indicators (centred without a mean structure).

"yhat", "ov"

Predicted means for observed variables E(y | \eta, \theta) = \nu + \Lambda \eta; no residual noise. An observed outcome is predicted from its regressors, and an indicator of a composite from the composite.

"ypred", "ydist"

Predicted observed values including residual noise y = \nu + \Lambda \eta + \varepsilon, \varepsilon \sim N(0, \Theta), with the residual variances that lavInspect(fit, "theta") reports (observed outcomes included, observed covariates excluded), except that the drawn indicators of a composite keep its weighted sum.

"ymis", "ovmis"

Imputed values for missing observations, drawn from the conditional distribution y_{mis} | y_{obs}, \theta.

newdata

An optional data frame of new observations. If supplied, predictions are computed for newdata rather than the original training data. Not supported for type = "ymis".

level

Integer; for type = "lv" in two-level models, specifies whether level 1 or level 2 latent variables are desired (default 1L). Other types ignore it: for two-level models, "yhat" and "ypred" give the total within plus between prediction, and a "ypred" draw keeps its cluster's between-level values, so only the observation-level residuals are new. For a random-slope model (see inlavaan()), each level 2 latent variable, slopes included, is drawn on its own, so draws of different variables are independent given the parameters, and level 1 values are conditional means. Its "yhat" and "ypred" use these empirical Bayes values (closed-form route only), and "ypred" adds the level-1 and between-level residuals.

nsamp

Integer; number of posterior samples to use for prediction. Defaults to 1000.

ymis_only

Logical; only applies when type = "ymis". When TRUE, returns only the imputed values as a named numeric vector per sample (names of the form "varname[rowindex]", matching the blavaan convention). When FALSE (default), returns the full data matrix with missing values filled in.

summary

Logical. When TRUE, collapse the posterior draws with summary() and return summary statistics (mean, SD, quantiles, mode) instead of the raw draws – equivalent to calling summary(predict(object, ...)) but without materialising the intermediate draws object. Default FALSE.

...

Currently unused.

Value

A list of nsamp posterior draws, each a matrix (or data frame, for multiple groups) with rows corresponding to cases and columns to variables or latent factors. When summary = TRUE, instead returns a summary.predict.inlavaan_internal object with the posterior mean, SD, quantiles, and mode for each case/variable.

See Also

sampling(), simulate(), summary()

Examples


HS.model <- "
  visual  =~ x1 + x2 + x3
  textual =~ x4 + x5 + x6
  speed   =~ x7 + x8 + x9
"
utils::data("HolzingerSwineford1939", package = "lavaan")
fit <- acfa(HS.model, HolzingerSwineford1939, std.lv = TRUE, nsamp = 100,
            test = "none", verbose = FALSE)

# Posterior latent variable scores
lv_scores <- predict(fit)
head(lv_scores)

# Predicted observed variable means
yhat <- predict(fit, type = "yhat")
head(yhat)

# Point estimates only, skipping the manual summary() step
predict(fit, type = "yhat", summary = TRUE)



INLAvaan documentation built on Nov. 13, 2026, 5:08 p.m.