Adding CFA factor scores to original dataset in R

I’m working on a project using R and the lavaan package for Confirmatory Factor Analysis (CFA). I’ve got my CFA model set up and running fine:

jobMotivation <- 'factor1 =~ var1 + var2 + var3
                  factor2 =~ var4 + var5 + var6
                  factor3 =~ var7 + var8 + var9 + var10
                  factor4 =~ var11 + var12 + var13
                  factor5 =~ var14 + var15 + var16'

results <- cfa(jobMotivation, data = myData, scores = 'regression')

I can get the factor scores with predict(results), but I’m stuck on how to add these scores back to my original dataset as new columns. I tried using cbind() but got an error about the S4 class.

Is there a straightforward way to merge these factor scores with my original data frame? I’d really appreciate any help or tips on this. Thanks!

Hey there, Melody_Cheerful! :wave: That’s a cool project you’re working on with CFA. I’m kinda curious about what kind of job motivation you’re studying. Sounds intriguing!

Anyway, about your question - have you tried using the bind_cols() function from the dplyr package? It’s pretty nifty for situations like this. Something like:

library(dplyr)
myData <- bind_cols(myData, predict(results))

This should do the trick without any S4 class hiccups. Plus, it’s super clean and easy to read.

Oh, and just wondering - what made you choose those specific variables for each factor? I’d love to hear more about your thought process there. It might give some insights into how to best integrate the scores.

Let us know how it goes, yeah? And don’t hesitate to ask if you hit any more snags. We’re all learning here! :blush:

I’ve encountered this issue before, and there’s a simple solution. Instead of using cbind(), try the lavPredict() function from the lavaan package. It’s designed specifically for this purpose and works seamlessly with CFA models.

Here’s how you can do it:

factor_scores <- lavPredict(results)
myData <- data.frame(myData, factor_scores)

This will add the factor scores as new columns to your original dataset. The column names will be the same as your factor names (factor1, factor2, etc.).

If you want to rename these columns, you can do so afterwards:

names(myData)[names(myData) == 'factor1'] <- 'MotivationScore1'

Hope this helps! Let me know if you run into any issues.

hey melody! u can try the data.frame() function to combine ur original data with the factor scores. like this:

myData <- data.frame(myData, predict(results))

it should work without any weird errors. lemme know if it helps or if u need more info!