單因素方差分析(aov)-R版本

R版本的方差分析

#做方差分析有三個假設,需要提前進行檢驗。1.每個處理效應和隨機誤差是可加的。2.正態獨立性,檢驗誤差應該是正態分佈的。3.方差齊次性。水平間的方差應該相等。


數據導入

wheat<- data.frame(
+ x=c(5.4,5.3,6.1,5.1,5.6,5.1,4.5,6,6.6,6.5,5.8,6.2,5.6), a=factor(c(rep(1,3),rep(2,4),rep(3,3),rep(4,3)))) #rep:重複產生數據


正態分佈檢驗

shapiro.test(wheatx[wheat a==1])

Shapiro-Wilk normality test

data: wheatx[wheat a == 1]
W = 0.84211, p-value = 0.2196

shapiro.test(wheatx[wheat a==2])

Shapiro-Wilk normality test

data: wheatx[wheat a == 2]
W = 0.94078, p-value = 0.6591

shapiro.test(wheatx[wheat a==3])

Shapiro-Wilk normality test

data: wheatx[wheat a == 3]
W = 0.87097, p-value = 0.2983

shapiro.test(wheatx[wheat a==4])

Shapiro-Wilk normality test

data: wheatx[wheat a == 4]
W = 0.96429, p-value = 0.6369

正態檢驗,4組數據的p值都大於0.05 都不能拒絕原假設,每組數據是正態的,所以滿足方差分析的一個條件,接下來做方差齊性檢驗


原假設這個原假設H0: 各個水平是等方差的

bartlett.test(wheatx,wheat a)#方差齊性檢驗,

Bartlett test of homogeneity of variances

data: wheatxandwheat a
Bartlett’s K-squared = 0.44019, df = 3, p-value = 0.9318

p>0.05,不能拒絕原假設,具有方差齊性,可以進行方差分析。


方差分析

wheat.aov <- aov(x~a, data=wheat)
summary(wheat.aov)
Df Sum Sq Mean Sq F value Pr(>F)
a 3 3.002 1.0007 6.523 0.0123 *
Residuals 9 1.381 0.1534

Signif. codes: 0 ‘’ 0.001 ‘’ 0.01 ‘’ 0.05 ‘.’ 0.1 ‘ ’ 1

p<0.05 ,拒絕原假設,認爲濃度對wheat有影響。


R代碼

wheat<- data.frame(  
    x=c(5.4,5.3,6.1,5.1,5.6,5.1,4.5,6,6.6,6.5,5.8,6.2,5.6), a=factor(c(rep(1,3),rep(2,4),rep(3,3),rep(4,3))))   


#正態分佈檢驗
shapiro.test(wheat$x[wheat$a==1])
shapiro.test(wheat$x[wheat$a==2])
shapiro.test(wheat$x[wheat$a==3])
shapiro.test(wheat$x[wheat$a==4])
#正態檢驗,4組數據的p值都大於0.05 都不能拒絕原假設,每組數據是正態的,所以滿足方差分析的一個條件,接下來做方差齊性檢驗     


#原假設這個原假設H0: 各個水平是等方差的
bartlett.test(wheat$x, wheat$a)#方差齊性檢驗,
#p>0.05,不能拒絕原假設,具有方差齊性,可以進行方差分析。

wheat.aov <- aov(x~a, data=wheat)#方差分析
summary(wheat.aov) 
#p<0.05 拒絕原假設,認爲濃度對wheat有影響。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章