Learn R Programming

INLAvaan (version 0.2.4)

predict: 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 INLAvaan
predict(
  object,
  type = c("lv", "yhat", "ov", "ypred", "ydist", "ymis", "ovmis"),
  newdata = NULL,
  level = 1L,
  nsamp = 1000,
  ymis_only = FALSE,
  ...
)

Value

A matrix of posterior draws with rows corresponding to samples and columns to variables or latent factors, or a list of such matrices when ymis_only = 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\).

"yhat", "ov"

Predicted means for observed variables \(E(y | \eta, \theta) = \nu + \Lambda \eta\); no residual noise.

"ypred", "ydist"

Predicted observed values including residual noise \(y = \nu + \Lambda \eta + \varepsilon\), \(\varepsilon \sim N(0, \Theta)\).

"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 multilevel models, specifies whether level 1 or level 2 latent variables are desired (default 1L).

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.

...

Currently unused.

See Also

sampling(), simulate(), summary()

Examples

Run this code
# \donttest{
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)
# }

Run the code above in your browser using DataLab