R语言方差分析的注意事项( 二 )

< 2.2e-16 ***## trt32.16324.884 1.674e-12 ***## Residuals49.97 116## ---## Signif. codes:0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
可以看到3种类型的结果是完全一样的 , 没有差别~
下面我们更改一下样本数量 , 使其变成非均衡设计(还是one-way anova):
# 每组样本数量改一下trt<-c(rep("group1",10),rep("group2",50),rep("group3",35),rep("group4",25))weight<-c(3.53,4.59,4.34,2.66,3.59,3.13,3.30,4.04,3.53,3.56,3.85,4.07,1.37,3.93,2.33,2.98,4.00,3.55,2.64,2.56,3.50,3.25,2.96,4.30,3.52,3.93,4.19,2.96,4.16,2.59,2.42,3.36,4.32,2.34,2.68,2.95,2.36,2.56,2.52,2.27,2.98,3.72,2.65,2.22,2.90,1.98,2.63,2.86,2.93,2.17,2.72,1.56,3.11,1.81,1.77,2.80,3.57,2.97,4.02,2.31,2.86,2.28,2.39,2.28,2.48,2.28,3.48,2.42,2.41,2.66,3.29,2.70,2.66,3.68,2.65,2.66,2.32,2.61,3.64,2.58,3.65,3.21,2.23,2.32,2.68,3.04,2.81,3.02,1.97,1.68,0.89,1.06,1.08,1.27,1.63,1.89,1.31,2.51,1.88,1.41,3.19,1.92,0.94,2.11,2.81,1.98,1.74,2.16,3.37,2.97,1.69,1.19,2.17,2.28,1.72,2.47,1.02,2.52,2.10,3.71)df1 <- data.frame(trt,weight)
然后再来看一下3种类型方差分析的结果:
# 1型fit1 <- aov(weight ~ trt, data = http://www.kingceram.com/post/df1)summary(fit1)##Df Sum Sq Mean Sq F valuePr(>F)## trt322.037.34314.17 6.23e-08 ***## Residuals11660.090.518## ---## Signif. codes:0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1# 2型car::Anova(fit1, type = 2)## Anova Table (Type II tests)## ## Response: weight##Sum SqDf F valuePr(>F)## trt22.029314.174 6.228e-08 ***## Residuals 60.094 116## ---## Signif. codes:0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1# 3型car::Anova(fit1, type = 3)## Anova Table (Type III tests)## ## Response: weight##Sum SqDf F valuePr(>F)## (Intercept) 131.5511 253.933 < 2.2e-16 ***## trt22.029314.174 6.228e-08 ***## Residuals60.094 116## ---## Signif. codes:0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
可以看到3种类型的结果也是一样的哦!
two-way anova
使用一个随机区组设计的方差分析进行演示 , 示例数据来自课本例4-3的数据 。
首先也是均衡设计的情况:

R语言方差分析的注意事项

文章插图
weight <- c(0.82,0.65,0.51,0.73,0.54,0.23,0.43,0.34,0.28,0.41,0.21,0.31,0.68,0.43,0.24)block <- c(rep(c("1","2","3","4","5"),each=3))group <- c(rep(c("A","B","C"),5))data4_4 <- data.frame(weight,block,group)str(data4_4)## 'data.frame': 15 obs. of3 variables:##$ weight: num0.82 0.65 0.51 0.73 0.54 0.23 0.43 0.34 0.28 0.41 ...##$ block : chr"1" "1" "1" "2" ...##$ group : chr"A" "B" "C" "A" ...
下面是3种类型方差分析的结果 , 由于是均衡设计 , 3种类型没有任何区别 , 并且即使你把区组因素和分组因素的位置互换 , 也不会有任何差别哦!
# 1型fit <- aov(weight ~ block + group, data = http://www.kingceram.com/post/data4_4)summary(fit)##Df Sum Sq Mean Sq F valuePr(>F)## block4 0.2284 0.057095.978 0.01579 * ## group2 0.2280 0.1140011.937 0.00397 **## Residuals8 0.0764 0.00955## ---## Signif. codes:0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1# 2型car::Anova(fit, type = 2)## Anova Table (Type II tests)## ## Response: weight##Sum Sq Df F valuePr(>F)## block0.2283645.978 0.015787 * ## group0.22800211.937 0.003968 **## Residuals 0.076408## ---## Signif. codes:0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1# 3型car::Anova(fit, type = 3)## Anova Table (Type III tests)## ## Response: weight##Sum Sq Df F valuePr(>F)## (Intercept) 1.440861 150.875 1.794e-06 ***## block0.2283645.9780.015787 *## group0.22800211.9370.003968 ** ## Residuals0.076408## ---## Signif. codes:0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1