如何使用 R 中的 lavaan 包在 SEM 模型中对分类自变量和连续调节剂(通过 CFA 创建)之间的交互进行建模?
特别是,在我的真实数据集中,我基本上对在 SEM 中重新创建双向 ANOVA 感兴趣,并且还希望包含一个调节变量来测试每个因子变量。
示例数据和问题:
### load packages: ###
library(dplyr)
library(lavaan)
library(car)
library(psychTools)
### Create some data: ###
# Dependent variable: Taken example data from psychTools
DV1 <- bfi$A1 # item 1
DV2 <- bfi$A2 # item 2
DV3 <- bfi$A3 # item 3
# Moderating variable: Taken example data from psychTools
MOD1 <- bfi$C1 # item 1
MOD2 <- bfi$C2 # item 2
MOD3 <- bfi$C3 # item 3
# Create example factor variables
x1 <- c("A","B")
x2 <- c("C","D")
set.seed(1)
FAC1 <- as.factor(sample(x1, 200, replace = TRUE)) # Factor 1, with two levels "A" and "B"
FAC2 <- as.factor(sample(x2, 200, replace = TRUE)) # Factor 2, with two levels "C" and "D"
FAC12 <- interaction(FAC1,FAC2) # Factor 12, interaction of FAC1 and FAC2, with four levels "A.C" "B.C" "A.D" "B.D"
# Combine to data frame
StudyData <- data.frame(DV1,DV2,DV3,
MOD1,MOD2,MOD3,
FAC1, FAC2, FAC12)
### Make all categorical variables numeric for use in SEM (orthogonal contrast coded as in ANOVA): ###
StudyData$FAC1 <- recode(StudyData$FAC1, "c('A')='-1';
c('B')='1'")
StudyData$FAC1 <- as.numeric(levels(StudyData$FAC1))[StudyData$FAC1]
StudyData$FAC2 <- recode(StudyData$FAC2, "c('C')='-1';
c('D')='1'")
StudyData$FAC2 <- as.numeric(levels(StudyData$FAC2))[StudyData$FAC2]
StudyData$FAC12 <- recode(StudyData$FAC12, "c('A.D','B.C')='-1';
c('A.C','B.D')='1'")
StudyData$FAC12 <- as.numeric(levels(StudyData$FAC12))[StudyData$FAC12]
### SEM Model One: ###
Model.one <- '
# cfa
DV =~ DV1 + DV2 + DV3
MOD =~ MOD1 + MOD2 + MOD3
# regressions
DV ~ FAC1 + FAC2 + FAC12
'
Modelone <- sem(Model.one, StudyData, estimator="MLM", effect.coding=TRUE, meanstructure=TRUE)
summary(Modelone)
fitMeasures(Modelone, c("chisq","cfi","rmsea","srmr","nfi","gfi"))
### SEM Model Two: ###
Model.two <- '
# cfa
DV =~ DV1 + DV2 + DV3
MOD =~ MOD1 + MOD2 + MOD3
# regressions
DV ~ FAC1 + FAC1:MOD + FAC2 + FAC12
'
Modeltwo <- sem(Model.two, StudyData, estimator="MLM", effect.coding=TRUE, meanstructure=TRUE)
summary(Modeltwo)
fitMeasures(Modeltwo, c("chisq","cfi","rmsea","srmr","nfi","gfi"))
### SEM Model Three: ###
Model.three <- '
# cfa
DV =~ DV1 + DV2 + DV3
MOD =~ MOD1 + MOD2 + MOD3
# regressions
DV ~ FAC1:MOD
'
Modelthree <- sem(Model.three, StudyData, estimator="MLM", effect.coding=TRUE, meanstructure=TRUE)
summary(Modelthree)
fitMeasures(Modelthree, c("chisq","cfi","rmsea","srmr","nfi","gfi")
模型一运行良好。我可以在 SEM 环境中运行我的“ANOVA”。
但是,当我想运行模型二时,其中包括 FAC1 和 MOD 之间的交互项(通过 SEM 模型中的 CFA 创建),我收到错误:
“lavaan WARNING:估计参数(vcov)的方差-协方差矩阵似乎不是正定的!最小特征值(= -3.458498e-20)小于零。这可能是模型不是识别出来。”
问题:
- 难道不能以这种方式在lavaan中创建一个因子:持续交互吗?
- 是否有任何解决方法以及如何做?(例如,提取 MOD 的 CFA 期间计算的值并在 SEM 之外计算 FAC1:MOD 交互,然后在 SEM 的路径分析(回归)部分重新使用变量)
- Mplus 可以在不需要变通的情况下做到这一点吗?
编辑:添加“SEM 模型三”和精益交互模型以回应评论。