Moderated mediation using lavaan, DWLS

254 views
Skip to first unread message

Yuanyuan

unread,
Jan 12, 2022, 2:54:53 AM1/12/22
to lavaan
Hi,

I am new to SEM and lavaan. I tried to test a moderated mediation model using lavaan, 
The outcome variable is binary (coded as 0 and 1)
A is mediator
B is moderator, which moderates the association between IV and mediator

I used the following code to run the analysis:

mod.med <- '

#measurement model

A =~ A1 + A1 + A3

B =~ B1 + B2 + B3 + B4 + B5 + B6  B7

B1 ~~ B2

B1 ~~ B3

B2 ~~ B3

 

#Regression

A~ a1*IV + a2*B + a3*IV:B + Covariates

Outcome~ c1*IV+ b*A + Covariates

'

my.fit <- sem(mod.med, data=df, missing = 'listwise', bootstrap = 10, se = "bootstrap", ordered ="Outcome", estimator = "DWLS")

summary(my.fit)



However, it has some warning message:

lavaan WARNING:
    The variance-covariance matrix of the estimated parameters (vcov)
    does not appear to be positive definite! The smallest eigenvalue
    (= -2.901927e-17) is smaller than zero. This may be a symptom that
    the model is not identified.

(1) I am not sure how should I interpret this message and what should I do to fix this problem?

(2) Also, when I look at the results, I found that it cannot deal interaction term as shown in the screen shot that the third line doesn't have Std.Err , z-value  or P(>|z|). I don't know why this happen? I tried if I use ML rather than DWLS, there won't have this problem. But my outcome variable is binary, so DWLS should be a proper method to use, then how should I solve this problem?

Screenshot 2022-01-12 at 15.51.25.png


Many thanks!!

Terrence Jorgensen

unread,
Jan 25, 2022, 5:35:05 PM1/25/22
to lavaan
  1. This can occur when some parameters' estimated sampling distributions are highly collinear.  If there are no other signs that your model is not identified, then you can ignore this warning.
  2. B is a latent variable, so you cannot use the colon operator to define a product.
Terrence D. Jorgensen
Assistant Professor, Methods and Statistics
Research Institute for Child Development and Education, the University of Amsterdam

Yuanyuan

unread,
Jan 27, 2022, 9:20:43 PM1/27/22
to lavaan
Thank you so much for your reply!
I still have one more question.
I computed the B variable first before I use SEM, then I run the following code

mod.med <- '

#Regression

A~ a1*IV + a2*B + a3*IV:B 

Outcome~ c1*IV+ b*A 

'

my.fit <- sem(mod.med, data=df, missing = 'listwise', bootstrap = 10, se = "bootstrap", ordered ="Outcome", estimator = "DWLS")

summary(my.fit)

It shows an error message 

ERROR: some variables have no values (only missings) or no variance

and a table showing that there is some problem with my interaction term, but I don't understand why my interaction term is NULL. 

Screenshot 2022-01-28 at 10.15.18.png

however, when I removed "ordered ="Outcome", it can produce some results. I would like to ask what caused the error? And since my outcome variable is a binary variable, 

shouldn't I use "ordered= ..." this argument? (https://lavaan.ugent.be/tutorial/cat.html


Mant thanks!


Shu Fai Cheung

unread,
Jan 27, 2022, 11:37:12 PM1/27/22
to lavaan
Not sure if it is a bug of the colon operator or if it is a known limitation. It should work if you compute the product term yourself. As far as I know, the colon operator just forms the product terms internally. What it does should be equivalent to forming the product term ourselves.

I can reproduce the error using a toy dataset:

library(lavaan) #> This is lavaan 0.6-9 #> lavaan is FREE software! Please report any bugs. set.seed(4314) n <- 100 B <- rnorm(n) IV <- rnorm(n) A <- rnorm(n) Outcome <- sample(c("a", "b"), n, replace = TRUE) df <- data.frame(A, B, IV, Outcome) df$IVB <- df$IV * df$B mod.med <- ' #Regression A~ a1*IV + a2*B + a3*IV:B Outcome~ c1*IV+ b*A ' my.fit <- sem(mod.med, data=df, missing = 'listwise', bootstrap = 10, se = "bootstrap", ordered ="Outcome", estimator = "DWLS") #> Warning in lav_options_set(opt): lavaan WARNING: information will be set to #> "expected" for estimator = "DWLS" #> name idx nobs type exo user mean var nlev lnam #> 1 A 1 100 numeric 0 0 -0.02342639 0.9369112 0 #> 2 Outcome 4 100 ordered 0 1 NA NA 2 a|b #> 3 IV 3 100 numeric 1 0 -0.13189269 1.0606386 0 #> 4 B 2 100 numeric 1 0 0.05082713 0.9287101 0 #> 5*** IV:B NA 0 NULL 1 0 NA NA 0 #> Error in lav_data_full(data = data, group = group, cluster = cluster, : lavaan ERROR: some variables have no values (only missings) or no variance
(P.S.: I am not sure why IV[M_literacy] is endogenous in your output but endogenous in the example above.)

The following version use IVB, computed before calling sem():

library(lavaan) #> This is lavaan 0.6-9 #> lavaan is FREE software! Please report any bugs. set.seed(4314) n <- 100 B <- rnorm(n) IV <- rnorm(n) A <- rnorm(n) Outcome <- sample(c("a", "b"), n, replace = TRUE) df <- data.frame(A, B, IV, Outcome) df$IVB <- df$IV * df$B mod.med <- ' #Regression A~ a1*IV + a2*B + a3*IVB Outcome~ c1*IV+ b*A ' my.fit <- sem(mod.med, data=df, missing = 'listwise', bootstrap = 10, se = "bootstrap", ordered ="Outcome", estimator = "DWLS") #> Warning in lav_options_set(opt): lavaan WARNING: information will be set to #> "expected" for estimator = "DWLS" summary(my.fit) #> lavaan 0.6-9 ended normally after 10 iterations #> #> Estimator DWLS #> Optimization method NLMINB #> Number of model parameters 8 #> #> Number of observations 100 #> #> Model Test User Model: #> #> Test statistic 1.902 #> Degrees of freedom 2 #> P-value (Chi-square) 0.386 #> #> Parameter Estimates: #> #> Standard errors Bootstrap #> Number of requested bootstrap draws 10 #> Number of successful bootstrap draws 10 #> #> Regressions: #> Estimate Std.Err z-value P(>|z|) #> A ~ #> IV (a1) -0.141 0.059 -2.379 0.017 #> B (a2) -0.017 0.102 -0.166 0.868 #> IVB (a3) -0.135 0.080 -1.680 0.093 #> Outcome ~ #> IV (c1) -0.096 0.179 -0.536 0.592 #> A (b) -0.136 0.108 -1.265 0.206 #> #> Intercepts: #> Estimate Std.Err z-value P(>|z|) #> .A -0.016 0.087 -0.183 0.855 #> .Outcome 0.000 #> #> Thresholds: #> Estimate Std.Err z-value P(>|z|) #> Outcome|t1 -0.058 0.067 -0.862 0.388 #> #> Variances: #> Estimate Std.Err z-value P(>|z|) #> .A 0.894 0.099 8.987 0.000 #> .Outcome 0.983 #> #> Scales y*: #> Estimate Std.Err z-value P(>|z|) #> Outcome 1.000

Created on 2022-01-28 by the reprex package (v2.0.1)

By the way, was DWLS specified explicitly because you want to get bootstrapping CIs? As shown above, robust test statistics, normally reported as in the example at https://lavaan.ugent.be/tutorial/cat.html if ordered is set, will not be computed if we specify estimator = "DWLS" directly.

If you really want to do what the example does but also need bootstrapping CIs, you can add test = "scaled.shifted":

my.fit <- sem(mod.med, data=df, missing = 'listwise', bootstrap = 10, se = "bootstrap", ordered ="Outcome", estimator = "DWLS", test = "scaled.shifted")
#> Warning in lav_options_set(opt): lavaan WARNING: information will be set to #> "expected" for estimator = "DWLS" summary(my.fit) #> lavaan 0.6-9 ended normally after 10 iterations #> #> Estimator DWLS #> Optimization method NLMINB #> Number of model parameters 8 #> #> Number of observations 100 #> #> Model Test User Model: #> Standard Robust #> Test Statistic 1.902 1.934 #> Degrees of freedom 2 2 #> P-value (Chi-square) 0.386 0.380 #> Scaling correction factor 0.986 #> Shift parameter 0.004 #> simple second-order correction #> #> Parameter Estimates: #> #> Standard errors Bootstrap #> Number of requested bootstrap draws 10 #> Number of successful bootstrap draws 10

Created on 2022-01-28 by the reprex package (v2.0.1)

However, I would not do this because there may be other default options that I may forget to set for analyzing endogenous ordered categorical variable (e.g, the warning displayed above).

A better approach is to omit estimator, se, and test, as in the example in https://lavaan.ugent.be/tutorial/cat.html, and let lavaan set up the options for us. Request bootstrap CIs in a separate call to sem(), as you did in the original code, if you need them.

My two cents.

-- Shu Fai

Shu Fai Cheung

unread,
Jan 27, 2022, 11:38:24 PM1/27/22
to lavaan
Sorry for a typo:

(P.S.: I am not sure why IV[M_literacy] is endogenous in your output but exogenous in the example above.)

-- Shu Fai
Reply all
Reply to author
Forward
0 new messages