[免责声明:此问题已在Google小组中针对lavaan(链接:https://groups.google.com/forum/#!topic/lavaan/nI8SUIU8JCM)进行了交叉发布。它也张贴在这里以提高可见度。]
对于交叉滞后的面板SEM,我有大约3000个数据(但只有265个完整的案例)。我的分析有点像下面的附图。比如,在我们的例子中,A = AGE(连续),B =性别(二进制),X = emp(二进制),Y = SA(二进制),Z = SE(4类)。所有变量都是观察到的变量。换句话说,我们想要测试我们假设的emp,SA和SE之间的跨滞后关系,其中AGE和性是这些关系的混杂因素。在这些观察到的变量的不同时间点存在缺失值(NA)。
1)我首先尝试使用以下代码来运行一个简单的模型,只使用emp和SE而没有任何混淆,通过http://lavaan.ugent.be/tutorial/cat.html中的建议声明分类的内生观察变量。
d <- read.csv('newdata.csv', header=T)
# Convert the variables to factors
d$sex <- factor(d$sex)
d$emp1 <- ordered(d$emp1)
d$emp2 <- ordered(d$emp2)
d$emp5 <- ordered(d$emp5)
d$SA1 <- ordered(d$SA1)
d$SA2 <- ordered(d$SA2)
d$SA5 <- ordered(d$SA5)
d$SE1 <- ordered(d$SE1)
d$SE2 <- ordered(d$SE2)
d$SE5 <- ordered(d$SE5)
library(lavaan)
clpm0 <- '
# synchronous covariances
SA1 ~~ emp1
SA2 ~~ emp2
SA5 ~~ emp5
# autoregressive + cross-lagged paths
SA2 ~ SA1 + emp1
emp2 ~ emp1 + SA1
SA5 ~ SA2 + emp2
emp5 ~ emp2 + SA2
'
fit0 <- sem(clpm0, data=d)
但是这会产生以下错误消息:
Error in tmp[cbind(REP$row[idx], REP$col[idx])] <- lavpartable$free[idx] :
NAs are not allowed in subscripted assignments
我也尝试了以下内容:
fit0 <- sem(clpm0, data=d, missing='listwise')
但得到了同样的错误信息:
Error in tmp[cbind(REP$row[idx], REP$col[idx])] <- lavpartable$free[idx] :
NAs are not allowed in subscripted assignments
我很确定我犯了一些错误但是作为新的lavaan用户无法弄清楚我的错误。你能告诉我这里的错误是什么吗?在这些情况下,当我理论上使用missing ='pairwise'时会发生什么?因为那时它给我看了像:
Error in tmp[cbind(REP$row[idx], REP$col[idx])] <- lavpartable$free[idx] :
NAs are not allowed in subscripted assignments
In addition: Warning message:
In lav_data_full(data = data, group = group, cluster = cluster, :
lavaan WARNING: some cases are empty and will be ignored:
1 2 10 14 16 18 19 20 21 31 33 58 60 62 98 105 188 209 253 293 303 306 308 324 328 341 346 392 425 435 436 498 500 511 517 518 540 542 548 555 557 574 581 583 585 595 596 597
2)其次,我试图将混杂的AGE和性别结合起来。即使第一部分的问题得到解决,我仍然感到困惑,如果代码应该如下:
clpm1 <- '
# synchronous covariances
SA1 ~~ emp1
SA2 ~~ emp2
SA5 ~~ emp5
# autoregressive + cross-lagged paths
SA2 ~ SA1 + emp1 + AGE + sex
emp2 ~ emp1 + SA1 + AGE + sex
SA5 ~ SA2 + emp2 + AGE + sex
emp5 ~ emp2 + SA2 + AGE + sex
'
fit1 <- sem(clpm1, data=d, missing='listwise')
然后它显示错误:
Error in vnames(FLAT, type = "ov.x", ov.x.fatal = TRUE) :
lavaan ERROR: model syntax contains variance/covariance/intercept formulas
involving (an) exogenous variable(s): [SA1 emp1];
Please remove them and try again.
In addition: Warning message:
In lavaan::lavaan(model = clpm1, data = d, missing = "listwise", :
lavaan WARNING: syntax contains parameters involving exogenous covariates; switching to fixed.x = FALSE
3)最后一个问题是:如果我想用附图中显示的模型图进行完整分析,下列代码需要做哪些更改?
clpm2 <- '
# synchronous covariances
SA1 ~~ emp1
SA2 ~~ emp2
SA5 ~~ emp5
SE1 ~~ emp1
SE2 ~~ emp2
SE5 ~~ emp5
SE1 ~~ SA1
SE2 ~~ SA2
SE5 ~~ SA5
# autoregressive + cross-lagged paths
SE1 ~ SA1 + emp1 + AGE + sex
emp2 ~ emp1 + SA1 + AGE + sex
SA2 ~ SA1 + emp1 + AGE + sex
SE2 ~ SE1 + SA1 + emp1 + SA2 + emp2 + AGE + sex
emp5 ~ emp2 + SA2 + AGE + sex
SA5 ~ SA2 + emp2 + AGE + sex
SE5 ~ SE2 + SA2 + emp2 + SA5 + emp5 + AGE + sex
'
# fit the model
fit1 <- sem(clpm1, data=d, missing='listwise')