[R語言] 單因素方差分析和多重比較

示例數(shù)據(jù)集如下:


導(dǎo)入數(shù)據(jù)作簡單可視化

library(tidyverse)
test <- rio::import('ANOVA.xlsx')
boxplot(value ~ group,data = test)

正態(tài)性檢驗

  1. 當分析小于50行的小樣本數(shù)據(jù)時,用Shapiro-Wilk 檢驗
  2. 當分析大于50行的大樣本數(shù)據(jù)時,用Kolmogorov-Smirnov檢驗
by(test, test$group, function(df) shapiro.test(df[,2]))

各組符合正態(tài)分布

方差齊性檢驗

bartlett.test(value ~ group,data = test)

各組方差齊

單因素方差分析

ANOVA <- aov(value ~ group,data = test)
summary(ANOVA)

多組之間存在兩組,總體均數(shù)不等

多重比較

  1. 事前比較的常用方法有LSD法、Dunnett-t檢驗
  2. 事后比較的常用方法有SNK法、Turkey法、Scheffe法
  3. 事前比較和事后比較都可以采用的方法有Bonferroni法、Sidak法

LSD法

p.adj=”none” 時,為LSD法,p.adj="bonferroni" 時為Bonferroni法

# install.packages("agricolae")
library(agricolae)
res <- LSD.test(ANOVA, 'group', p.adj = 'none')
res
plot(res)

可見顯著性字母標識


TukeyHSD法

tuk <- TukeyHSD(ANOVA)
tuk
plot(tuk)

完整代碼

library(tidyverse)
test <- rio::import('ANOVA.xlsx')
boxplot(value ~ group,data = test)

# 正態(tài)性檢驗
by(test, test$group, function(df) shapiro.test(df[,2]))

# 方差齊性檢驗
bartlett.test(value ~ group,data = test)

# 單因素方差分析
ANOVA <- aov(value ~ group,data = test)
summary(ANOVA)

# 多重比較
# 1. LSD法
# install.packages("agricolae")
library(agricolae)
res <- LSD.test(ANOVA, 'group', p.adj = 'none')
res
plot(res)
# 2. TukeyHSD法
tuk <- TukeyHSD(ANOVA)
tuk
plot(tuk)
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容