R语言—方差分析(4)
>attach(ToothGrowth)
>table(supp,dose)
dose
supp 0.5 1 2
OJ 10 10 10
VC 10 10 10
>aggregate(len,by=list(supp,dose),FUN=mean)
Group.1 Group.2 x
1 OJ 0.5 13.23
2 VC 0.5 7.98
3 OJ 1.0 22.70
4 VC 1.0 16.77
5 OJ 2.0 26.06
6 VC 2.0 26.14
>aggregate(len,by=list(supp,dose),FUN=sd)
Group.1 Group.2 x
1 OJ 0.5 4.459709
2 VC 0.5 2.746634
3 OJ 1.0 3.910953
4 VC 1.0 2.515309
5 OJ 2.0 2.655058
6 VC 2.0 4.797731
>dose<-factor(dose)
>fit<-aov(len~supp*dose)
>summary(fit)
Df Sum Sq Mean Sq F value Pr(>F)
supp 1 205.4 205.4 15.572 0.000231 ***
dose 2 2426.4 1213.2 92.000 < 2e-16 ***
supp:dose 2 108.3 54.2 4.107 0.021860 *
Residuals 54 712.1 13.2
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#可以看到主效应和交互效应都非常的显著
-
结果可视化
>interaction.plot(dose,supp,len,type = "b",
col =c( "red","blue"),pch = c(16,18),
main="Interaction between Dose and Supplement Type")
>plotmeans(len~interaction(supp,dose,sep = ""),
connect = list(c(1,3,5),c(2,4,6)),
col = c("gold","darkgreen"),main="Interaction Plot with 95% CIs",
xlab = "Treatment and Dose Combination")
(3)使用HH包中的interaction2wt()函数
>library(HH)
> interaction2wt(len~supp*dose)
以上三幅图均表明随着橙汁和维生素C中的抗坏血酸剂量的增加,牙齿长度变长。对于0.5mg和1mg剂量,橙汁比维生素C更能促进牙齿生长;对于2mg 剂量的抗坏血酸,两种喂食方法下牙齿长度增长相同。
扫描二维码
获取更多精彩
大康的笔记