单因素方差分析
aov(x~f),x是数值向量,来自与个水平实验结果,f是因子,对应各种水平。
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)
Df Sum Sq Mean Sq F value Pr(>F)
A 3 49212 16404 2.1659 0.1208
Residuals 22 166622 7574
如果数据来自数据框,则用参数data指明数据来源即可。如下:
lamp<-data.frame(x,A)
lamp.aov<-aov(x ~ A, data=lamp)
summary(lamp.aov)
双因素方差分析
如右表,对其做方差分析,判断A和B因素对实验的结果影响。
| B1 | B2 | B3 |
A1 | 325 | 292 | 316 |
A2 | 317 | 310 | 318 |
A3 | 310 | 320 | 318 |
A4 | 330 | 370 | 365 |
x=c(325, 292, 316, 317, 310, 318,310, 320, 318, 330, 370, 365)
A=gl(4,3)
B=gl(3,1,12)
result.aov<-aov(x~A+B)
summary(result.aov)
Df Sum Sq Mean Sq F value Pr(>F)
A 3 3824.2 1274.7 5.2262 0.04126 *
B 2 162.5 81.2 0.3331 0.72915
Residuals 6 1463.5 243.9
如果数据来自数据框,如下:
y<-data.frame(x,A,B)
summary(aov(x~A+B,data=y))
| B1 | B2 | B3 | B4 |
A1 | 23 25 21 14 15 | 20 17 11 26 21 | 16 19 13 16 24 | 20 21 18 27 24 |
A2 | 28 30 19 17 22 | 26 24 21 25 26 | 19 18 19 20 25 | 26 26 28 29 23 |
A3 | 18 15 23 18 10 | 21 25 12 12 22 | 19 23 22 14 13 | 22 13 12 22 19 |
有交互作用的方差分析
如右表所示,检验是否有交互作用的存在。
x=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)
result.aov<-aov(x~A+B+A:B)
summary(result.aov)
Df Sum Sq Mean Sq F value Pr(>F)
A 2 352.53 176.27 8.9589 0.000494 ***
B 3 87.52 29.17 1.4827 0.231077
A:B 6 71.73 11.96 0.6077 0.722890
Residuals 48 944.40 19.67
如果数据来自数据框,如下:
y<-data.frame(x,A,B)
summary(aov(x~A+B+A*B,data=y))
其实论坛有很多资源的
你多看看就好了