# NOT RUN {
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"))
## ------------------------------------------
## 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 susequent models
## -------------------------------------
## see Examples in semTools package: ?plausibleValues
# }
Run the code above in your browser using DataLab