The colon operator in specifying a lavaan model

173 views
Skip to first unread message

Shu Fai Cheung

unread,
Sep 2, 2021, 10:09:28 PM9/2/21
to lavaan
I've just learned from another post that we can use ":" to ask lavaan to create a product term (interaction term) when fitting a model. This is a very useful feature because, just like lm, we do not need to create the product terms in the data frame.

Is this a new feature? I checked the help page of lavaan syntax (version 0.6-9) and could not find the documentation on this operator. Is this an experimental feature?

I would be happy to introduce this to my students if it is not experimental.

-- Shu Fai

Shu Fai Cheung

unread,
Sep 2, 2021, 11:38:15 PM9/2/21
to lavaan
Does the colon operator currently support only two-way interaction? I tried something like x1:x2:x3 and it does not work:

```
sigma <- matrix(.4, 4, 4)
diag(sigma) <- 1
set.seed(84324)
dat <- MASS::mvrnorm(100, rep(0, 4), sigma)
colnames(dat) <- paste0("x", 1:4)
mod <-
"
x4 ~ x1 + x2 + x3 + x1:x2 + x1:x3 + x2:x3 + x1:x2:x3
"
fit <- sem(mod, dat)
parameterEstimates(fit, remove.nonfree = TRUE)

```

The output:
```
> parameterEstimates(fit, remove.nonfree = TRUE)
  lhs op   rhs    est    se      z pvalue ci.lower ci.upper
1  x4  ~    x1  0.041 0.095  0.436  0.663   -0.144    0.227
2  x4  ~    x2  0.232 0.115  2.014  0.044    0.006    0.458
3  x4  ~    x3  0.266 0.105  2.526  0.012    0.060    0.473
4  x4  ~ x1:x2 -0.054 0.084 -0.640  0.522   -0.219    0.111
5  x4  ~ x1:x3 -0.034 0.106 -0.326  0.745   -0.242    0.173
6  x4  ~ x2:x3 -0.005 0.111 -0.046  0.963   -0.222    0.212
7  x4 ~~    x4  0.737 0.104  7.071  0.000    0.533    0.941

```

-- Shu Fai

Terrence Jorgensen

unread,
Sep 17, 2021, 4:39:57 PM9/17/21
to lavaan
Is this a new feature? 

It has been around for about 5 years (0.5-21):   https://lavaan.ugent.be/history/dot5.html
But it is one of the details that has not made it into the ?model.syntax documentation.
Indeed, I don't think a three-way interaction was foreseen.  I posted your example on GitHub


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

Shu Fai Cheung

unread,
Sep 19, 2021, 11:15:24 PM9/19/21
to lavaan
Thanks! My fault! Really need to read carefully the release history.

This is a good feature. It makes writing the model syntax more intuitive and also reduces the chance of human error in computing the product terms before calling lavaan. I will promote it to my students. :)

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