R學習-方差分析


單因素方差分析
題解 
單因素四水平試驗
四種不同配方材料A1,A2,A3,A4生產出的原件,壽命如下表,問:四種不同配方下原件的使用壽命有無顯著差異
lamp<-data.frame(
X=c(1600, 1610, 1650, 1680, 1700, 1700, 1780, 1500, 1640,
1400, 1700, 1750, 1640, 1550, 1600, 1620, 1640, 1600,
1740, 1800, 1510, 1520, 1530, 1570, 1640, 1600),
A=factor(c(rep(1,7),rep(2,5), rep(3,8), rep(4,6)))
)
lamp.aov<-aov(X ~ A, data=lamp)
summary(lamp.aov)
例 小白鼠在接種了3種不同菌型的傷寒桿菌後的存活天數如表所示,判斷小白鼠被注射三種菌型後的平均存活天數有無顯著差異
mouse<-data.frame(
X=c( 2, 4, 3, 2, 4, 7, 7, 2, 2, 5, 4, 5, 6, 8, 5, 10, 7,
12, 12, 6, 6, 7, 11, 6, 6, 7, 9, 5, 5, 10, 6, 3, 10),
A=factor(c(rep(1,11),rep(2,10), rep(3,12)))
)
mouse.aov<-aov(X ~ A, data=mouse)
7.1.4 均值的多重比較
多重t檢驗:缺點,如果因素的水平較多,多次重複使用t檢驗會增加犯第一類錯誤的概率
P值的修正 
p.adjust()
調整方法有
Bonferroni "bonferroni"
Holm (1979) "holm"
Hochberg (1988) "hochberg"
Hommel (1988) "hommel"
Benjamini & Hochberg (1995) "BH"
Benjamini & Yekutieli (2001) "BY"
均值的多重比較
pairwise.t.test()
attach(mouse)
mu<-c(mean(X[A==1]), mean(X[A==2]), mean(X[A==3])); mu
pairwise.t.test(X, A, p.adjust.method = "none")
調整後的p值
pairwise.t.test(X, A, p.adjust.method = "holm")
pairwise.t.test(X, A, p.adjust.method = "bonferroni")
plot(mouse$X~mouse$A)
方差的齊次檢驗
方差分析的三個條件
1 可加性 假設模型是線性可加模型,每個處理效應與隨機誤差是可以疊加的
2 獨立正態性 試驗誤差應當服從正態分佈,而其相互獨立
3 方差齊性,不同處理間的方差是一致的。
誤差的正態性檢驗 shapiro.test()
attach(lamp)
shapiro.test(X[A==1])
shapiro.test(X[A==2])
shapiro.test(X[A==3])
shapiro.test(X[A==4])
方差齊性檢驗 Baerlett檢驗
bartlett.test()
bartlett.test(X~A, data=lamp)
bartlett.test(lamp$X, lamp$A)
對兩個以上樣本進行比較的Kruskal-Wallis 秩和檢驗
kruskal.test()
kruskal.test(formula, data, subset, na.action, ...)
例子 爲了比較屬於同一類的四種不同食譜的營養效果,將25只老鼠隨機的氛圍4組,每組分別爲8只,4只,7只和6只,採用食譜甲乙丙丁餵養,假設其他條件均值相同,12周後測得體重增加量如表所示,在alpha=0.05水平上,檢驗各食譜的營養效果是否有顯著差異。
food<-data.frame(
x=c(164, 190, 203, 205, 206, 214, 228, 257,
185, 197, 201, 231,
187, 212, 215, 220, 248, 265, 281,
202, 204, 207, 227, 230, 276),
g=factor(rep(1:4, c(8,4,7,6)))
)
kruskal.test(x~g, data=food)
另一種寫法
kruskal.test(food$x, food$g)

A<-c(164, 190, 203, 205, 206, 214, 228, 257)
B<-c(185, 197, 201, 231)
C<-c(187, 212, 215, 220, 248, 265, 281)
D<-c(202, 204, 207, 227, 230, 276)
kruskal.test(list(A,B,C,D))
方差分析的過程
1 正態性檢驗
attach(food)
shapiro.test(x[g==1])
shapiro.test(x[g==2])
shapiro.test(x[g==3])
shapiro.test(x[g==4])
2 方差齊性檢驗
bartlett.test(x~g, data=food)
aovtable<-aov(x~g, data=food)
summary(aovtable)
Friedman秩和檢驗
在配伍組設計中,如果總體不能滿足正態性和方差齊性的要求,可以採用Friedman秩和檢驗
friedman.test()
friedman.test(y, groups, blocks, ...)
friedman.test(formula, data, subset, na.action, ...)
24只小鼠按不同窩氛圍八個區組,再把每個區組中的觀察單位隨機分配到3種不同的飼料組,餵養一定時間之後,測得小鼠肝中的鐵含量,結果如表,所示,試分析不同飼料的小鼠肝中鐵含量是否不同。
X<-matrix(
c(1.00, 1.01, 1.13, 1.14, 1.70, 2.01, 2.23, 2.63,
0.96, 1.23, 1.54, 1.96, 2.94, 3.68, 5.59, 6.96,
2.07, 3.72, 4.50, 4.90, 6.00, 6.84, 8.23, 10.33),
ncol=3, dimnames=list(1:8, c("A", "B", "C"))
)
friedman.test(X)
雙因素方差分析
在一個農業試驗中,考慮四種不同的品種A1,A2,A3,A4和三種不同的施肥方法B1,B2,B3得到的產量數據表。分析種子與施肥對產量有無顯著影響。
1 不考慮交互作用時
agriculture<-data.frame(
Y=c(325, 292, 316, 317, 310, 318,
310, 320, 318, 330, 370, 365),
A=gl(4,3),
B=gl(3,1,12)
)
agriculture.aov <- aov(Y ~ A+B, data=agriculture)
summary(agriculture.aov)
考慮交互作用
分析樹種與地理位置對松樹生長的影響。A表示樹種,B表示地區,對同一水平組合進行了五次測量,對結果進行方差分析。
tree<-data.frame(
Y=c(23, 25, 21, 14, 15, 20, 17, 11, 26, 21,
16, 19, 13, 16, 24, 20, 21, 18, 27, 24,
28, 30, 19, 17, 22, 26, 24, 21, 25, 26,
19, 18, 19, 20, 25, 26, 26, 28, 29, 23,
18, 15, 23, 18, 10, 21, 25, 12, 12, 22,
19, 23, 22, 14, 13, 22, 13, 12, 22, 19),
A=gl(3,20,60),
B=gl(4,5,60)
)
tree.aov <- aov(Y ~ A+B+A:B, data=tree)
summary(tree.aov)
雙因素方差分析的方差齊性檢驗
attach(tree)
正態性檢驗
shapiro.test(Y[A==1])
shapiro.test(Y[A==2])
shapiro.test(Y[A==3])
shapiro.test(Y)
shapiro.test(Y)
shapiro.test(Y)
shapiro.test(Y)
方差齊性檢驗
bartlett.test(Y~A, data=tree)
正交試驗設計與方差分析
正交試驗設計分析
爲提高某種化學產品的轉化率,考慮三個有關因素,反應溫度A,反應時間B,和用鹼量C,各因素選三個水平,
rate<-data.frame(
A=gl(3,3),
B=gl(3,1,9),
C=factor(c(1,2,3,2,3,1,3,1,2)),
Y=c(31, 54, 38, 53, 49, 42, 57, 62, 64)
)
K<-matrix(0, nrow=3, ncol=3, dimnames=list(1:3, c("A","B","C")))
> for (j in 1:3)
for (i in 1:3)
K<-mean(rate$Y[rate[j]==i])
K
plot(as.vector(K), axes=F, xlab="Level", ylab="Rate")
xmark<-c(NA,"A1","A2","A3","B1","B2","B3","C1","C2","C3",NA)
axis(1,0:10,labels=xmark)
axis(2,4*10:16)
axis(3,0:10,labels=xmark)
axis(4,4*10:16)
lines(K[,"A"]); lines(4:6, K[,"B"]); lines(7:9,K[,"C"])
正交試驗的方差分析
假設沒有交互作用
rate.aov<-aov(Y~A+B+C, data=rate)
summary(rate.aov)
有交互作用的實驗
在梳棉機上紡紗,爲了提高質量,選了三個因素,每個因素兩個水平,三個因素之間可能有交互作用,設計實驗。並做方差分析
cotton<-data.frame(
Y=c(0.30, 0.35, 0.20, 0.30, 0.15, 0.50, 0.15, 0.40),
A=gl(2,4), B=gl(2,2,8), C=gl(2,1,8)
)
cotton.aov<-aov(Y~A+B+C+A:B+A:C+B:C, data=cotton)
summary(cotton.aov)
結果表明,A,A:B, B:C的F值都很小,P值很大,可將這三個因素去掉。
cotton.new<-aov(Y~B+C+A:C, data=cotton)
研究四種藥物對淡色庫蚊的殺滅作用,每種藥物取三水平,採用L9(34)正交表,不考慮交互作用,相同實驗條件下均做四次實驗,檢驗四種藥物對淡色庫蚊殺滅作用有無差別。
mosquito<-data.frame(
A=gl(3, 12), B=gl(3,4,36),
C=factor(rep(c(1,2,3,2,3,1,3,1,2),rep(4,9))),
D=factor(rep(c(1,2,3,3,1,2,2,3,1),rep(4,9))),
Y=c( 9.41, 7.19, 10.73, 3.73, 11.91, 11.85, 11.00, 11.72,
10.67, 10.70, 10.91, 10.18, 3.87, 3.18, 3.80, 4.85,
4.20, 5.72, 4.58, 3.71, 4.29, 3.89, 3.88, 4.71,
7.62, 7.01, 6.83, 7.41, 7.79, 7.38, 7.56, 6.28,
8.09, 8.17, 8.14, 7.49)
)
mosquito.aov<-aov(Y~A+B+C+D, data=mosquito)
source("anova.tab.R"); anova.tab(mosquito.aov) 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章