Hi All,
Suppose a mediation model with the b-path moderated is fitted:
library(lavaan)
#> This is lavaan 0.6-19
#> lavaan is FREE software! Please report any bugs.
mod <-
"
x2 ~ x1
x3 ~ x2 + x4 + x2:x4 + x1
"
dat <- HolzingerSwineford1939[, paste0("x", 1:9)]
fit1 <- sem(mod,
dat,
fixed.x = FALSE)
fit1
#> lavaan 0.6-19 ended normally after 10 iterations
#>
#> Estimator ML
#> Optimization method NLMINB
#> Number of model parameters 11
#>
#> Number of observations 301
#>
#> Model Test User Model:
#>
#> Test statistic 1090.398
#> Degrees of freedom 4
#> P-value (Chi-square) 0.000
fit2 <- sem(mod,
dat + 2,
fixed.x = FALSE)
fit2
#> lavaan 0.6-19 ended normally after 11 iterations
#>
#> Estimator ML
#> Optimization method NLMINB
#> Number of model parameters 11
#>
#> Number of observations 301
#>
#> Model Test User Model:
#>
#> Test statistic 1289.517
#> Degrees of freedom 4
#> P-value (Chi-square) 0.000
This model syntax does not work because the model is not invariant to linear transformation of the variables. This is a known issue, and we need to free the covariance between the interaction term interaction x2:x4 and the error term of x2, the mediator, among some other covariances fixed by lavaan to zero.
The following syntax will work. Other covariances need to be specified manually because we manually free the covariance between the interaction term and an error term:
modb <-
"
x2 ~ x1
x3 ~ x2 + x4 + x2:x4 + x1
x1 ~~ x4 + x2:x4
x4 ~~ x2 + x2:x4
x2 ~~ x2:x4
"
fitb1 <- sem(modb,
dat,
fixed.x = FALSE)
fitb1
#> lavaan 0.6-19 ended normally after 75 iterations
#>
#> Estimator ML
#> Optimization method NLMINB
#> Number of model parameters 15
#>
#> Number of observations 301
#>
#> Model Test User Model:
#>
#> Test statistic 0.000
#> Degrees of freedom 0
The model is a saturated model, as it should be.
However, an error will be raised if the interaction term "x2:x4" appears on the left-hand side, "x2:x4 ~~ x2" in the following example:
modc <-
"
x2 ~ x1
x3 ~ x2 + x4 + x2:x4 + x1
x1 ~~ x4 + x2:x4
x4 ~~ x2 + x2:x4
x2:x4 ~~ x2
"
fitc1 <- sem(modc,
dat,
fixed.x = FALSE)
#> Error: lavaan->ldw_parse_model_string():
#> Invalid block identifier. The correct syntax is: "LHS: RHS", where LHS is
#> a block identifier (eg group or level), and RHS is the group/level/block
#> number or label. at line 5, pos 1
#> x2:x4 ~~ x2
#> ^
For this particular model, the problem can be solved by not placing the interaction term on the left-hand side.
However, is there any workaround or unofficial "trick" to specify an interaction term on the left-hand side without resulting in this error?
I have this question because what I want to fit is a more complicated model with more than one interaction term, and I will need to free a covariance like "x2:x4 ~~ x3:x5".
This error can be avoided by forming the interaction terms in the data directly, e.g., x2_x4 <- x2 * x4 and x3_x5 <- x3 * x5, and not using ":". However, for some reason, I would like to use ":", if this is possible.
-- Shu Fai