我尝试使用"中介"包运行HLM中介:
med.fit <- glmer(M ~ treat + control + (1|subject_id) ,family = binomial(link = "logit"), data = R1_data)
out.fit <- glmer(Y ~ M+ treat + control+ (1 + M|subject_id),family = binomial(link = "logit"), data = R1_data)
med.out <- mediate(med.fit, out.fit, treat = "treat", mediator = "M", sims = 1000)
我收到此错误消息:
[.data.frame
错误 (y.data, int.term.name[p]( : 选择了未定义的列
如何解决这个问题?
以下是原始数据和代码:
names(R1_data)
[1] "subject_id"
[3] "Presented_is_solvable"
[5] "JOS"
[17] "Answer_JOS"
[23] "Matrix_Z_score"
library(mediation)
library(lme4)
med.fit <- glmer(JOS ~ Matrix_Z_score + Presented_is_solvable + (1|subject_id) ,family = binomial(link = "logit"), data = R1_data)
out.fit <- glmer(Answer_JOS ~ JOS + Matrix_Z_score +Presented_is_solvable + (1 + JOS|subject_id),family = binomial(link = "logit"), data = R1_data)
med.out <- mediate(med.fit, out.fit, treat = "Matrix_Z_score", mediator = "JOS", sims = 1000)
发现当治疗或调解员数据在 R 中被归类为因子数据时会发生这种情况。中介函数无法像模型中那样从拟合模型中正确定位这些变量的名称,它们显示为"变量名称"+因子水平。
解决方案是确保这些变量被分类为整数。您可以查看中介包中student
数据集中的变量分类。