How to attach predicted factor scores back to data frame?

27 views
Skip to first unread message

tuth...@bc.edu

unread,
Jul 27, 2018, 2:33:23 PM7/27/18
to lavaan
Hello,

I'm trying to merge/attach my predicted factor scores back to my data frame (which has missing data, so not every row will have a predicted factor scores). I've been using inspect(fit, "case.idx") to see which cases have factor scores in the factor score matrix that lavaan produces, but I don't really know where to take it from there. Any help would be greatly appreciated!!

Kaitlyn

Terrence Jorgensen

unread,
Jul 29, 2018, 7:18:57 PM7/29/18
to lavaan
I've been using inspect(fit, "case.idx") to see which cases have factor scores in the factor score matrix that lavaan produces, but I don't really know where to take it from there.

Those are the rows in the original data.frame to which the model was fit.  So you can attach as follows:

data("HolzingerSwineford1939")
HolzingerSwineford1939$x1[1] <- NA
example
(cfa)

factor
.scores <- predict(fit)
nrow
(HolzingerSwineford1939) # 301
nrow
(factor.scores) # 300

for (vv in colnames(factor.scores)) HolzingerSwineford1939[inspect(fit, "case.idx"), vv] <- factor.scores[ , vv]


Terrence D. Jorgensen
Postdoctoral Researcher, Methods and Statistics
Research Institute for Child Development and Education, the University of Amsterdam

Reply all
Reply to author
Forward
0 new messages