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
Run the code above in your browser using DataLab