【问题标题】:Posthoc test nested anova using multcomp in R在 R 中使用 multcomp 进行事后测试嵌套方差分析
【发布时间】:2020-02-24 13:52:19
【问题描述】:

我的数据嵌套在组级别。有五种不同的治疗方法。在每次治疗中,将四名参与者分组。它是关于那些参与者在竞争下的捐赠行为(因变量 = 捐赠,度量,单位为欧元)(解释变量 = 治疗,序数)。数据结构是这样的:

Treatment   Session   player.cumulative_donation:
CG             uk4rlbdo         2.5
CG             uk4rlbdo         1.4 
CG             uk4rlbdo         0
CG             uk4rlbdo         1
CG             dg0bqvit         0
CG             dg0bqvit         0
CG             dg0bqvit         0.5
CG             dg0bqvit         0
TG1            g6n3z46r         1
TG1            g6n3z46r         0
TG1            g6n3z46r         0
TG1            g6n3z46r         0.2

基于Rcompanion 计算方差分析后,我想使用 multcomp 函数执行 Posthoc 测试。

但是,如果我运行

library(multcomp)

posthoc = glht(model,
               linfct = mcp(Treatment="Tukey"))

我收到了这个我不明白的错误信息

Error in model.frame.lme(object) : object does not contain any data
Error in factor_contrasts(model) : 
  no ‘model.matrix’ method for ‘model’ found!

模型中存储有数据:

> model
Linear mixed-effects model fit by REML
  Data: NULL 
  Log-restricted-likelihood: -166.8703
  Fixed: Donation ~ Treatment 
 (Intercept) TreatmentTG1 TreatmentTG2 TreatmentTG3 TreatmentTG4 
   0.7492227    1.3343727    0.2981268    1.4943010    0.5274175 

Random effects:
 Formula: ~1 | Session
        (Intercept) Residual
StdDev:   0.1759392 1.651152

Number of Observations: 88
Number of Groups: 27

变量是:

$ player.cumulative_donation: num  2.5 1.4 0 1 0 0 0.5 0 1 0 ...
$ player.treatmentgroup     : chr  "CG" "CG" "CG" "CG" ...
$ Session code              : chr  "uk4rlbdo" "uk4rlbdo" "uk4rlbdo" "uk4rlbdo" ...

编辑: 创建模型的 R 命令:

library(nlme)

model = lme(Donation ~ Treatment, random=~1|Session,
            method="REML")

anova.lme(model,
          type="sequential",
          adjustSigma = FALSE)

dput的输出(head(SPSS_Data.df,10)):

structure(list(Participant_id = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 
10), participant.id_in_session = c(1, 2, 3, 4, 1, 2, 3, 1, 2, 
3), participant.code = c("hcj5o43a", "ugiv2jlq", "53vepzb7", 
"j2k7noqy", "njm1sr5d", "c2phh8p1", "5xaot5ii", "lvfkfw72", "05pjmgwp", 
"o0yt5qbt"), `Session code` = c("uk4rlbdo", "uk4rlbdo", "uk4rlbdo", 
"uk4rlbdo", "dg0bqvit", "dg0bqvit", "dg0bqvit", "8stn6uxo", "8stn6uxo", 
"8stn6uxo"), player.cumulative_donation = c(2.5, 1.4, 0, 1, 0, 
0, 0.5, 0, 1, 0), player.treatmentgroup = c("CG", "CG", "CG", 
"CG", "CG", "CG", "CG", "CG", "CG", "CG"), TG_coded = c(0, 0, 
0, 0, 0, 0, 0, 0, 0, 0), CG_Dummy = c(1, 1, 1, 1, 1, 1, 1, 1, 
1, 1), TG1_Dummy = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), TG2_Dummy = c(0, 
0, 0, 0, 0, 0, 0, 0, 0, 0), TG3_Dummy = c(0, 0, 0, 0, 0, 0, 0, 
0, 0, 0), TG4_Dummy = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), Gruppe = c(1, 
1, 1, 1, 2, 2, 2, 3, 3, 3), `Perceived Competition` = c(2, 1, 
1, 1, 3, 1, 2, 2, 1, 1), `Influenced behavior` = c(2, 2, 1, 1, 
3, 1, 4, 1, 1, 1), `Donate more` = c(3, 3, 1, 1, 1, 3, 2, 1, 
1, 1), `Donate less` = c(3, 3, 1, 1, 3, 3, 3, 1, 1, 1)), row.names = c(NA, 
10L), class = "data.frame")

【问题讨论】:

  • 嗨 R_Beginner。欢迎来到 StackOverflow!请阅读有关how to ask a good question 的信息以及如何提供minimale reproducible example。这样你就可以帮助别人帮助你!
  • 显示你的 R 命令来创建model
  • 而且,你展示的数据肯定不是你在模型中测试过的。系数不同
  • 感谢您的任何形式的输入,我编辑了我的帖子并包含了真实数据。

标签: r anova posthoc


【解决方案1】:

如果数据是环境中的变量,则回归有效,但对于下游分析,他们要求将其作为 data.frame 存储在 lme 对象中:

例如,这非常有效

library(nlme)
library(multcomp)

SPSS_Data.df = data.frame(
"player.treatmentgroup"=sample(c("TG1","TG2","TG3"),100,replace=TRUE),
"player.cumulative_donation"=rnorm(100),
"Session code" = sample(c("uk4rlbdo","dg0bqvit"),100,replace=TRUE),
check.names=FALSE)

df = setNames(SPSS_Data.df[,c("player.cumulative_donation",
"player.treatmentgroup","Session code")],
              c("Donation","Treatment","Session")
              )

model = lme(Donation ~ Treatment, random=~1|Session,data=df)
glht(model,linfct=mcp(Treatment="Tukey"))

而当您将变量放入环境时,我得到了同样的错误:

Donation = df$Donation
Treatment = df$Treatment
Session =df$Session
model = lme(Donation ~ Treatment, random=~1|Session)
glht(model,linfct=mcp(Treatment="Tukey"))

Error in model.frame.lme(object) : object does not contain any data
Error in factor_contrasts(model) : 
  no ‘model.matrix’ method for ‘model’ found!

【讨论】:

  • 我认为你说的是​​对的!问题是我从 excel 文件中获取数据(大量),所以我直接导入它。但是,我尝试使用SPSS_Data.df <- as.data.frame(SPSS_Data) 将我的数据转换为数据框。我在上面的模型函数中使用了确切的变量名称(player.treatmentgroup 而不是 SPSS_Data$player.treatmentgroup)。但是,我在 mcp 函数中遇到错误:Error in mcp2matrix(model, linfct = linfct) : Variable(s) ‘player.treatmentgroup’ have been specified in ‘linfct’ but cannot be found in ‘model’
  • 嘿,您的列名与模型中的不同,因此它不起作用。要么将 SPSS_Data.df 的列重命名为 Donation、Treatment 等,要么只使用 lme(player.cumulative_donation~player.treatmentgroup, random=~'Session code',data=..)
  • 我似乎还有一些事情没有弄清楚。 1. 我将数据转换为数据框(SPSS_Data.df),2. 我重命名了数据框的列(捐赠、治疗、会话)。 3. 我创建了这样的变量:Treatment
  • 完成,再次感谢您的帮助,非常感谢!
  • 我的错,你是对的。感谢您的帮助,我想我可以自己完成其余的工作!
猜你喜欢
  • 2014-02-06
  • 2021-03-19
  • 2013-10-08
  • 2021-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-12
相关资源
最近更新 更多