Saving Factor Scores Using LavPredict w/ 2 Groups

56 views
Skip to first unread message

Allie Choate

unread,
Aug 17, 2018, 6:12:24 PM8/17/18
to lavaan
Hi everyone,

I am trying to save factor scores using the LavPredict function from a multiple-group CFA.
I am able to save the factor scores successfully, but because I have a differing number of males and females in my groups, I am trying to thus save just the male factor scores into its own subset and then same with the female scores. I'm assuming this should be a easy fix, just haven't been able to find much on the topic. 

My code is below. Any help would be very much appreciated! 
Thanks in advance!


###
model1<- cfa(bifactormodel, data = main.data, group="Gender", 
                 std.lv=TRUE, orthogonal = TRUE, 
                 estimator = "MLR", missing="fiml")

summary(model1, fit.measures = TRUE, standardized = TRUE)
fscores <- lavPredict(model1,type="lv",method="regression")

Terrence Jorgensen

unread,
Aug 18, 2018, 10:47:09 AM8/18/18
to lavaan
I am trying to thus save just the male factor scores into its own subset and then same with the female scores.

lavPredict() already returns a list (one matrix per group) of factor scores for multigroup models.  Using the ?cfa help-page example:

example(cfa)
model1
<- update(fit, group = "sex")
fscores
<- lavPredict(model1, type="lv", method="regression")
str
(fscores)
lapply
(fscores, head)
sapply
(fscores, nrow) # group sample sizes


If you are wondering how to merge them back into your data, you can use the case indices:

(ID.list <- lavInspect(model1, "case.idx"))
IDs <- do.call(c, ID.list)
fitted
.data <- main.data[IDs, ]
allFscores
<- do.call(rbind, fscores)
fscores
.data <- cbind(fitted.data, allFscores)

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

Allie Choate

unread,
Aug 19, 2018, 1:51:21 PM8/19/18
to lavaan
I figured it was an easy fix that I was just missing!
Thank you so much.
Reply all
Reply to author
Forward
0 new messages