Interactions in lavaan with one variable classified as “factor” and another variable classified as “numeric”

190 views
Skip to first unread message

Alissa von Großmann

unread,
Sep 29, 2023, 4:29:54 AM9/29/23
to lavaan

Using SEM, I want to test if the association between interpersonal attention and self-disclosure (both continuous variables) is moderated by group membership (a factor). To test this, I aim to include a dummy-coded (0, 1) moderator variable for group membership that is classified as a factor in my model (this is also the way I would normally do this in a classic regression model. A tutorial recommended dealing with categorical exogenous variables the same way in lavaan as in classic regression models, see https://lavaan.ugent.be/tutorial/cat.html). It seems that lavaan is unable to compute the interaction term when the moderator is classified as 'factor'. Is there a common solution to this issue?

I provide my model specification (note that this is just one part of the model, but it demonstrates my problem. I know that I would not need SEM for this model but I need it for the other parts of my analyses). There are no missing values on the variables selfdisclosure, attention and group membership:

test_interaction <- '

selfdisclosure ~ p*attention + h*group + int*attention:group

'

fit.test_interaction <- sem(test_interaction, data=data)

 

and the error message:

Screenshot 2023-09-29 093044.png

Christian Arnold

unread,
Sep 29, 2023, 5:32:38 AM9/29/23
to lavaan
To my knowledge (which is very limited), lavaan is not interested in definitions (classes) like factor. You can add a column to your data.frame, dummy code (numeric) and fit the model. If I understand you correctly, you have 2 groups. Accordingly, I would use a multi-group model. This has numerous advantages.

Alissa von Großmann

unread,
Sep 29, 2023, 10:02:23 AM9/29/23
to lavaan
Thank you for your answer! 

So you are suggesting to dummy code the group membership variable as 'numeric' and not as 'factor'? Then I get no error message and some results. You would just ignore the fact, that lavaan then calculates mean and standarddeviation for the group variable (which does not make sense conceptually)? I wondered if this could somehow influence the results?

And yes, I do have two groups - each participant is in one of these groups. However, the participants are paired in dyads and the group membership sometimes varies within the dyadmembers and sometimes not. The way I specified my whole model at the moment is not suitable for multi-group. Maybe I have to reconsider this. (I wanted to keep the example as easy as possible, this is whiy you can not see the dyadic structure in the code I provided)

Christian Arnold

unread,
Sep 29, 2023, 10:50:12 AM9/29/23
to lav...@googlegroups.com
You don't have to calculate the meanstructure. In addition,  the means don't harm the model and you typically also consider the direct effect of M on Y in a regression style moderation model and then "ignore" it. Other parameters can be constrained between the groups, if this makes sense from a theoretical point of view.

HTH

Christian 

From: lav...@googlegroups.com <lav...@googlegroups.com> on behalf of Alissa von Großmann <alissa.vo...@gmail.com>
Sent: Friday, September 29, 2023 4:02:22 PM
To: lavaan <lav...@googlegroups.com>
Subject: Re: Interactions in lavaan with one variable classified as “factor” and another variable classified as “numeric”
 
--
You received this message because you are subscribed to a topic in the Google Groups "lavaan" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/lavaan/JmE1BwIPCSk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to lavaan+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lavaan/55ccbe73-6300-4e47-a56e-1dd1ab4f8a12n%40googlegroups.com.

Alissa von Großmann

unread,
Sep 29, 2023, 11:57:22 AM9/29/23
to lavaan
Thank you. That was helpful in gaining a better understanding of what lavaan does! For me, the way I treat means and standard deviations is significant, because I have to equate them within the individuals of a dyad. So, if I treat group as 'numeric', I thought I have to include the constraints for mean and standard deviation for group, too (I did not have to do this, when I treated group as 'factor' as than there are no means and standard deviations). However, including these constraints appears to be affecting the results significantly, up to the point where I believe they may be incorrect. I have to think this through again - but I believe this is now more a indistinguishable-dyadic-data-specific question and no longer lavaan-specific :)

Christian Arnold

unread,
Sep 29, 2023, 2:57:59 PM9/29/23
to lav...@googlegroups.com
Great. I am glad that II was abme to help a little bit

Gesendet von Outlook für Android

From: lav...@googlegroups.com <lav...@googlegroups.com> on behalf of Alissa von Großmann <alissa.vo...@gmail.com>
Sent: Friday, September 29, 2023 5:57:22 PM
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/e8a067c0-fc84-4aac-ab64-cc6bb639218en%40googlegroups.com.

Yago Luksevicius de Moraes

unread,
Sep 30, 2023, 11:53:22 AM9/30/23
to lavaan
From what I understood about your problem, I see three alternatives:

1) Do a multi-group analysis
2) lavaan does not leads well with exogenous qualitative data. The best way to deal with them is to treat them as ordinal variables (what is not a problem with dichotomous factors) and correlate it with something (it can be a zero correlation, but it has to appear in the model ´group~~something')
3) treat group as numeric. I do not recommend this option because it underestimates group's coefficients, but if you are interested only in the structure, this might be a valid alternative.

Further, your error message is "some variables have no variance". Check if, inside each group, there are at least two values for both selfdisclosure and attention.

Hope this helps,
Yago

Alissa von Großmann

unread,
Oct 5, 2023, 2:56:43 AM10/5/23
to lavaan
Hi Yago, 

Sorry for my late reply! Thank you for your answer. Could you explain your second alternative further? Or do you know a good resource where I could get some information? I do not understand how correlating the ordinal variable with something helps. That would be great.

The error message you mentioned only appears when I include the interaction term, as Lavaan seems unable to compute it with an ordinal variable. There is variance in each group for selfdisclosure and attention.

Thank you!

Yago Luksevicius de Moraes

unread,
Oct 5, 2023, 1:14:01 PM10/5/23
to lav...@googlegroups.com
Hi, Alissa,

I'm afraid I do not know exactly why exogenous ordinal variables need to be correlated with something, but it is something I've observed in all models I've tested so far. I suppose it has something to do with how polychoric correlations are calculated.

Basically, if you do something like
```{r}
model <- 'DV~sex'
```
with sex treated as an ordinal dichotomous item, lavaan will give an error saying that there are exogenous ordinal items. But if you do

```{r}
model <- 'DV~sex
sex~~age
```
Then, lavaan will analyze the model without complaining.

However, this was just a suggestion of what to try, I'm not sure if this will settle your problem because in your case, the error is lack of variance in the interaction term. I've never done tests with interactions. As a matter of fact, I always thought lavaan couldn't handle interactions, and, in your case, it says the interaction is a logical argument (TRUE/FALSE) with zero observations. 
Thus, I believe you'll need to do option 1 or calculate the interaction manually, as explained in Finch, W., & French, B. (2015). Latent variable modeling with R (pp.117-124). Routledge. doi: 10.4324/9781315869797.

P.S.: As I said, I've never tested models with interactions, so I'm not sure this will settle the problem. These are educated guesses based on the error messages you are getting, and I would very much appreciate others opinions.

Best,
Yago


--
You received this message because you are subscribed to a topic in the Google Groups "lavaan" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/lavaan/JmE1BwIPCSk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to lavaan+un...@googlegroups.com.


--
Yago Luksevicius de Moraes
Master in Experimental Psychology
Bachelor in Psychology

Ibrahim Nasser

unread,
Oct 5, 2023, 5:55:13 PM10/5/23
to lav...@googlegroups.com
Sounds like guessing.

Ibrahim 

--
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

Alissa von Großmann

unread,
Oct 6, 2023, 3:54:38 AM10/6/23
to lavaan
Thank you Yago! That is quite interesting, because when I specify my model like this:

test_nointeraction <- 'selfdisclosure ~ p*attention + h*group'

I have no problems with running my model and I do not have to correlate group with something

Anyway, I tried your suggestion of correlating group with something (including the interaction term) but the error message remains the same. So, I guess you are right and I have to either calculate the interaction term manually or reconsider my model specification and use a multi-group approach.

Yago Luksevicius de Moraes

unread,
Oct 6, 2023, 9:51:09 AM10/6/23
to lav...@googlegroups.com
I see. Sorry for not being more helpful.
I hope you find a way to test your model.

Best,
Yago

Reply all
Reply to author
Forward
0 new messages