# R in action读书笔记（12）第九章 方差分析

9.2 ANOVA 模型拟合

9.2.1 aov()函数

aov(formula, data = NULL, projections =FALSE, qr = TRUE,

contrasts = NULL, ...)

9.2.2 表达式中各项的顺序

y ~ A + B + A:B

B调整。

9.3 单因素方差分析

> library(multcomp)

> attach(cholesterol)

> table(trt) #各组样本大小

trt

1time 2times4times drugD drugE

10 10 10 10 10

> aggregate(response,by=list(trt),FUN=mean)#各组均值

Group.1 x

1 1time 5.78197

2 2times 9.22497

3 4times 12.37478

4 drugD 15.36117

5 drugE 20.94752

> aggregate(response,by=list(trt),FUN=sd) #各组标准差

Group.1 x

1 1time 2.878113

2 2times 3.483054

3 4times 2.923119

4 drugD 3.454636

5 drugE 3.345003

> fit<-aov(response~trt)

> summary(fit) #检验组间差异（ANOVA）

Df SumSq Mean Sq F value Pr(>F)

trt 41351.4 337.8 32.43 9.82e-13 ***

Residuals 45 468.8 10.4

---

Signif. codes:

0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

> library(gplots)

> plotmeans(response~trt,xlab="treatment",ylab="response",main="meanplot\nwith 95% CI")

#绘制各组均值及其置信区间的图形

> detach(cholesterol)

9.3.1 多重比较

TukeyHSD()函数提供了对各组均值差异的成对检验。但要注意TukeyHSD()函数与HH包存在兼容性问题：若载入HH包，TukeyHSD()函数将会失效。使用detach("package::HH")将它从搜寻路径中删除，然后再调用TukeyHSD()

> TukeyHSD(fit)

Tukey multiplecomparisons of means

95% family-wise confidence level

Fit: aov(formula = response ~ trt)

\$trt

diff lwr upr p adj

2times-1time 3.44300 -0.6582817 7.5442820.1380949

4times-1time 6.59281 2.4915283 10.6940920.0003542

drugD-1time 9.57920 5.4779183 13.680482 0.0000003

drugE-1time 15.16555 11.0642683 19.266832 0.0000000

4times-2times 3.14981 -0.9514717 7.2510920.2050382

drugD-2times 6.13620 2.0349183 10.2374820.0009611

drugE-2times 11.72255 7.6212683 15.8238320.0000000

drugD-4times 2.98639 -1.1148917 7.0876720.2512446

drugE-4times 8.57274 4.4714583 12.6740220.0000037

drugE-drugD 5.58635 1.4850683 9.687632 0.0030633

> par(las=2)

> par(mar=c(5,8,4,2))

> plot(TukeyHSD(fit))

multcomp包中的glht()函数提供了多重均值比较更为全面的方法，既适用于线性模型，也适用于广义线性模型.

> library(multcomp)

> par(mar=c(5,4,6,2))

> tuk<-glht(fit,linfct=mcp(trt="Tukey"))

> plot(cld(tuk,level=.5),col="lightgrey")

9.3.2 评估检验的假设条件

> library(car)

> qqPlot(lm(response~trt,data=cholesterol),simulate=TRUE,main="Q-Qplot",labels=FALSE)

Bartlett检验:

> bartlett.test(response~trt,data=cholesterol)

Bartlett test ofhomogeneity of variances

data: response by trt

Bartlett's K-squared = 0.5797, df = 4,

p-value = 0.9653

> library(car)

> outlierTest(fit)

No Studentized residuals withBonferonni p < 0.05

Largest |rstudent|:

rstudent unadjusted p-value Bonferonni p

19 2.251149 0.029422 NA

9.4 单因素协方差分析

> data(litter,package="multcomp")

> attach(litter)

> table(dose)

dose

0 5 50 500

20 19 18 17

> aggregate(weight,by=list(dose),FUN=mean)

Group.1 x

1 0 32.30850

2 5 29.30842

3 50 29.86611

4 500 29.64647

> fit<-aov(weight~gesttime+dose)

> summary(fit)

Df Sum Sq Mean Sq Fvalue Pr(>F)

gesttime 1 134.3 134.30 8.049 0.00597 **

dose 3 137.1 45.71 2.739 0.04988 *

Residuals 69 1151.3 16.69

---

Signif. codes:

0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’0.1 ‘ ’ 1

> library(effects)

> effect("dose",fit)

dose effect

dose

0 5 50 500

32.35367 28.87672 30.56614 29.33460

> library(multcomp)

> contrast<-rbind("no drugvs. drug"=c(3,-1,-1,-1))

> summary(glht(fit,linfct=mcp(dose=contrast)))

Simultaneous Tests for General LinearHypotheses

Multiple Comparisons of Means: User-definedContrasts

Fit: aov(formula = weight ~ gesttime +dose)

Linear Hypotheses:

Estimate Std. Error tvalue

no drug vs. drug == 0 8.284 3.209 2.581

Pr(>|t|)

no drug vs. drug == 0 0.012 *

---

Signif. codes:

0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’0.1 ‘ ’ 1

(Adjusted p values reported --single-step method)

9.4.1 评估检验的假设条件

> library(multcomp)

> fit2<-aov(weight~gesttime*dose,data=litter)

> summary(fit2)

Df Sum Sq Mean Sq F value Pr(>F)

gesttime 1 134.3 134.30 8.289 0.00537 **

dose 3 137.1 45.71 2.821 0.04556 *

gesttime:dose 3 81.9 27.29 1.684 0.17889

Residuals 66 1069.4 16.20

---

Signif. codes:

0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’0.1 ‘ ’ 1

9.4.2 结果可视化

HH包中的ancova()函数可以绘制因变量、协变量和因子之间的关系图。

> library(HH)

> ancova(weight~gesttime+dose,data=litter)

Analysis of Variance Table

Response: weight

Df Sum Sq Mean Sq F value Pr(>F)

gesttime 1 134.30 134.304 8.0493 0.005971 **

dose 3 137.12 45.708 2.7394 0.049883 *

Residuals 69 1151.27 16.685

---

Signif. codes:

0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’0.1 ‘ ’ 1

9.5 双因素方差分析

> 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

> fit<-aov(len~supp*dose)

> summary(fit)

Df Sum Sq Mean Sq F value Pr(>F)

supp 1 205.3 205.3 12.317 0.000894 ***

dose 1 2224.3 2224.3 133.415 < 2e-16 ***

supp:dose 1 88.9 88.9 5.333 0.024631 *

Residuals 56 933.6 16.7

---

Signif. codes:

0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’0.1 ‘ ’ 1

table语句的预处理表明该设计是均衡设计（各设计单元中样本大小都相同），aggregate

（supp和dose）和交互效应都非常显著。有多种方式对结果进行可视化处理。interaction.plot()函数来展示双因素方差分析的交互效应。

> interaction.plot(dose,supp,len,type="b",col=c("red","blue"),pch=c(16,18),main="interactionbetween dose and supplement type")

> library(gplots)

> plotmeans(len~interaction(supp,dose,sep=""),connect=list(c(1,3,5),c(2,4,6)),col=c("red","darkgreen"),main="interactionplot with 95%CIs",xlab="treatment and dose combination")

> library(HH)

> interaction2wt(len~supp*dose)

9.6 重复测量方差分析

w1b1<-subset(CO2,Treatment=="chilled")

w1b1

fit<-aov(uptake~conc*Type+Error(Plant/(conc)),w1b1)

summary(fit)

par(las=2)

par(mar=c(10,4,4,2))

with(w1b1,interaction.plot(conc,Type,uptake,type="b",col=c("red","blue"),pch=c(16,18),main="InteractionPlot for Plant Type and Concentration"))

boxplot(uptake~Type*conc,data=w1b1,col=c("gold","green"),

main="Chilled Quebec andMississippi Plants",

ylab="Carbon dioxide uptake rateumol/m^2 sec")

9.7 多元方差分析

library(MASS)

attach(UScereal)

y<-cbind(calories,fat,sugars)

aggregate(y,by=list(shelf),FUN=mean)

cov(y)

fit<-manova(y~shelf)

summary(fit)

summary.aov(fit)

9.7.1 评估假设检验

> center<-colMeans(y)

> n<-nrow(y)

> p<-ncol(y)

> cov<-cov(y)

> d<-mahalanobis(y,center,cov)

> coord<-qqplot(qchisq(ppoints(n),df=p),d,main="q-qplot assessing multivariate normality",ylab="mahalanobis d2")

> identify(coord\$x,coord\$y,labels=row.names(UScereal))

9.7.2 稳健多元方差分析

> library(rrcov)

> Wilks.test(y,shelf,method="mcd")

9.8 用回归来做ANOVA

> library(multcomp)

> levels(cholesterol\$trt)

[1] "1time" "2times" "4times""drugD"

[5] "drugE"

> fit.aov<-aov(response~trt,data=cholesterol)

> summary(fit.aov)

Df Sum Sq Mean Sq F value Pr(>F)

trt 4 1351.4 337.8 32.43 9.82e-13 ***

Residuals 45 468.8 10.4

---

Signif. codes:

0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’0.1 ‘ ’ 1

> fit.lm<-lm(response~trt,data=cholesterol)

> summary(fit.lm)

Call:

lm(formula = response ~ trt, data =cholesterol)

Residuals:

Min 1Q Median 3Q Max

-6.5418 -1.9672 -0.0016 1.8901 6.6008

Coefficients:

Estimate Std. Error t valuePr(>|t|)

(Intercept) 5.782 1.021 5.665 9.78e-07 ***

trt2times 3.443 1.443 2.385 0.0213 *

trt4times 6.593 1.443 4.568 3.82e-05 ***

trtdrugD 9.579 1.443 6.637 3.53e-08 ***

trtdrugE 15.166 1.443 10.507 1.08e-13 ***

---

Signif. codes:

0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’0.1 ‘ ’ 1

Residual standard error: 3.227 on 45degrees of freedom

Multiple R-squared: 0.7425, AdjustedR-squared: 0.7196

F-statistic: 32.43 on 4 and 45DF, p-value: 9.819e-13

Top