方差分析在试验科学中有重要的地位,今天谈谈如何用R做方差分析
前提假设:独立、正态、方差齐次(各水平间)
例子:x
数据集用5个因子水平测量,问是否存在差异
光是这样是无法进行分析的,对数据x进行格式转化
b
x a
1 25.6 1
2 22.2 1
3 28.0 1
4 29.8 1
5 24.4 2
6 30.0 2
7 29.0 2
8 27.5 2
9 25.0 3
10 27.7 3
11 23.0 3
12 32.2 3
13 28.8 4
14 28.0 4
15 31.5 4
16 25.9 4
17 20.6 5
18 21.2 5
19 22.0 5
20 21.2 5
在进行方差分析之前先对几条假设进行检验,由于随机抽取,假设总体满足独立、正态,考察方差齐次性(用bartlett检验)
> bartlett.test(x~a,data=b)
Bartlett test of homogeneity of variances
data: x by a
Bartlett's K-squared = 7.0966, df = 4, p-value = 0.1309
方差齐次性符合,因此选用方差分析aov()或者KW检验kruskal.test()均可(。
下面进行方差分析
m1
summary(m1)
Df Sum Sq Mean Sq F value
Pr(>F) a 4 132.0 32.99 4.306 0.0162 *
Residuals 15 114.9 7.66 ---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05
‘.’ 0.1 ‘ ’ 1
从这个结果看出差别显著
接下来考察具体的差异(多重比较),
> TukeyHSD(m1)
Tukey multiple comparisons of means
95%
family-wise confidence level
Fit: aov(formula = x ~ a, data = b)
$a
diff lwr upr p adj
2-1 1.325 -4.718582 7.3685818 0.9584566
3-1 0.575 -5.468582 6.6185818 0.9981815
4-1 2.150 -3.893582 8.1935818 0.8046644
5-1 -5.150 -11.193582 0.8935818 0.1140537
3-2 -0.750 -6.793582 5.293581