1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
R version 3.2.3 (2015-12-10) -- "Wooden Christmas-Tree"
Copyright (C) 2015 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
>
> library("multcomp")
Loading required package: mvtnorm
Loading required package: survival
Loading required package: TH.data
Loading required package: MASS
Attaching package: 'TH.data'
The following object is masked from 'package:MASS':
geyser
> set.seed(290875)
>
> testdata <- data.frame(y = rnorm(21),
+ f1 <- factor(c(rep(c("A", "B", "C"), 7))),
+ f2 <- factor(c(rep("D", 10), rep("E", 11))),
+ x <- rnorm(21))
>
> # one-way ANOVA
> coef(amod <- aov(y ~ f1, data = testdata))
(Intercept) f1B f1C
-0.4394751 0.5151680 0.6886101
> glht(amod, linfct = mcp(f1 = "Dunnett"))
General Linear Hypotheses
Multiple Comparisons of Means: Dunnett Contrasts
Linear Hypotheses:
Estimate
B - A == 0 0.5152
C - A == 0 0.6886
>
> # and a continuous covariable: ANCOVA
> coef(lmod <- lm(y ~ f1 + x, data = testdata))
(Intercept) f1B f1C x
-0.434528566 0.509444592 0.686181780 -0.009491201
> glht(lmod, linfct = mcp(f1 = "Dunnett"))
General Linear Hypotheses
Multiple Comparisons of Means: Dunnett Contrasts
Linear Hypotheses:
Estimate
B - A == 0 0.5094
C - A == 0 0.6862
>
> # ANCOVA with an additional factor as covariable
> coef(lmod <- lm(y ~ f1 + f2 + x, data = testdata))
(Intercept) f1B f1C f2E x
-0.40849498 0.51296437 0.69200699 -0.05266965 -0.01613183
> glht(lmod, linfct = mcp(f1 = "Dunnett"))
General Linear Hypotheses
Multiple Comparisons of Means: Dunnett Contrasts
Linear Hypotheses:
Estimate
B - A == 0 0.513
C - A == 0 0.692
>
> # and with interaction terms
> coef(lmod <- lm(y ~ f1 + f2 + f2:f1 + x, data = testdata))
(Intercept) f1B f1C f2E x f1B:f2E
-0.44532319 0.70282663 0.65613337 0.05552324 -0.03443721 -0.37862471
f1C:f2E
0.02753451
> glht(lmod, linfct = mcp(f1 = "Dunnett"))
General Linear Hypotheses
Multiple Comparisons of Means: Dunnett Contrasts
Linear Hypotheses:
Estimate
B - A == 0 0.7028
C - A == 0 0.6561
Warning message:
In mcp2matrix(model, linfct = linfct) :
covariate interactions found -- default contrast might be inappropriate
>
> # with contrasts as expressions
> glht(lmod, linfct = mcp(f1 = c("B - A = 0", "C - A = 0")))
General Linear Hypotheses
Multiple Comparisons of Means: User-defined Contrasts
Linear Hypotheses:
Estimate
B - A == 0 0.7028
C - A == 0 0.6561
Warning message:
In mcp2matrix(model, linfct = linfct) :
covariate interactions found -- default contrast might be inappropriate
>
> tmp <- multcomp:::chrlinfct2matrix(c(l1 = "x1 - x2 = 2",
+ l2 = "x2 + 3 * x3 = 1"),
+ paste("x", 1:3, sep = ""))
>
> stopifnot(max(abs(tmp$K - rbind(c(1, -1, 0), c(0, 1, 3)))) < sqrt(.Machine$double.eps))
> stopifnot(max(abs(tmp$m - c(2, 1))) < sqrt(.Machine$double.eps))
>
> ### coef.survreg and vcov.survreg need special tuning
> ### thx to Z for pointing this out
> if (require("survival")) {
+ smod <- survreg(Surv(futime, fustat) ~ ecog.ps + rx,
+ data = ovarian, dist = 'weibull')
+ K <- diag(length(coef(smod)))
+ rownames(K) <- names(coef(smod))
+ glht(smod, linfct = K)
+ }
General Linear Hypotheses
Linear Hypotheses:
Estimate
(Intercept) == 0 6.8967
ecog.ps == 0 -0.3850
rx == 0 0.5286
>
> ### new `means' comparisons
> amod <- aov(weight ~ dose + gesttime + number, data = litter)
> confint(glht(amod, linfct = mcp(dose = "Means")))
Simultaneous Confidence Intervals
Multiple Comparisons of Means: Mean Contrasts
Fit: aov(formula = weight ~ dose + gesttime + number, data = litter)
Quantile = 2.5558
95% family-wise confidence level
Linear Hypotheses:
Estimate lwr upr
0 == 0 32.3651 30.0805 34.6498
5 == 0 29.0127 26.6372 31.3883
50 == 0 30.0743 27.5239 32.6246
500 == 0 29.6899 27.1591 32.2207
>
> proc.time()
user system elapsed
0.516 0.012 0.525
|