mcp2matrix(模型,linfct = linfct)中的错误:class“整数”is/are 的变量“方法”未作为“模型”中的一个因素包含

Error in mcp2matrix(model, linfct = linfct) : Variable(s) ‘Methode’ of class ‘integer’ is/are not contained as a factor in ‘model’

我不明白为什么它不起作用这是我的代码:

     library(lme4,multcomp)

tijd <- c(7.667, 9.5, 6.467, 10.033, 9.767, 29.133, 15.933, 8.1, 8.6, 
          12.3, 5.867, 13.667, 12.933, 11.933, 27.2, 11.567, 10.9, 12.567, 
          9.033, 12.6, 13.133, 17.067, 14.333, 15.2, 9.1, 9.167, 8.4, 11.4, 
          18.467, 15.633, 10.4, 10.767, 12.7, 7.667, 9.867, 36.167, 20.4, 
          33.8, 15.767, 20.1, 10.533, 17.7, 62.667, 21.133, 40.133, 9.2, 
          19.467, 7.667, 8.867, 20.333, 14.367, 10.167, 28.4, 11.367, 10.867, 
          22.1, 24.8, 13.3, 17.067, 10.967, 12.333, 28.867, 21.1)

week <- c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3)

Methode <- c(1, 1, 2, 2, 3, 3, 4, 4, 1, 1, 2, 2, 3, 3, 4, 
            4, 1, 1, 2, 2, 3, 3, 4, 4, 1, 1, 2, 2, 3, 3, 4, 
            1, 1, 2, 2, 3, 3, 4, 1, 1, 2, 2, 3, 3, 4, 1, 1, 
            2, 2, 3, 3, 1, 1, 2, 2, 3, 3, 1, 1, 2, 2, 3, 3)

freq <- c(c(5.9, 5.9, 5.9, 5.9, 5.9, 5.9, 5.9, 5.9, 8, 8, 8, 8, 8, 8, 8, 
            8, 10, 10, 10, 10, 10, 10, 10, 10, 5.9, 5.9, 5.9, 5.9, 5.9, 5.9, 
            5.9, 8, 8, 8, 8, 8, 8, 8, 10, 10, 10, 10, 10, 10, 10, 5.9, 5.9, 
            5.9, 5.9, 5.9, 5.9, 8, 8, 8, 8, 8, 8, 10, 10, 10, 10, 10, 10))

ID <-c(1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 
       8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 8, 
       1, 2, 3, 4, 5, 6, 8, 1, 2, 3, 4, 5, 6, 8, 1, 2, 
       3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6)

week <- as.factor(week)
Methode <- as.factor(Methode)
ID <- as.factor(ID)


M1 <- lmer(log(tijd) ~ Methode + (1|freq) + (1|week) + (1|ID), data = data)
summary(M1)
anova(M1, type = "III", test = "F")

plot(M1)
plot(cooks.distance(M1))


summary(glht(M1, linfct = mcp(Methode = "Tukey")))

这是我的数据集的第一行:https://imgur.com/RYr9nNp 我目前正在写我的学士论文,如果你能帮助我,那就太好了。

提前致谢:)

在你的 lmer 代码中,你依赖 data 但你没有将 data$Methode 转换为因子,那么你的代码应该是:

data$week <- as.factor(data$week)
data$Methode <- as.factor(data$Methode)
data$ID <- as.factor(data$ID)

M1 <- lmer(log(tijd) ~ Methode + (1|freq) + (1|week) + (1|ID), data = data)

您在图像中显示的数据不包含与 lmer 代码相同的列名称,因此您必须注意(例如:frequentie 和 freq)