【问题标题】:R: One Way Anova and pairwise post hoc tests (Turkey, Scheffe or other) for numerical columnsR:数值列的单向方差分析和成对事后检验(Turkey、Scheffe 或其他)
【发布时间】:2017-03-21 23:23:16
【问题描述】:

我在数据框 dune(页面底部下方) 中有三列,描述了三种不同沙丘生态系统记录的马拉姆草覆盖率:

(1) 恢复; (2) 退化的;和 (3) 自然的;

我执行了两种不同的单向方差分析测试(如下)——测试 1 和测试 2——以确定生态系统之间的显着差异。测试 1 清楚地显示了生态系统之间的显着差异;但是,测试 2 显示没有显着差异。箱线图(下图)显示了生态系统之间差异的明显差异。

之后,我将数据框融合以生成一个阶乘列(即,以 Ecosystem.Type 为标题),它也是响应变量。这个想法是应用 glm 模型(下面的测试 3)来使用 One Way Anova 进行测试;但是,此方法不成功(请在下面查找错误消息)。

问题

我很困惑我执行每个 One Way Anova 测试的代码是否正确,以及执行事后测试(土耳其 HSD、Scheffe 或其他)以区分显着不同的生态系统对的正确程序。如果有人可以提供帮助,我将非常感谢您的建议。非常感谢....

data(dune)

测试 1

dune.type.1<-aov(Natural~Restored+Degraded, data=dune)
summary.aov(dune.type.1, intercept=T)

               Df Sum Sq Mean Sq F value   Pr(>F)    
     (Intercept)  1  34694   34694 138.679 1.34e-09 ***
     Restored     1     94      94   0.375    0.548    
     Degraded     1    486     486   1.942    0.181    
     Residuals   17   4253     250                     
           ---
  Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

事后测试

    posthoc<-TukeyHSD(dune.type.1, conf.level=0.95)

    Error in TukeyHSD.aov(dune.type.1, conf.level = 0.95) : 

    no factors in the fitted model

    In addition: Warning messages:
    1: In replications(paste("~", xx), data = mf) :
       non-factors ignored: Restored
    2: In replications(paste("~", xx), data = mf) :
       non-factors ignored: Degraded

测试 2

     dune1<-aov(Restored~Natural, data=dune)
     dune2<-aov(Restored~Degraded, data=dune)
     dune3<-aov(Degraded~Natural, data=dune)

     summary(dune1)

                 Df Sum Sq Mean Sq F value Pr(>F)
     Natural      1     86   85.58   0.356  0.558
     Residuals   18   4325  240.26               

    summary(dune2)

                 Df Sum Sq Mean Sq F value Pr(>F)
     Degraded     1    160   159.7   0.676  0.422
     Residuals   18   4250   236.1               

     summary(dune3)

                 Df Sum Sq Mean Sq F value Pr(>F)
     Natural      1  168.5  168.49   2.318  0.145
     Residuals   18 1308.5   72.69   

测试 3

melt.dune<-melt(dune, measure.vars=c("Degraded", "Restored", "Natural"))


colnames(melt.dune)=c("Ecosystem.Type", "Percentage.cover")
melt.dune$Percentage.cover<-as.numeric(melt.dune$Percentage.cover)

glm.dune<-glm(Ecosystem.Type~Percentage.cover, data=melt.dune)
summary(glm.dune)

Error

glm.dune<-glm(Ecosystem.Type~Percentage.cover, data=melt.dune)
Error in glm.fit(x = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  : 
NA/NaN/Inf in 'y'
In addition: Warning messages:
1: In Ops.factor(y, mu) : ‘-’ not meaningful for factors
2: In Ops.factor(eta, offset) : ‘-’ not meaningful for factors
3: In Ops.factor(y, mu) : ‘-’ not meaningful for factors

融化的数据框

structure(list(Ecosystem.Type = structure(c(1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("Degraded", "Restored", 
"Natural"), class = "factor"), Percentage.cover = c(12, 17, 21, 
11, 22, 16, 7, 9, 14, 2, 3, 15, 23, 4, 19, 36, 26, 4, 15, 23, 
38, 46, 65, 35, 54, 29, 48, 13, 19, 33, 37, 55, 11, 53, 13, 24, 
28, 44, 42, 39, 18, 61, 31, 46, 51, 51, 41, 44, 55, 47, 73, 43, 
25, 42, 21, 13, 65, 30, 47, 29)), row.names = c(NA, -60L), .Names =         c("Ecosystem.Type", 
 "Percentage.cover"), class = "data.frame")

数据

 structure(list(Degraded = c(12L, 17L, 21L, 11L, 22L, 16L, 7L, 
 9L, 14L, 2L, 3L, 15L, 23L, 4L, 19L, 36L, 26L, 4L, 15L, 23L), 
 Restored = c(38L, 46L, 65L, 35L, 54L, 29L, 48L, 13L, 19L, 
 33L, 37L, 55L, 11L, 53L, 13L, 24L, 28L, 44L, 42L, 39L), Natural = c(18L, 
 61L, 31L, 46L, 51L, 51L, 41L, 44L, 55L, 47L, 73L, 43L, 25L, 
 42L, 21L, 13L, 65L, 30L, 47L, 29L)), .Names = c("Degraded", 
 "Restored", "Natural"), class = "data.frame", row.names = c(NA, 
 -20L))

【问题讨论】:

  • 1.) melt 您的 data.frame 以便拥有一个具有 Degraded、Restored、Natural 级别的因子列。这就是这些函数所期望的数据形状。 2.) 作为依赖的百分比违反了aov 的假设。您可能应该使用 GLM。
  • 您好 Roland,感谢您的建议。我融化了数据框(请在上面找到)并生成了一个 glm 模型;但是,我遇到了一条错误消息(也在上面)。您对如何修复错误有任何建议吗?非常感谢您的帮助。

标签: r anova posthoc


【解决方案1】:

我想向您指出几件事。

首先,测试 1 和测试 2 产生相似的结果。唯一的区别是您在测试 1 中选择了截距,因此结果告诉您,如果您拟合线性模型(我将在几分钟内完成),则需要截距。因此,您看到的意义在于您强制拟合的线是否需要截距。尝试对其他结果使用“intercept=T”,我很确定您会得到类似的结果。

您应该注意的第二件事是您尝试拟合的线性模型。 dune.type.1 模型是一个模型,您可以在其中实际看到不同沙丘生态系统之间的相关性。换句话说,您假设自然和恢复之间存在线性关联,并且随着恢复的每单位增加,您对自然有一些增加。如果我理解正确,您想要的是检查平均差异而不是它们的相关性。因此,您可以做两件事:

  1. 数据已准备好执行 t.tests(比较多个类别之间的平均值的测试)。在 R 中很容易做到并且有效,因为所有变量都相当正常。但是,您将遇到多个测试问题(您可能会执行 3 次 t 检验以获得所有平均差),因此需要使用 Bonferroni 校正。

  2. 但我认为您真正想要的是以下内容:

先改造数据

       data <- data.frame(v = c(dune$Degraded, dune$Restored, dune$Natural), 
                   labels = c(rep("Degraded", times = 20), rep("Restored", times = 20), 
                              rep("Natural", times = 20)))

然后拟合一个线性模型

    mod.1 <- lm(v ~ labels, data = data)
    summary(mod.1)
    lm(formula = v ~ labels, data = data)

Residuals:
Min      1Q  Median      3Q     Max 
-28.650 -10.725   0.875   8.050  31.350 

Coefficients:
               Estimate Std. Error t value Pr(>|t|)    
(Intercept)      14.950      3.066   4.875 9.07e-06 ***
labelsNatural    26.700      4.337   6.157 7.95e-08 ***
labelsRestored   21.350      4.337   4.923 7.64e-06 ***

您实际上可以看到基线类别的平均值(即退化的)明显小于自然类别的平均值等。您还可以检查模型假设,看看您的模型是否适合

    qqnorm(residuals(mod.1))
    qqline(residuals(mod.1))

它们的残差相当正常,因此模型很好。您也可以按照您的 anova 方法并拥有:

    anova.model <- aov(v ~ labels, data = data))
    summary(anova.model)

             Df Sum Sq Mean Sq F value   Pr(>F)    
 labels       2   7982    3991   21.22 1.29e-07 ***
 Residuals   57  10720     188  

这表明沙丘生态系统的平均值之间至少存在一个显着差异,并跟进 Tukey 的逐点区间:

    post <- TukeyHSD(aov(v ~ labels, data = data))
    plot(post, ylim = c(0, 4))

已经针对多次测试进行了调整:)

【讨论】:

  • 谢谢Akis,这个解释和代码很棒!对此深表赞赏。我绘制了事后测试的结果,图中只有两个成对的标签是明显的。任何有关如何解决它的建议.........谢谢你,再次感谢你:)
  • 尝试在 plot 函数中使用 ylim。在我的示例中,我使用了 ylim = c(0,4),因此我可以清楚地看到成对差异的所有三个置信区间
猜你喜欢
  • 2013-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-25
  • 2021-09-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多