Вы находитесь на странице: 1из 9

Battery Example in R

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


> attach(data) # makes "data" the default data set
> names(data)
[1] "temp" "mat"
>
> data
temp mat life
1
1
1 130

"life"

(data omitted)

160

36
3
3
60
> windows(record=T)
#opens a graph window and starts recording
> temp <- factor(temp)
> mat <- factor(mat)
> interaction.plot(temp,mat,life,type=("b")) #b -> both lines and
symbols

140

mat
3

3
3
1
2

3
1
2

100

80

60

mean of life

120

2
1

3
temp

> g <- aov(life ~ mat +temp+mat:temp) # or temp*map, which includes


main eff and interaction
> summary(g)
Df Sum Sq Mean Sq F value
Pr(>F)
mat
2 10684
5342 7.9114 0.001976 **
temp
2 39119
19559 28.9677 1.909e-07 ***
mat:temp
4
9614
2403 3.5595 0.018611 *
Residuals
27 18231
675
--Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
>
> model.matrix(g)
(I eliminated redundant rows to get the reduced design matrix
(Intercept) mat2 mat3 temp2 temp3 mat2:temp2 mat3:temp2 mat2:temp3 mat3:temp3
1
1
0
0
0
0
0
0
0
0
5
1
1
0
0
0
0
0
0
0
9
1
0
1
0
0
0
0
0
0
13
1
0
0
1
0
0
0
0
0
17
1
1
0
1
0
1
0
0
0
21
1
0
1
1
0
0
1
0
0
25
1
0
0
0
1
0
0
0
0
29
1
1
0
0
1
0
0
1
0
33
1
0
1
0
1
0
0
0
1
attr(,"assign")
[1] 0 1 1 2 2 3 3 3 3
attr(,"contrasts")
attr(,"contrasts")$mat
[1] "contr.treatment"
attr(,"contrasts")$temp
[1] "contr.treatment"

> model.tables(g,"means")
Tables of means
Grand mean

# This gives a table of lsmeans

105.5278
mat
mat
1
2
3
83.17 108.33 125.08
temp
temp
1
2
144.83 107.58

3
64.17

mat:temp

temp
mat 1
2
3
1 134.75 57.25 57.50
2 155.75 119.75 49.50
3 144.00 145.75 85.50
>
#"effects" gives a table of parameter estimates
>
> # Tukey comparisons on main effects or interaction effects;
> TukeyHSD(g)
Tukey multiple comparisons of means
95% family-wise confidence level
Fit: aov(formula = life ~ mat + temp + mat:temp)
$mat
diff
lwr
upr
p adj
2-1 25.16667 -1.135677 51.46901 0.0627571
3-1 41.91667 15.614323 68.21901 0.0014162
3-2 16.75000 -9.552344 43.05234 0.2717815
$temp
diff
lwr
upr
p adj
2-1 -37.25000 -63.55234 -10.94766 0.0043788
3-1 -80.66667 -106.96901 -54.36432 0.0000001
3-2 -43.41667 -69.71901 -17.11432 0.0009787
$`mat:temp`
2:1-1:1
3:1-1:1
1:2-1:1
2:2-1:1
3:2-1:1
1:3-1:1
2:3-1:1
3:3-1:1
3:1-2:1
1:2-2:1
2:2-2:1
3:2-2:1
1:3-2:1
2:3-2:1
3:3-2:1
1:2-3:1
2:2-3:1
3:2-3:1
1:3-3:1
2:3-3:1
3:3-3:1

diff
21.00
9.25
-77.50
-15.00
11.00
-77.25
-85.25
-49.25
-11.75
-98.50
-36.00
-10.00
-98.25
-106.25
-70.25
-86.75
-24.25
1.75
-86.50
-94.50
-58.50

lwr
-40.823184
-52.573184
-139.323184
-76.823184
-50.823184
-139.073184
-147.073184
-111.073184
-73.573184
-160.323184
-97.823184
-71.823184
-160.073184
-168.073184
-132.073184
-148.573184
-86.073184
-60.073184
-148.323184
-156.323184
-120.323184

upr
82.823184
71.073184
-15.676816
46.823184
72.823184
-15.426816
-23.426816
12.573184
50.073184
-36.676816
25.823184
51.823184
-36.426816
-44.426816
-8.426816
-24.926816
37.573184
63.573184
-24.676816
-32.676816
3.323184

p adj
0.9616404
0.9998527
0.0065212
0.9953182
0.9994703
0.0067471
0.0022351
0.2016535
0.9991463
0.0003449
0.5819453
0.9997369
0.0003574
0.0001152
0.0172076
0.0018119
0.9165175
1.0000000
0.0018765
0.0006078
0.0742711

2:2-1:2
3:2-1:2
1:3-1:2
2:3-1:2
3:3-1:2
3:2-2:2
1:3-2:2
2:3-2:2
3:3-2:2
1:3-3:2
2:3-3:2
3:3-3:2
2:3-1:3
3:3-1:3
3:3-2:3

62.50
88.50
0.25
-7.75
28.25
26.00
-62.25
-70.25
-34.25
-88.25
-96.25
-60.25
-8.00
28.00
36.00

0.676816
26.676816
-61.573184
-69.573184
-33.573184
-35.823184
-124.073184
-132.073184
-96.073184
-150.073184
-158.073184
-122.073184
-69.823184
-33.823184
-25.823184

124.323184
150.323184
62.073184
54.073184
90.073184
87.823184
-0.426816
-8.426816
27.573184
-26.426816
-34.426816
1.573184
53.823184
89.823184
97.823184

0.0460388
0.0014173
1.0000000
0.9999614
0.8281938
0.8822881
0.0474675
0.0172076
0.6420441
0.0014679
0.0004744
0.0604247
0.9999508
0.8347331
0.5819453

> TukeyHSD(g,"mat")
Tukey multiple comparisons of means
95% family-wise confidence level
Fit: aov(formula = life ~ mat + temp + mat:temp)
$mat
diff
lwr
upr
p adj
2-1 25.16667 -1.135677 51.46901 0.0627571
3-1 41.91667 15.614323 68.21901 0.0014162
3-2 16.75000 -9.552344 43.05234 0.2717815
> TukeyHSD(g,c("temp","mat"))
Tukey multiple comparisons of means
95% family-wise confidence level
Fit: aov(formula = life ~ mat + temp + mat:temp)
$temp
diff
lwr
upr
p adj
2-1 -37.25000 -63.55234 -10.94766 0.0043788
3-1 -80.66667 -106.96901 -54.36432 0.0000001
3-2 -43.41667 -69.71901 -17.11432 0.0009787
$mat
diff
lwr
upr
p adj
2-1 25.16667 -1.135677 51.46901 0.0627571
3-1 41.91667 15.614323 68.21901 0.0014162
3-2 16.75000 -9.552344 43.05234 0.2717815

> install.packages("multcomp")
> library(multcomp)
>
>
> dht <- glht(g, linfct = mcp(temp = "Tukey"))
Warning message:
In mcp2matrix(model, linfct = linfct) :
covariate interactions found -- default contrast might be
inappropriate
> summary(dht) # Gives the adjusted p-values
Simultaneous Tests for General Linear Hypotheses
Multiple Comparisons of Means: Tukey Contrasts

Fit: aov(formula = life ~ mat + temp + mat:temp)


Linear Hypotheses:
Estimate Std. Error t value Pr(>|t|)
2 - 1 == 0
-77.50
18.37 -4.218
<0.001 ***
3 - 1 == 0
-77.25
18.37 -4.204
<0.001 ***
3 - 2 == 0
0.25
18.37
0.014
1
--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 = life ~ mat + temp + mat:temp)


Estimated Quantile = 2.4803
95% family-wise confidence level

Linear Hypotheses:
Estimate lwr
upr
2 - 1 == 0 -77.5000 -123.0736 -31.9264
3 - 1 == 0 -77.2500 -122.8236 -31.6764
3 - 2 == 0
0.2500 -45.3236
45.8236

>
> dht<-glht(g, linfct = mcp(temp="Tukey"), interaction_average=TRUE)
Warning message:
In mcp2matrix(model, linfct = linfct) :
covariate interactions found -- default contrast might be
inappropriate
> summary(dht)
Simultaneous Tests for General Linear Hypotheses
Multiple Comparisons of Means: Tukey Contrasts

Fit: aov(formula = life ~ mat + temp + mat:temp)


Linear Hypotheses:
Estimate Std. Error t value Pr(>|t|)
2 - 1 == 0
-77.50
18.37 -4.218
<0.001 ***
3 - 1 == 0
-77.25
18.37 -4.204
<0.001 ***
3 - 2 == 0
0.25
18.37
0.014
1
--Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
(Adjusted p values reported -- single-step method)
> dht <- glht(g, linfct = mcp(temp =
"Tukey"),interaction_average=TRUE,covariate_average=TRUE)
Warning message:
In mcp2matrix(model, linfct = linfct) :
covariate interactions found -- default contrast might be
inappropriate
> summary(dht)
Simultaneous Tests for General Linear Hypotheses
Multiple Comparisons of Means: Tukey Contrasts

Fit: aov(formula = life ~ mat + temp + mat:temp)


Linear Hypotheses:
Estimate Std. Error t value Pr(>|t|)
2 - 1 == 0
-77.50
18.37 -4.218 0.000653 ***
3 - 1 == 0
-77.25
18.37 -4.204 0.000721 ***
3 - 2 == 0
0.25
18.37
0.014 0.999898
--Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
(Adjusted p values reported -- single-step method)

>
>
>
>
>
>

h <- aov(life ~ -1+temp:mat)


c1 <- c(1,-1,0,0,0,0,0,0,0)
c2 <- c(1,0,-1,0,0,0,0,0,0)
c3 <- c(0,1,-1,0,0,0,0,0,0)
cmat <- rbind(c1,c2,c3)
cmat
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
c1
1
-1
0
0
0
0
0
0
0
c2
1
0
-1
0
0
0
0
0
0
c3
0
1
-1
0
0
0
0
0
0
> hht <- glht(h, linfct = cmat)
> summary(hht)
Simultaneous Tests for General Linear Hypotheses
Fit: aov(formula = life ~ -1 + temp:mat)
Linear Hypotheses:
Estimate Std. Error t value Pr(>|t|)
c1 == 0
77.50
18.37
4.218 0.000662 ***
c2 == 0
77.25
18.37
4.204 0.000709 ***
c3 == 0
-0.25
18.37 -0.014 0.999898
--Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
(Adjusted p values reported -- single-step method)
>
> install.packages("car")
> library(car)
> h <- aov(life ~ -1+temp:mat)
> linearHypothesis(h,c(1,-2,1,0,0,0,-1,2,-1))
Linear hypothesis test
Hypothesis:
temp1:mat1 - 2 temp2:mat1 + temp3:mat1 - temp1:mat3 + 2 temp2:mat3 temp3:mat3 = 0
Model 1: life ~ -1 + temp:mat
Model 2: restricted model
Res.Df
RSS Df Sum of Sq
F
Pr(>F)
1
27 18231
2
28 24741 -1
-6510 9.6414 0.004434 **
--Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1

>
>
>
>
>

# Now test two linear functions simultaneously


d1 <- c(1,0,0,-1,0,0,0,0,0)
d2 <- c(1,0,0,0,0,0,-1,0,0) # compare 3 mats at temp=1
dmat <- rbind(d1,d2)
dmat
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
d1
1
0
0
-1
0
0
0
0
0
d2
1
0
0
0
0
0
-1
0
0
> linearHypothesis(h,dmat, rhs=c(0,0)) #lht() is the abbrev form
Linear hypothesis test
Hypothesis:
temp1:mat1 - temp1:mat2 = 0
temp1:mat1 - temp1:mat3 = 0
Model 1: life ~ -1 + temp:mat
Model 2: restricted model
Res.Df
RSS Df Sum of Sq
F Pr(>F)
1
27 18230.8
2
29 19116.9 -2
-886.2 0.6562 0.5269
> # The above matches the "slice=mat" for temp=1 from SAS
> install.packages("gmodels")
> library(gmodels)
> t1 <- c(2,-1,-1)
> t2 <- c(0,1,-1)
> tmat <- rbind(t1,t2)
> tmat
[,1] [,2] [,3]
t1
2
-1
-1
t2
0
1
-1
> # Compute tests and CIs for multiple contrasts of the temp effect
> fit.contrast(g,temp,tmat,conf.int=0.95)
Estimate Std. Error
t value
Pr(>|t|) lower CI upper CI
tempt1 154.75 31.82482 4.86255630 0.0000440664 89.45085 220.04915
tempt2 -0.25 18.37407 -0.01360613 0.9892442560 -37.95048 37.45048
>
> # Now use the glh.test function in gmodels to test two hypotheses
> r1 <- c(2,-1,-1,2,-1,-1,2,-1,-1)
> r2 <- c(0,1,-1,0,1,-1,0,1,-1)
> rmat <- rbind(r1,r2)
> rmat
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
r1
2
-1
-1
2
-1
-1
2
-1
-1
r2
0
1
-1
0
1
-1
0
1
-1
> coef(h)
temp1:mat1 temp2:mat1 temp3:mat1 temp1:mat2 temp2:mat2 temp3:mat2
temp1:mat3 temp2:mat3
134.75
57.25
57.50
155.75
119.75
49.50
144.00
145.75
temp3:mat3

85.50
> test2 <- glh.test(h,rmat,d=c(0,0))
> summary(test2)
Test of General Linear Hypothesis
Regression: h
Null Hypothesis: C %*% Beta-hat = d
C matrix:
temp1:mat1 temp2:mat1 temp3:mat1 temp1:mat2 temp2:mat2 temp3:mat2
temp1:mat3 temp2:mat3
r1
2
-1
-1
2
-1
-1
2
-1
r2
0
1
-1
0
1
-1
0
1
temp3:mat3
r1
-1
r2
-1
d vector:
[1] 0 0
C %*% Beta-hat:
[1] 353.75 130.25
F = 28.9677, df1 =

2, df2 = 27, p-value = 1.909e-07

> # result matches the main effect for temp from the aov table
> # The "estimable" function computes Chi-square tests (instead of
F-tests)
> eef <- estimable(h,dmat,joint.test=T) #joint.test=F gives two
# separate tests
X2.stat DF Pr(>|X^2|)
1 1.312425 2 0.5188125
> eef[1,1]
[1] 1.312425
> eef[1,2]
[1] 2
> df.residual(h)
[1] 27
> f <- eef[1,1]/eef[1,2]
> f
[1] 0.6562127
> pval <- 1-pf(f,eef[1,2],df.residual(h))
> pval
[1] 0.5268904
> # matches the f-test produced by glh
> glh.test(h,dmat, d=c(0,0))
Test of General Linear Hypothesis
Call:
glh.test(reg = h, cm = dmat, d = c(0, 0))
F = 0.6562, df1 = 2, df2 = 27, p-value = 0.5269

Вам также может понравиться