compute marginal effects from sem model in lavaan

110 views
Skip to first unread message

max meier

unread,
Oct 26, 2020, 9:36:30 AM10/26/20
to lavaan
Hi there,

i am wondering if there is a possibility to get an output for the average marginal effects of a fitted sem lavaan model? Unfortunately, the ggeffects and margins packages do not offer the possibility to calculate the average marginal effects from a sem model.

Greetings,
Max

Terrence Jorgensen

unread,
Oct 26, 2020, 4:23:40 PM10/26/20
to lavaan
the average marginal effects of a fitted sem lavaan model? 

What is that?  Can you provide a simple model and calculation to show us what exactly you are looking for?

Terrence D. Jorgensen
Assistant Professor, Methods and Statistics
Research Institute for Child Development and Education, the University of Amsterdam

 

max meier

unread,
Oct 27, 2020, 5:22:55 AM10/27/20
to lavaan
Sure!

If I fit a model in lavaan like:

sem_model <- '
betrug ~ alter + geschlecht + abitur + sat
sat =~ v30a + v30b + v30c'
fit <- sem(sem_model, data = daten, ordered = TRUE, estimator = "WLSMV")

I get the summary of the model with summary(). The coefficients (standardized/unstandardized) of the different variables in the model are reported there, but to compare the effects of different variables in several models, it is easier to compare them with average marginal effects.

For example in Stata: If I fit a similar model as above, I can get the average marginal effects calculated after the model is fitted:


(table is shortened)
. margins, dydx(*)

Average marginal effects    Number    of    obs    =    298
Model VCE    : OIM
                       
        Delta-method
    dy/dx              Std. Err.          z        P>z        [95% Conf.    Interval]
                 
      
abitur   
_predict   
1    -.1492429    .0430974    -3.46    0.001    -.2337122    -.0647735
2    -.0139091    .0041602    -3.34    0.001    -.0220629    -.0057553
3    -.0181716    .0054299    -3.35    0.001    -.028814    -.0075293
4    -.0168459    .0050291    -3.35    0.001    -.0267027    -.0069891
                       
So, for example an increase of one unit of abitur (it´s a binary variable) in the table above would result in a reduction of the probability of Betrug (1. Row) of 14%.

That is what I am looking for. As I said, the packages margins and ggeffects in R unfortunately won't calculate the average marginal effects for structural equation models.

Terrence Jorgensen

unread,
Oct 28, 2020, 6:27:07 AM10/28/20
to lavaan
fit <- sem(sem_model, data = daten, ordered = TRUE, estimator = "WLSMV")

The ordered= argument is not logical, it is a character vector naming which variables are ordinal.

in Stata: If I fit a similar model as above, I can get the average marginal effects calculated after the model is fitted:

It is not clear to me what you are marginalizing over.  The effect of abitur in your SEM will be a marginal effect on betrug (controlling for the other 3 predictors) because it is not involved in any interactions or nonlinear terms that would imply it has a conditional effect in different contexts.  From your description, the best I can guess is that you are averaging across models of the same outcome that have different combinations of predictors.  I have never heard that called a "marginal" effect (which is quite vague given it is already marginal in a single model), but it sounds like Bayesian model averaging.  

If you want to do something like that, I'm pretty certain you would have to calculate the average effects yourself.  But I can't imagine how you would obtain valid test statistics when averaging across models that do not share an asymptotic covariance matrix.  I expect that's why I have only heard about model averaging being done with Bayesian methods.  Here are a couple papers I just found searching on Google that might be informative for you (assuming I haven't completely misunderstood your goal):  


Patrick (Malone Quantitative)

unread,
Oct 28, 2020, 10:22:15 AM10/28/20
to lav...@googlegroups.com
Terrence,

In recent versions, cfa() can take "ordered = TRUE" to mean all
observed variables are ordered. I'm not sure what sem() does with it,
but it might be allowed.

Pat
> --
> 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 view this discussion on the web visit https://groups.google.com/d/msgid/lavaan/2e27e31d-93c5-4d21-8358-b6906c19c1f8o%40googlegroups.com.



--
Patrick S. Malone, Ph.D., Malone Quantitative
NEW Service Models: http://malonequantitative.com

He/Him/His

Terrence Jorgensen

unread,
Oct 29, 2020, 5:32:55 AM10/29/20
to lavaan
In recent versions, cfa() can take "ordered = TRUE" to mean all
observed variables are ordered.

Ah, I had not noticed that change.  Thanks!
 
I'm not sure what sem() does with it,

cfa() and sem() are identical wrappers around lavaan(), so they set the same defaults.

HS.model <- ' visual =~ x1 + x2 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9 ' fit1 <- cfa(HS.model, data = HolzingerSwineford1939)fit2 <- sem(HS.model, data = HolzingerSwineford1939)
fit1@call
# compare to additional arguments
fit2@call
# set in the ?lavaan help-page example

Patrick (Malone Quantitative)

unread,
Oct 29, 2020, 8:51:11 AM10/29/20
to lav...@googlegroups.com
>> I'm not sure what sem() does with it,
>
>
> cfa() and sem() are identical wrappers around lavaan(), so they set the same defaults.

Yes. What I didn't say and should have was I hadn't looked at how or
if it affected exogenous observed variables in sem(), since the
question doesn't come up in cfa(). Only endogenous variables would be
what makes sense, of course. The changelog says

"using ordered = TRUE implies all variables should be treated as ordinal"

But the full documentation clarifies that it's the sensible thing:

" If TRUE, all observed endogenous variables are treated as ordered (ordinal)"

Pat

>
> HS.model <- ' visual =~ x1 + x2 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9 ' fit1 <- cfa(HS.model, data = HolzingerSwineford1939)fit2 <- sem(HS.model, data = HolzingerSwineford1939)
> fit1@call # compare to additional arguments
> fit2@call # set in the ?lavaan help-page example
>
>
> Terrence D. Jorgensen
> Assistant Professor, Methods and Statistics
> Research Institute for Child Development and Education, the University of Amsterdam
> http://www.uva.nl/profile/t.d.jorgensen
>
> --
> 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 view this discussion on the web visit https://groups.google.com/d/msgid/lavaan/2143b3e5-bd07-4432-94f6-6039c78bee06o%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages