SEM with imputed data

55 views
Skip to first unread message

Susan Robinson

unread,
Jun 19, 2025, 4:32:28 PM6/19/25
to lavaan
I am trying to set up a SEM with imputed data using lavaan/lavaan.mi so that I can next do genetic sensitivty analysis using Gsens. I had been using runMI but then read that it's better to use lavaan.mi instead. 

In my data - MH is a binary variable (0,1) and I checked that it's coded as a factor. CF is a continuous variable, PGS is a continuous variable. 

Here is my code to set up the a, b, and c paths and assign the variables from the data:

#path a
cf_pgs_p1<-'
CF ~ PGS
CF ~~ CF
CF ~ 1'

#path b
mh_pgs_p1 <- 'MH ~ PGS'

#path c
MH_CF<-'
MH ~ CF
'

#define MH=depression
for (i in 1:100) {
  print(i)
  imputed_list[[i]] <- transform(imputed_list[[i]], MH = depression)}


#define CF= zert_sad
for (i in 1:100) {
  print(i)
  imputed_list[[i]] <- transform(imputed_list[[i]], CF = zert_sad)}

#define PGS = AlsMDD_std_score_S0
for (i in 1:100) {
  print(i)
  imputed_list[[i]] <- transform(imputed_list[[i]], PGS = AlsMDD_std_score_S0)}


Setting up the models:

#b path 
fit_b <- lavaan.mi(mh_pgs_p1,
               data=imputed_list,
               ordered = "MH",
               estimator = "WLSMV"
               )
r2_p1 <-summary(fit_b, rsquare=TRUE)[6,5]
mh_pgs_p1_r<-sqrt(r2_p1)



#c path 
fit_c <- lavaan.mi(MH_CF,
                   data=imputed_list,
                   ordered = "MH",
                   estimator = "WLSMV"
                   )
r2<-summary(fit_c, rsquare=TRUE)[6,5]
MH_CF_r<-sqrt(r2)



#a path
fit_a<-lavaan.mi(cf_pgs_p1,
                 data=imputed_list
                 )

#the dimensions for getting R-squared are wrong here.
r2_a<-summary(fit_a,rsquare=TRUE)[6,5]
cf_pgs_p1_r<-sqrt(r2_a) 


When I run each of the models with lavaan.mi() I get the below warning:

Warning message: In semTools::runMI(model = mh_pgs_p1, data = imputed_list, ordered = "MH", : The runMI() function and lavaan.mi-class have been deprecated and will cease to be included in future versions of semTools. Support is still provided for analyzing lavaan.mi-class objects (e.g., compRelSEM() can estimate reliability using pooled results), which can now be created using the lavaan.mi package. The deprecated runMI() function now creates an object of class OLDlavaan.mi, which can be analyzed using the deprecated functions in semTools, like lavTestLRT.mi(), that have been updated and improved in the lavaan.mi package. Find more details help('semTools-deprecated)

Also, when I run summary(fit_b) and summary (fit_c) I get the below error message:

Error in cov(do.call(rbind, coefList)[, PT$free > 0L & !duplicated(PT$free)]) : supply both 'x' and 'y' or a matrix-like 'x'

Summary(fit_a) works fine. 

My questions are:
1. why am I still getting the warning if I am using lavaan.mi()
2. What is causing the error message when I try and get summary stats? I don't get any warnings/error (other than the one above) when running the model itself. 

Terrence Jorgensen

unread,
Jun 24, 2025, 12:05:13 AM6/24/25
to lavaan

I am trying to set up a SEM with imputed data using lavaan/lavaan.mi so that I can next do genetic sensitivty analysis using Gsens.


In my data - MH is a binary variable (0,1) and I checked that it's coded as a factor. CF is a continuous variable, PGS is a continuous variable. 

Based the notes in your syntax, I'm guessing PGS is your genetic influence, CF is your environmental influence, and you want to predict whether someone is diagnosed with depression (MH)?

Here is my code to set up the a, b, and c paths and assign the variables from the data:

Why aren't you defining all paths in a single SEM?  Your estimates of b and c are not correct from single-predictor models, unless PGS and CF are perfectly uncorrelated (which would not be consistent with modeling the a path). 

1. why am I still getting the warning if I am using lavaan.mi()

Did you load both lavaan.mi and semTools?  If you load semTools after loading lavaan.mi, you should see a message warning you that this will happen.  You should load lavaan.mi second so it will "mask" the deprecated semTools functionality.

2. What is causing the error message when I try and get summary stats? I don't get any warnings/error (other than the one above) when running the model itself. 

If it still happens with the lavaan.mi package, please provide a reprex with enough data for me to track down the issue.  

Best,
Terrence Jorgensen

Reply all
Reply to author
Forward
0 new messages