lavaan and purrr:map()

43 views
Skip to first unread message

Christopher Bratt

unread,
Dec 17, 2018, 10:41:06 AM12/17/18
to lavaan
Hi

I hope I'm not asking a question that has already been answered: I would like to rund a SEM model repeatedly for several groups. One approach to repeatedly run a model for several groups is to use the map() function from the purrr package. That would require lavaan to go well along with 'pipes' (%>%). Is it possible to use lavaan when coding with pipes (%>%)?

Christopher Bratt 

Jarrett Byrnes

unread,
Dec 17, 2018, 10:42:18 AM12/17/18
to lav...@googlegroups.com
It should be. If not, please send a reprex.

However, this sounds like multi group analysis. You can use the group = argument in your call to sem to make this happen. Not sure if broom::tidy works with this output, but it might?

-Jarrett
--
You received this message because you are subscribed to the Google Groups "lavaan" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lavaan+un...@googlegroups.com.
To post to this group, send email to lav...@googlegroups.com.
Visit this group at https://groups.google.com/group/lavaan.
For more options, visit https://groups.google.com/d/optout.

Christopher David Desjardins

unread,
Dec 17, 2018, 10:44:12 AM12/17/18
to lav...@googlegroups.com

Hi Christopher,
Do you mean like this?

library(tidyverse)
model <- ' 
  # latent variable definitions
     ind60 =~ x1 + x2 + x3
     dem60 =~ y1 + a*y2 + b*y3 + c*y4
     dem65 =~ y5 + a*y6 + b*y7 + c*y8

  # regressions
    dem60 ~ ind60
    dem65 ~ ind60 + dem60

  # residual correlations
    y1 ~~ y5
    y2 ~~ y4 + y6
    y3 ~~ y7
    y4 ~~ y8
    y6 ~~ y8
'

tidy_fit <- PoliticalDemocracy %>% 
  sem(model = model, data = .)

tidy_fit %>% 
  summary()

Christopher Bratt

unread,
Dec 17, 2018, 11:08:28 AM12/17/18
to lavaan
Thank you, Christopher. Looks good (I'm not very familiar with R, so I'm thankful for this kind of assistance.) I'll try your code. 

Jarrett: It could be done as a multigroup analysis, but I wasn't heading for that now. Thanks
Reply all
Reply to author
Forward
0 new messages