You are on page 1of 3

One-Way Multiple Comparisons in R

> data <- read.table("c:/temp/redclover.txt", header=T)


> attach(data)
> data
strain
n
1
3DOk1 19.4
2
3DOk1 32.6
(data list omitted)
29 compos 16.9
30 compos 20.8
> # Pairwise t-tests are available in the "stats" package with:
>
> pairwise.t.test(n,strain, p.adjust.method=c("none"))
Pairwise comparisons using t tests with pooled SD
data:

n and strain

3DOk13
3DOk4
3DOk5
3DOk7
compos

3DOk1
2.1e-07
9.4e-07
0.03545
0.00041
9.8e-05

3DOk13
0.53111
4.9e-05
0.00529
0.01943

3DOk4
0.00025
0.02287
0.07378

3DOk5
0.07378
0.02287

3DOk7
0.57945

Pairwise comparisons using t tests with pooled SD


n and strain

3DOk13
3DOk4
3DOk5
3DOk7
compos

3DOk1
3.1e-06
1.4e-05
0.53169
0.00616
0.00148

3DOk13
1.00000
0.00073
0.07935
0.29144

Pairwise comparisons using t tests with pooled SD


data:

n and strain

3DOk13
3DOk4
3DOk5
3DOk7
compos

3DOk1
3.1e-06
7.0e-06
0.04834
0.00103
0.00037

3DOk13
0.56905
0.00024
0.01134
0.03431

3DOk4
0.00074
0.03431
0.08513

3DOk5
0.08513
0.03431

3DOk7
0.57945

P value adjustment method: BH


>
> pairwise.t.test(n,strain, p.adjust.method=c("holm")) # Holm's
stepdown method with Bonferroni
Pairwise comparisons using t tests with pooled SD

P value adjustment method: none


>
> pairwise.t.test(n,strain, p.adjust.method=c("bonferroni"))

data:

>
> pairwise.t.test(n,strain, p.adjust.method=c("BH")) # BenjaminiHochberg

3DOk4
0.00368
0.34310
1.00000

3DOk5
1.00000
0.34310

P value adjustment method: bonferroni

3DOk7
1.00000

data:

n and strain

3DOk13
3DOk4
3DOk5
3DOk7
compos

3DOk1
3.1e-06
1.3e-05
0.17723
0.00411
0.00118

3DOk13
1.00000
0.00063
0.04761
0.15543

3DOk4
0.00270
0.16011
0.29512

3DOk5
0.29512
0.16011

3DOk7
1.00000

P value adjustment method: holm


> # And lots more methods
>
> g <- aov(n~strain) # Note: TukeyHSD does not work with the lm
function
> summary(g)
Df Sum Sq Mean Sq F value
Pr(>F)
strain
5 847.05 169.41 14.370 1.485e-06 ***
Residuals
24 282.93
11.79
--Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
>
> # Obtain a set of Tukey intervals using TukeyHSD from the "stats"
package
> TukeyHSD(g, conf.level=0.95) # The TukeyHSD function is in the
stats package
Tukey multiple comparisons of means
95% family-wise confidence level

Fit: aov(formula = n ~ strain)


$strain
diff
lwr
upr
3DOk13-3DOk1 -15.56 -22.27416704 -8.845833
3DOk4-3DOk1
-14.18 -20.89416704 -7.465833
3DOk5-3DOk1
-4.84 -11.55416704 1.874167
3DOk7-3DOk1
-8.90 -15.61416704 -2.185833
compos-3DOk1 -10.12 -16.83416704 -3.405833
3DOk4-3DOk13
1.38 -5.33416704 8.094167
3DOk5-3DOk13
10.72
4.00583296 17.434167
3DOk7-3DOk13
6.66 -0.05416704 13.374167
compos-3DOk13
5.44 -1.27416704 12.154167
3DOk5-3DOk4
9.34
2.62583296 16.054167
3DOk7-3DOk4
5.28 -1.43416704 11.994167
compos-3DOk4
4.06 -2.65416704 10.774167
3DOk7-3DOk5
-4.06 -10.77416704 2.654167
compos-3DOk5
-5.28 -11.99416704 1.434167
compos-3DOk7
-1.22 -7.93416704 5.494167
>
> plot(TukeyHSD(g, conf.level=0.95))

p adj
0.0000029
0.0000128
0.2617111
0.0048849
0.0012341
0.9870716
0.0006233
0.0527514
0.1621550
0.0029837
0.1852490
0.4434643
0.4434643
0.1852490
0.9926132

compos-3DOk7

3DOk7-3DOk4

3DOk5-3DOk13

3DOk5-3DOk1

95% family-wise confidence level

> # The general Multivariate T method is available in the


"multcomp" package
> install.packages("multcomp")
>
> library(multcomp)
> levels(strain)
[1] "3DOk1" "3DOk13" "3DOk4" "3DOk5" "3DOk7" "compos"
>
> dht <- glht(g, linfct = mcp(strain = "Tukey"))
> summary(dht) # Gives the adjusted p-values
Simultaneous Tests for General Linear Hypotheses
Multiple Comparisons of Means: Tukey Contrasts
Fit: aov(formula = n ~ strain)
Linear Hypotheses:
Estimate Std. Error t value Pr(>|t|)
3DOk13 - 3DOk1 == 0
-15.560
2.172 -7.166 < 0.001 ***
3DOk4 - 3DOk1 == 0
-14.180
2.172 -6.530 < 0.001 ***
3DOk5 - 3DOk1 == 0
-4.840
2.172 -2.229 0.26164
3DOk7 - 3DOk1 == 0
-8.900
2.172 -4.099 0.00486 **
compos - 3DOk1 == 0
-10.120
2.172 -4.660 0.00121 **
3DOk4 - 3DOk13 == 0
1.380
2.172
0.636 0.98708
3DOk5 - 3DOk13 == 0
10.720
2.172
4.937 < 0.001 ***
3DOk7 - 3DOk13 == 0
6.660
2.172
3.067 0.05285 .
compos - 3DOk13 == 0
5.440
2.172
2.505 0.16220
3DOk5 - 3DOk4 == 0
9.340
2.172
4.301 0.00302 **
3DOk7 - 3DOk4 == 0
5.280
2.172
2.431 0.18517
compos - 3DOk4 == 0
4.060
2.172
1.870 0.44342
3DOk7 - 3DOk5 == 0
-4.060
2.172 -1.870 0.44353
compos - 3DOk5 == 0
-5.280
2.172 -2.431 0.18536
compos - 3DOk7 == 0
-1.220
2.172 -0.562 0.99262
--Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
(Adjusted p values reported -- single-step method)
> confint(dht)

# Gives the simultaneous confidence intervals

Simultaneous Confidence Intervals


Multiple Comparisons of Means: Tukey Contrasts

Fit: aov(formula = n ~ strain)


-20

-10

Differences in mean levels of strain

10

Estimated Quantile = 3.0913


95% family-wise confidence level

Linear Hypotheses:
Estimate
3DOk13 - 3DOk1 == 0 -15.56000
3DOk4 - 3DOk1 == 0
-14.18000
3DOk5 - 3DOk1 == 0
-4.84000
3DOk7 - 3DOk1 == 0
-8.90000
compos - 3DOk1 == 0 -10.12000
3DOk4 - 3DOk13 == 0
1.38000
3DOk5 - 3DOk13 == 0
10.72000
3DOk7 - 3DOk13 == 0
6.66000
compos - 3DOk13 == 0
5.44000
3DOk5 - 3DOk4 == 0
9.34000
3DOk7 - 3DOk4 == 0
5.28000
compos - 3DOk4 == 0
4.06000
3DOk7 - 3DOk5 == 0
-4.06000
compos - 3DOk5 == 0
-5.28000
compos - 3DOk7 == 0
-1.22000

--Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1


(Adjusted p values reported -- holm method)
>
> confint(dht)

lwr
upr
-22.27276 -8.84724
-20.89276 -7.46724
-11.55276
1.87276
-15.61276 -2.18724
-16.83276 -3.40724
-5.33276
8.09276
4.00724 17.43276
-0.05276 13.37276
-1.27276 12.15276
2.62724 16.05276
-1.43276 11.99276
-2.65276 10.77276
-10.77276
2.65276
-11.99276
1.43276
-7.93276
5.49276

> dht <- glht(g, linfct = mcp(strain = "Dunnett"), base=6)


the first trt group as the default
> summary(dht) # This uses the wrong group as control

Simultaneous Confidence Intervals


Multiple Comparisons of Means: Dunnett Contrasts
Fit: aov(formula = n ~ strain)
Estimated Quantile = 2.6952
95% family-wise confidence level
Linear Hypotheses:

# Uses

Simultaneous Tests for General Linear Hypotheses


Multiple Comparisons of Means: Dunnett Contrasts
Fit: aov(formula = n ~ strain)
Linear Hypotheses:
Estimate Std. Error t value Pr(>|t|)
3DOk13 - 3DOk1 == 0 -15.560
2.172 -7.166 < 0.001 ***
3DOk4 - 3DOk1 == 0
-14.180
2.172 -6.530 < 0.001 ***
3DOk5 - 3DOk1 == 0
-4.840
2.172 -2.229 0.12972
3DOk7 - 3DOk1 == 0
-8.900
2.172 -4.099 0.00192 **
compos - 3DOk1 == 0 -10.120
2.172 -4.660 < 0.001 ***
--Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
(Adjusted p values reported -- single-step method)
>
> summary(dht, test=adjusted("holm")) #adjusts the p-values using
the Holm method
Simultaneous Tests for General Linear Hypotheses
Multiple Comparisons of Means: Dunnett Contrasts
Fit: aov(formula = n ~ strain)
Linear Hypotheses:
Estimate Std. Error t value Pr(>|t|)
3DOk13 - 3DOk1 == 0 -15.560
2.172 -7.166 1.05e-06 ***
3DOk4 - 3DOk1 == 0
-14.180
2.172 -6.530 3.76e-06 ***
3DOk5 - 3DOk1 == 0
-4.840
2.172 -2.229 0.035446 *
3DOk7 - 3DOk1 == 0
-8.900
2.172 -4.099 0.000821 ***
compos - 3DOk1 == 0 -10.120
2.172 -4.660 0.000295 ***

Estimate lwr
upr
3DOk13 - 3DOk1 == 0 -15.5600 -21.4126 -9.7074
3DOk4 - 3DOk1 == 0 -14.1800 -20.0326 -8.3274
3DOk5 - 3DOk1 == 0
-4.8400 -10.6926
1.0126
3DOk7 - 3DOk1 == 0
-8.9000 -14.7526 -3.0474
compos - 3DOk1 == 0 -10.1200 -15.9726 -4.2674
> c1 <- c(0,0,0,0,0,1)
> c2 <- c(0,1,0,0,0,-1)
> c3 <- c(0,0,1,0,0,-1)
> c4 <- c(0,0,0,1,0,-1)
> c5 <- c(0,0,0,0,1,-1)
> cmat <- t(cbind(c1,c2,c3,c4,c5))
> cmat
[,1] [,2] [,3] [,4] [,5] [,6]
c1
0
0
0
0
0
1
c2
0
1
0
0
0
-1
c3
0
0
1
0
0
-1
c4
0
0
0
1
0
-1
c5
0
0
0
0
1
-1
> dht <- glht(g, linfct = cmat) # the dunnett contrasts by hand
> summary(dht) # Gives the adjusted p-values
Simultaneous Tests for General Linear Hypotheses
Fit: aov(formula = n ~ strain)
Linear Hypotheses:
Estimate Std. Error t value Pr(>|t|)
c1 == 0 -10.120
2.172 -4.660
<0.001 ***
c2 == 0
-5.440
2.172 -2.505
0.0747 .
c3 == 0
-4.060
2.172 -1.870
0.2487
c4 == 0
5.280
2.172
2.431
0.0868 .
c5 == 0
1.220
2.172
0.562
0.9703
--Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
(Adjusted p values reported -- single-step method

You might also like