Predict From modsem Models
Source: R/generics.R, R/generics_modsem_pi.R, R/predict_da.R
modsem_predict.RdA 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_piormodsem_da, respectively.- ...
Further arguments passed to
lavaan::predictformodsem_piobjects; currently ignored by themodsem_damethod.- newdata
Optional
data.frameor 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 themodsem_damethod.- 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"). Formodsem_daobjects this selects the optimisation-based scoring algorithm. Formodsem_piobjects the argument is forwarded tolavaan::lavPredict, which accepts the samemethodvalues.- 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. Formodsem_daobjects this is handled directly; formodsem_piobjects it is forwarded tolavaan::lavPredict, which supports the sametypevalues.- standardized
Logical. If
TRUE, each score column is standardised (mean 0, SD 1) before returning. Currently only used by themodsem_damethod.- se
Logical. If
TRUE, attach standard-error and asymptotic covariance attributes formodsem_dapredictions. 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-groupmodsem_dapredictions drop the outer group list, matchinglavaan::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 forlavaan::predictmodsem_predict(modsem_da): Computes factor scores or model-implied observed-variable scores for amodsem_damodel 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
# }