| lavPredict | R Documentation |
The main purpose of the lavPredict() function is to compute (or
‘predict’) individual scores for the latent variables in the model
(‘factor scores’). NOTE: the goal of this
function is NOT to predict future values of dependent variables as in the
regression framework! (For models with only continuous observed variables, the function lavPredictY() supports this.)
lavPredict(object, newdata = NULL, type = "lv", method = "EBM",
transform = FALSE, se = "none", acov = "none",
label = TRUE, fsm = FALSE, mdist = FALSE, rel = FALSE,
append_data = FALSE, assemble = FALSE,
level = 1L, optim_method = "bfgs", eta = NULL,
parallel = c("auto", "no", "multicore", "snow"),
ncpus = NULL, cl = NULL,
drop_list_single_group = TRUE,
mdist_draws = 2000L,
...)
object |
An object of class |
newdata |
An optional data.frame, containing the same variables as
the data.frame used when fitting the model in object. For multilevel
models, the cluster variable(s) must be included; the cluster structure
and (for |
type |
A character string. If |
method |
A character string. In the linear case (when the indicators are
continuous), the possible options are |
transform |
Logical. If |
se |
Character. If |
acov |
Similar to the |
label |
Logical. If TRUE, the columns in the output are labeled. |
fsm |
Logical. If TRUE, return the factor score matrix as an attribute. Only for numeric data. |
mdist |
Logical. If TRUE, the (squared)
Mahalanobis distances of the factor scores (if |
rel |
Logical. Only used if |
append_data |
Logical. Only used when |
assemble |
Logical. If TRUE, the separate groups are reassembled into a single data.frame with a group column, having the same dimensions as the original (or newdata) dataset. |
level |
Integer. Only used in a multilevel SEM.
If |
optim_method |
Character string. Only used in the categorical case.
If |
eta |
An optional matrix or list, containing latent variable values
for each observation. Used for computations when |
parallel |
Character. Only used in the categorical case, where factor
scores are computed by a per-observation numerical optimization. The options
are |
ncpus |
Integer. The number of processes to use in parallel computation. By default, this is 2 (following CRAN policy). You may set this to a higher value (for example, the number of available cores, minus two) if you wish to use more processes. |
cl |
An optional parallel or snow cluster for use when
|
drop_list_single_group |
Logical. If |
mdist_draws |
Integer. Only used if |
... |
To support old argument names. |
The predict() function calls the lavPredict() function
with its default options.
If there are no latent variables in the model, type = "ov" will
simply return the values of the observed variables. Note that this function
can not be used to ‘predict’ values of dependent variables, given the
values of independent variables (in the regression sense). In other words,
the structural component is completely ignored (for now).
For an overview (and evaluation) of the various factor score methods, see:
Grice, J. W. (2001). Computing and evaluating factor scores. Psychological Methods, 6(4), 430-450. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.1037/1082-989X.6.4.430")}
For the (continuous) regression and Bartlett methods, see:
Bartlett, M. S. (1937). The statistical conception of mental factors. British Journal of Psychology, 28, 97-104. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.1111/j.2044-8295.1937.tb00863.x")}
Bentler, P. M., & Yuan, K.-H. (1997). Optimal conditionally unbiased equivariant factor score estimators. In M. Berkane (Ed.), Latent variable modeling and applications to causality (pp. 259-281). New York: Springer-Verlag. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.1007/978-1-4612-1842-5_14")}
For the (categorical) Empirical Bayes Modal (EBM) and Maximum Likelihood (ML) methods, see:
Skrondal, A., & Rabe-Hesketh, S. (2004). Generalized latent variable modeling: Multilevel, longitudinal, and structural equation models. Boca Raton, FL: Chapman & Hall/CRC.
For the Mahalanobis distances with ordered categorical data
(mdist = TRUE), see:
Mansolf, M., & Reise, S. P. (2017). Case diagnostics for factor analysis of ordered categorical data with applications to person-fit measurement. Structural Equation Modeling: A Multidisciplinary Journal, 25(1), 86-100. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.1080/10705511.2017.1367926")}
For the correlation-preserving factor scores (transform = TRUE), see:
ten Berge, J. M. F., Krijnen, W. P., Wansbeek, T., & Shapiro, A. (1999). Some new results on correlation-preserving factor scores prediction methods. Linear Algebra and its Applications, 289(1-3), 311-318. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.1016/S0024-3795(97)10007-6")}
lavPredictY to predict y-variables given x-variables.
data(HolzingerSwineford1939)
## fit model
HS.model <- ' visual =~ x1 + x2 + x3
textual =~ x4 + x5 + x6
speed =~ x7 + x8 + x9 '
fit <- cfa(HS.model, data = HolzingerSwineford1939)
head(lavPredict(fit))
head(lavPredict(fit, type = "ov"))
## ---------------------------------------------
## standard errors for the factor scores (se =)
## ---------------------------------------------
## the standard errors are returned as the "se" attribute (a list, one
## (nobs x nfactor) matrix per group)
## for continuous indicators, the (naive) standard errors are the same
## for every observation
fscores <- lavPredict(fit, se = "standard")
attr(fscores, "se")[[1]]
## for categorical indicators, the factor scores are obtained by numerical
## optimization, and their standard errors differ from one response pattern
## (observation) to the next
## (a two-factor model is used here, as the numerical optimization and the
## Monte Carlo integration below both get expensive as the number of
## ordered indicators grows)
HS.model.ord <- ' visual =~ x1 + x2 + x3
textual =~ x4 + x5 + x6 '
HS.ord <- HolzingerSwineford1939
HS.ord[ , paste0("x", 1:6)] <-
lapply(HS.ord[ , paste0("x", 1:6)], function(x) ordered(cut(x, 3)))
fit.ord <- cfa(HS.model.ord, data = HS.ord, ordered = paste0("x", 1:6))
fscores <- lavPredict(fit.ord, se = "standard")
head(attr(fscores, "se")[[1]])
## -------------------------------------------------
## casewise Mahalanobis distances (case diagnostics)
## -------------------------------------------------
## continuous data: the squared distances are exact
fscores <- lavPredict(fit, mdist = TRUE)
head(attr(fscores, "mdist")[[1]])
## ordered categorical data: expected (squared) distances of the latent
## responses, obtained by Monte Carlo integration (Mansolf & Reise, 2017);
## set the seed for reproducibility
set.seed(123)
resid.ord <- lavPredict(fit.ord, type = "resid", mdist = TRUE)
## expected casewise residuals of the latent responses:
head(resid.ord)
## expected squared residual-based distances (person fit):
head(attr(resid.ord, "mdist")[[1]])
## ------------------------------------------
## merge factor scores to original data.frame
## ------------------------------------------
idx <- lavInspect(fit, "case.idx")
fscores <- lavPredict(fit)
## loop over factors
for (fs in colnames(fscores)) {
HolzingerSwineford1939[idx, fs] <- fscores[ , fs]
}
head(HolzingerSwineford1939)
## multigroup models return a list of factor scores (one per group)
data(HolzingerSwineford1939)
mgfit <- update(fit, group = "school", group_equal = c("loadings","intercepts"))
idx <- lavInspect(mgfit, "case.idx") # list: 1 vector per group
fscores <- lavPredict(mgfit) # list: 1 matrix per group
## loop over groups and factors
for (g in seq_along(fscores)) {
for (fs in colnames(fscores[[g]])) {
HolzingerSwineford1939[ idx[[g]], fs] <- fscores[[g]][ , fs]
}
}
head(HolzingerSwineford1939)
## -------------------------------------
## Use factor scores in subsequent models
## -------------------------------------
## see Examples in semTools package: ?plausibleValues
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.