Skip to contents

A generic function (and corresponding methods) that produces predicted values or factor scores from modsem models.

Usage

modsem_predict(object, ...)

# S3 method for class 'modsem_pi'
modsem_predict(object, ...)

# S3 method for class 'modsem_da'
modsem_predict(
  object,
  newdata = NULL,
  method = c("EBM", "ML", "Bartlett", "Regression"),
  type = c("lv", "ov", "all"),
  standardized = FALSE,
  se = FALSE,
  drop.list.single.group = TRUE,
  ...
)

Arguments

object

An object of class modsem_pi or modsem_da, respectively.

...

Further arguments passed to lavaan::predict for modsem_pi objects; currently ignored by the modsem_da method.

newdata

Optional data.frame or matrix. If supplied, factor scores are computed for these observations instead of the data used during model estimation. Must contain all observed-variable columns required by the model; missing values are handled via FIML. Currently only supported by the modsem_da method.

method

Character. Scoring method. One of "EBM" (Empirical Bayes Modal; MAP estimate of the posterior), "ML" (maximum likelihood), "Regression" (alias for "EBM"), or "Bartlett" (alias for "ML"). For modsem_da objects this selects the optimisation-based scoring algorithm. For modsem_pi objects the argument is forwarded to lavaan::lavPredict, which accepts the same method values.

type

Character. Which scores to return: "lv" for latent variable scores only (default), "ov" for model-implied observed variable scores, or "all" for both combined. For modsem_da objects this is handled directly; for modsem_pi objects it is forwarded to lavaan::lavPredict, which supports the same type values.

standardized

Logical. If TRUE, each score column is standardised (mean 0, SD 1) before returning. Currently only used by the modsem_da method.

se

Logical. If TRUE, attach standard-error and asymptotic covariance attributes for modsem_da predictions. Standard errors are returned in the "se" attribute and covariance matrices in the "acov" attribute.

drop.list.single.group

Logical. If TRUE, covariance attributes for single-group modsem_da predictions drop the outer group list, matching lavaan::lavPredict.

Value

* For modsem_pi: whatever lavaan::predict() returns, which is usually a matrix of factor scores. * For modsem_da: a numeric matrix with one row per observation. Columns depend on type: latent variable scores ("lv"), model-implied observed-variable scores ("ov"), or both ("all"). Columns are optionally standardised if standardized = TRUE.

Methods (by class)

  • modsem_predict(modsem_pi): Wrapper for lavaan::predict

  • modsem_predict(modsem_da): Computes factor scores or model-implied observed-variable scores for a modsem_da model via MAP optimisation.

Examples

m1 <- '
# Outer Model
  X =~ x1 + x2 + x3
  Z =~ z1 + z2 + z3
  Y =~ y1 + y2 + y3

# Inner Model
  Y ~ X + Z + X:Z
'

est_dca <- modsem(m1, oneInt, method = "dblcent")
head(modsem_predict(est_dca))
#>               X           Z           Y         XZ
#> [1,]  0.7335391  0.06120326  0.05047257 -0.2208065
#> [2,] -0.6821195  2.18041524 -0.88082672 -1.8387785
#> [3,] -1.7943935  0.51493114 -1.93045740 -1.1061514
#> [4,]  1.3702391  2.15235456  5.01851357  2.8293527
#> [5,]  1.7397838 -0.87027404  0.80814709 -1.6768080
#> [6,] -1.7169401 -0.86698974 -0.34724128  1.3455015

# \dontrun{
est_lms <- modsem(m1, oneInt, method = "lms")
head(modsem_predict(est_lms))
#>               X           Z          Y
#> [1,]  0.7357389  0.04716883  0.1964398
#> [2,] -0.7203068  2.20235753 -0.7361697
#> [3,] -1.8030916  0.53553350 -1.7885657
#> [4,]  1.4377306  2.18265291  5.1743911
#> [5,]  1.6977740 -0.77326868  0.9575354
#> [6,] -1.7324633 -0.90187073 -0.2039848
# }