示例數(shù)據(jù)集如下:
導(dǎo)入數(shù)據(jù)作簡單可視化
library(tidyverse)
test <- rio::import('ANOVA.xlsx')
boxplot(value ~ group,data = test)

正態(tài)性檢驗
- 當分析小于50行的小樣本數(shù)據(jù)時,用Shapiro-Wilk 檢驗
- 當分析大于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ù)不等
多重比較
- 事前比較的常用方法有LSD法、Dunnett-t檢驗
- 事后比較的常用方法有SNK法、Turkey法、Scheffe法
- 事前比較和事后比較都可以采用的方法有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)
