factor scores as.data.frame

577 views
Skip to first unread message

Anne Schulz

unread,
Jan 28, 2016, 7:40:18 AM1/28/16
to lavaan
Hello, 

I would like to save factor scores as a data frame to export them as txt. later. 
This has worked for a less complex cfa-model, not for MGCFA of higher order. 

This would be my code: 


#2nd order CFA
pa3dim22.1 <- 'anti =~ anti1 + anti2 + anti3 + anti5 
                souv =~ souv1 + souv2 + souv3 + souv4 
                hom =~ homli2 + homli1 + homli3 + homre1 
                pop =~ anti+souv+hom' 

#metric invariance
fit.pa3dim22.3 <- cfa(pa3dim22.1, data=d4c, group="country", group.equal=c("loadings"))            

predict(fit.pa3dim22.3)
fscores= as.data.frame(predict(fit.pa3dim22.3))
write.table(fscores, "Factor_Scores_CH.txt", quote=F, sep="\t", row.names=F)



The error message that I get would be this: 

Error in data.frame(c(-0.651082034559646, -0.979662281594498, -0.164683697288618,  : 
  Argumente implizieren unterschiedliche Anzahl Zeilen: 649, 640, 1260, 847
[translated: arguments imply different number of rows]


The following could be part of the problem: What I see is that a less complex model is an object of type "formal class lavan", the MGCFA is this typ: "large lavaan 1.1 MB". 

I would appreciate any help! 
Thank you! 
Anne


Terrence Jorgensen

unread,
Jan 29, 2016, 3:46:54 AM1/29/16
to lavaan
fit.pa3dim22.3 <- cfa(pa3dim22.1, data=d4c, group="country", group.equal=c("loadings"))            

predict(fit.pa3dim22.3)
fscores= as.data.frame(predict(fit.pa3dim22.3))

When you have a multiple-group model, predict() does not return a single matrix that can be coerced into a data.frame, but rather a list of matrices (one for each group).  So you can stack them into a single matrix using rbind(), then convert to a data.frame

fscores <- as.data.frame(do.call(rbind, predict(fit.pa3dim22.3)))

predict() doesn't return the group id's, so you can add them afterward if you need them.

fscores$group <- rep(fit.pa3dim22.3@Data@group.label, times = fit.pa3dim22.3@Data@nobs)

Terry

Anne Schulz

unread,
Jan 29, 2016, 5:00:13 AM1/29/16
to lavaan
Thank you Terrence, this was helpful. 
I found out myself meanwhile. Another option would be to do this: 

de = as.data.frame(predict(fit.pa3dim22.3)[[1]])

Do this for all groups and than merge them afterwards. 

Warm regards, 
Anne

Jessica Fritz

unread,
May 10, 2016, 12:40:31 PM5/10/16
to lavaan
Dear Terrence,

You stated in the message above that you can add the id's later on manually.

Could you explain to me how you are doing this. I have a data set with missing data in it and I have 1403 obervations but get factors scores for 1118 cases. How can I link them correctly back to the belonging id's? Are the factor scores ordered on the obersvations of the data set, in the way you give them to lavaan? I am ony abit afraid that I could link the factor scores to the wrong id's.

I was thinking about using the lavinspect function to retrieve the used id's?
      cases <- lavInspect(fit_Model, what = "case.idx", add.labels = TRUE, add.class = TRUE,
                    drop.list.single.group = TRUE)

Do you maybe know whether this works appropriately?



Thank you very much in advance for your help!

Best wishes,

Jes

Terrence Jorgensen

unread,
May 11, 2016, 4:19:29 AM5/11/16
to lavaan
You stated in the message above that you can add the id's later on manually.

The above message was about the grouping variable, not about individual case IDs.

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