lavaan ERROR: contains either a reserved word (in R) or an illegal character: “dem65:ind60”

376 views
Skip to first unread message

Ale Dea

unread,
May 12, 2021, 6:11:19 AM5/12/21
to lavaan
Hi all,

Is it possible to run sem with a an interaction variable on the lhs of the regression by using the : operator. I have tried (please see example below) but I get the following error 

Error in lavParseModelString(model) : 
  lavaan ERROR: left hand side (lhs) of this formula:
    dem65:ind60 ~ dem60
    contains either a reserved word (in R) or an illegal character: “dem65:ind60”
    See ?reserved for a list of reserved words in R
    Please use a variable name that is not a reserved word in R
    and use only characters, digits, or the dot symbol.

Is this a bug of the command? Please see a very simple example below to illustrate the error 

model <- '
  # measurement model
    ind60 =~ x1 + x2 + x3
    dem60 =~ y1 + y2 + y3 + y4
    dem65 =~ y5 + y6 + y7 + y8
  # regressions
    dem65:ind60 ~   + dem60
  # residual correlations
    y1 ~~ y5
    y2 ~~ y4 + y6
    y3 ~~ y7
    y4 ~~ y8
    y6 ~~ y8
'
fit <- sem(model, data = PoliticalDemocracy)

Many thanks

Pat Malone

unread,
May 12, 2021, 7:20:24 AM5/12/21
to lav...@googlegroups.com
Ale,

I don't recognize the syntax in that line. What regressions are you trying to estimate?

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/40e7aa93-210e-413a-be8e-7851af198bb0n%40googlegroups.com.


--
Patrick S. Malone, PhD
Sr Research Statistician, FAR HARBΦR
This message may contain confidential information; if you are not the intended recipient please notify the sender and delete the message.

Ale Dea

unread,
May 12, 2021, 8:15:19 AM5/12/21
to lavaan
Hi Pat,

I would like to estimate the following regressions

Y = b*X + c*Z + d*XZ
and
XZ = e*W

Where X and Z are obtained from factor models.
In the regressions, I interact the factor scores X and Z to see their effect on Y.
I am also interested in how the interaction between these two factor scores depend on another variable W.

I thought I could introduce interactions between variables by using the : operator in the lavaan syntax. But I think I may be mistaken

Thanks
Ale 

Mauricio Garnier-Villarreal

unread,
May 12, 2021, 12:12:58 PM5/12/21
to lavaan
Ale

I havent seen anyone tryong something like this. But a few pointers of what works in lavaan

- the : for interactions only works with observed variables, not factors
- if you want to create an interaction factor and predict it, my best guess is that the product indicator approach is the best option. You can follow the examples from the indprod() function in semTools
- when you say "I am also interested in how the interaction between these two factor scores depend on another variable W.". It sounds to me that you should do a three way interaction instead, as this addresses how a 2 way interaction is affected by a third variable. Instad of predicting the product term

Ale Dea

unread,
May 13, 2021, 8:45:09 AM5/13/21
to lavaan
Thank you for your answers! If I understand correctly, you are saying is that it is not possible to interact factor scores in lavaan when estimating the regression in one step?

It should be possible to do this in two-step but then I would need to correct from the bias in the prediction of the scores. Is there a simple way to do it?
I have tried with the sam function, but it does not work..

Pat Malone

unread,
May 13, 2021, 9:05:44 AM5/13/21
to lav...@googlegroups.com
Ale,

As Mauricio said, the recommendation, at least for a two-way interaction, is the product-indicator approach. This is done in one model and does not involve factor scores or bias.

Pat

Ale Dea

unread,
May 14, 2021, 7:07:20 AM5/14/21
to lavaan
I see, I will post here an example of the code for others who may have encountered the same problem. The code is for illustrative purpose, please feel free to amend it. 

# Get data for interaction factor
data.interac <- indProd(PoliticalDemocracy,var1 = c('x1','x2','x3'), var2 = c('y1','y2'), match=FALSE, meanC=FALSE, residualC=FALSE, doubleMC=FALSE)

# define sem model
model <- ' 
  # latent variable definitions
     ind60 =~ x1 + x2  +x3
     dem60 =~ y3 + y4 +y5
     ind=~y1 + y2  
# define interaction factor
ind60ind =~ x1.y1 +x1.y2+x2.y1+x2.y2 +x3.y1 +x3.y2
  # regressions
    dem60 ~ ind60+ ind+ ind60ind
'
# estimate model
sem_model <- sem(model, data=data.interac, std.lv=TRUE) 

Ale Dea

unread,
May 14, 2021, 7:10:08 AM5/14/21
to lavaan
Two additional queries related to this:

(1) Is it possible to do this when x1, x2,x3,y1,y2 are categorical? 
(2) When is it recommend it to set  meanC=TRUE?

Terrence Jorgensen

unread,
May 15, 2021, 12:36:07 AM5/15/21
to lavaan
(1) Is it possible to do this when x1, x2,x3,y1,y2 are categorical? 

No, unless they are exogenous dummy codes treated as numeric.
 
(2) When is it recommend it to set  meanC=TRUE?

Read the source articles in the References section of the ?indProd help page to make your decision.  Those were the basis for the chosen default settings, but you are free to disagree.

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

Ale Dea

unread,
May 16, 2021, 4:17:51 AM5/16/21
to lavaan
Hi Terrence, 

So it is not possible if x1,x2,x3 take 0,1,2 as values and y1 and y2 take 0 or 1?

Is there a way around this?

Thanks

Terrence Jorgensen

unread,
May 26, 2021, 12:37:48 AM5/26/21
to lavaan
So it is not possible if x1,x2,x3 take 0,1,2 as values and y1 and y2 take 0 or 1?

y1 and y2 are irrelevant because they are the outcome's indicators.

 Is there a way around this?

Only to treat them as numeric rather than ordinal.  Or use MCMC estimation, in which case you can draw factor scores from the posterior and calculate their products to estimate latent interaction effects. Then you wouldn't need to mess around with product indicators at all.

Ale Dea

unread,
Jun 8, 2021, 10:48:39 AM6/8/21
to lavaan
Thanks Terrence. By MCMC, do you mean draw monte carlo simulations? Do I implement this with blavaan and do you know where I may find an example?
Many thanks
Ale

Terrence Jorgensen

unread,
Jun 13, 2021, 8:53:19 AM6/13/21
to lavaan
By MCMC, do you mean draw monte carlo simulations?

I mean use Markov chain Monte Carlo estimation, which simulates parameters that for the given data.
 
Do I implement this with blavaan and do you know where I may find an example

blavaan does use MCMC estimation, but it is not yet capable of handling categorical outcomes, so you would have to learn to write your own syntax in JAGS or Stan.  You can find lots of syntax examples in the book by Song & Lee (2012), including models with categorical indicators and models with latent nonlinear effects (quadratic, interactions).

Reply all
Reply to author
Forward
0 new messages