Sie sind auf Seite 1von 5

>

> abline(coef(g))
>

ONE-WAY Model in lm

1.0
0.6

0.8

drywt

1.2

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


>
> attach(data) # makes "data" the default data set
>
> names(data)
[1] "trt"
"drywt"
>
> data
trt drywt
1
1 1.02
2
1 0.79
3
1 1.00
4
1 0.59
5
1 0.97
6
2 1.00
7
2 1.21
8
2 1.22
9
2 0.96
10
2 0.79
11
3 0.99
12
3 1.36
13
3 1.17
14
3 1.22
15
3 1.12
> windows(record=T)
#opens a graph window and starts recording
>
> plot(drywt~trt) # default is a scatterplot b/c trt is "numeric"
> g <- lm(drywt~trt) # model is a simple regression
> summary(g)

1.0

2.0

2.5

3.0

trt

plot(drywt~factor(trt)) #default is a boxplot b/c trt is a "factor"

Max
0.192667

0.6

0.8

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.72933
0.11046
6.603 1.71e-05 ***
trt
0.14900
0.05113
2.914
0.0121 *
--Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1

1.0

3Q
0.131667

drywt

1.2

Call:
lm(formula = drywt ~ trt)
Residuals:
Min
1Q
Median
-0.288333 -0.077833 -0.006333

1.5

factor(trt)

Residual standard error: 0.1617 on 13 degrees of freedom


Multiple R-squared: 0.3951,
Adjusted R-squared: 0.3486
F-statistic: 8.492 on 1 and 13 DF, p-value: 0.01208

> trt <- factor(trt)


>
> g <- lm(drywt~trt)
> summary(g)

#changes the trt variable to a factor

> g <- lm(drywt~trt -1)


> model.matrix(g)
trt1 trt2 trt3
1
1
0
0
2
1
0
0
3
1
0
0
4
1
0
0
5
1
0
0
6
0
1
0
7
0
1
0
8
0
1
0
9
0
1
0
10
0
1
0
11
0
0
1
12
0
0
1
13
0
0
1
14
0
0
1
15
0
0
1
attr(,"assign")
[1] 1 1 1
attr(,"contrasts")
attr(,"contrasts")$trt
[1] "contr.treatment"

# uses default parameterization

Call:
lm(formula = drywt ~ trt)
Residuals:
Min
1Q Median
-0.284 -0.080 -0.002

3Q
0.136

Max
0.188

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept)
0.8740
0.0752 11.622 6.9e-08 ***
trt2
0.1620
0.1064
1.523
0.154
trt3
0.2980
0.1064
2.802
0.016 *
--Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
Residual standard error: 0.1682 on 12 degrees of freedom
Multiple R-squared: 0.3961,
Adjusted R-squared: 0.2955
F-statistic: 3.936 on 2 and 12 DF, p-value: 0.0485
> model.matrix(g)
(Intercept) trt2 trt3
1
1
0
0
2
1
0
0
3
1
0
0
4
1
0
0
5
1
0
0
6
1
1
0
7
1
1
0
8
1
1
0
9
1
1
0
10
1
1
0
11
1
0
1
12
1
0
1
13
1
0
1
14
1
0
1
15
1
0
1
attr(,"assign")
[1] 0 1 1
attr(,"contrasts")
attr(,"contrasts")$trt
[1] "contr.treatment"

# omits the intercept

>
>
>
>

# sets the parameterization for unordered and ordered factors


options(contrasts = c("contr.treatment", "contr.poly"))
g <- lm(drywt~trt)
model.matrix(g)
(Intercept) trt2 trt3
1
1
0
0
2
1
0
0
3
1
0
0
4
1
0
0
5
1
0
0
6
1
1
0
7
1
1
0
8
1
1
0
9
1
1
0
10
1
1
0
11
1
0
1
12
1
0
1
13
1
0
1
14
1
0
1
15
1
0
1
attr(,"assign")
[1] 0 1 1
attr(,"contrasts")
attr(,"contrasts")$trt
[1] "contr.treatment"

> options(contrasts = c("contr.SAS", "contr.poly"))


> g <- lm(drywt~trt)
> model.matrix(g)
(Intercept) trt1 trt2
1
1
1
0
2
1
1
0
3
1
1
0
4
1
1
0
5
1
1
0
6
1
0
1
7
1
0
1
8
1
0
1
9
1
0
1
10
1
0
1
11
1
0
0
12
1
0
0
13
1
0
0
14
1
0
0
15
1
0
0
attr(,"assign")
[1] 0 1 1
attr(,"contrasts")$trt
[1] "contr.SAS"

> options(contrasts = c("contr.sum", "contr.poly"))


>
> g <- lm(drywt~trt)
> model.matrix(g)
(Intercept) trt1 trt2
1
1
1
0
2
1
1
0
3
1
1
0
4
1
1
0
5
1
1
0
6
1
0
1
7
1
0
1
8
1
0
1
9
1
0
1
10
1
0
1
11
1
-1
-1
12
1
-1
-1
13
1
-1
-1
14
1
-1
-1
15
1
-1
-1
attr(,"assign")
[1] 0 1 1
attr(,"contrasts")
attr(,"contrasts")$trt
[1] "contr.sum"

> options(contrasts = c("contr.helmert", "contr.poly"))


> g <- lm(drywt~trt)
> model.matrix(g)
(Intercept) trt1 trt2
1
1
-1
-1
2
1
-1
-1
3
1
-1
-1
4
1
-1
-1
5
1
-1
-1
6
1
1
-1
7
1
1
-1
8
1
1
-1
9
1
1
-1
10
1
1
-1
11
1
0
2
12
1
0
2
13
1
0
2
14
1
0
2
15
1
0
2
attr(,"assign")
[1] 0 1 1
attr(,"contrasts")$trt
[1] "contr.helmert"

> # If you want to see the contrast coefficients w/o the whole
design matrix
> contr.treatment(3)
2 3
1 0 0
2 1 0
3 0 1
> contr.SAS(3)
1 2
1 1 0
2 0 1
3 0 0
> contr.sum(3)
[,1] [,2]
1
1
0
2
0
1
3
-1
-1
> contr.poly(3)
.L
.Q
[1,] -7.071068e-01 0.4082483
[2,] -9.073264e-17 -0.8164966
[3,] 7.071068e-01 0.4082483

> # Set up your own matrix of contrasts and obtain CI's:


> levels(trt)
[1] "1" "2" "3"
> contrasts(trt) <- cbind(c(2,-1,-1)/3,c(0,0.5,-0.5))
> # I divided the first contrast by 3 so that difference between the
two values would equal 1.0. What is a partial reg coef?
> g <- lm(drywt~trt)
> model.matrix(g)
(Intercept)
trt1 trt2
1
1 0.6666667 0.0
2
1 0.6666667 0.0
3
1 0.6666667 0.0
4
1 0.6666667 0.0
5
1 0.6666667 0.0
6
1 -0.3333333 0.5
7
1 -0.3333333 0.5
8
1 -0.3333333 0.5
9
1 -0.3333333 0.5
10
1 -0.3333333 0.5
11
1 -0.3333333 -0.5
12
1 -0.3333333 -0.5
13
1 -0.3333333 -0.5
14
1 -0.3333333 -0.5
15
1 -0.3333333 -0.5
attr(,"assign")
[1] 0 1 1
attr(,"contrasts")
attr(,"contrasts")$trt
[,1] [,2]
1 0.6666667 0.0
2 -0.3333333 0.5
3 -0.3333333 -0.5
> confint(g,level=0.95) # t-based CI's for lm objects
2.5 %
97.5 %
(Intercept) 0.9327339 1.12193274
trt1
-0.4306756 -0.02932436
trt2
-0.3677203 0.09572027

> summary(g)
Call:
lm(formula = drywt ~ trt)
Residuals:
Min
1Q Median
-0.284 -0.080 -0.002

3Q
0.136

Max
0.188

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 1.02733
0.04342 23.662 1.94e-11 ***
trt1
-0.23000
0.09210 -2.497
0.0281 *
trt2
-0.13600
0.10635 -1.279
0.2252
--Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
Residual standard error: 0.1682 on 12 degrees of freedom
Multiple R-squared: 0.3961,
Adjusted R-squared: 0.2955
F-statistic: 3.936 on 2 and 12 DF, p-value: 0.0485

>
>
>
>

# Dealing with ordered treatment levels


trt <- ordered(trt)
g <- lm(drywt~trt) # uses the "contr.poly" orthogonal polynomials
model.matrix(g)
(Intercept)
trt.L
trt.Q
1
1 -7.071068e-01 0.4082483
2
1 -7.071068e-01 0.4082483
3
1 -7.071068e-01 0.4082483
4
1 -7.071068e-01 0.4082483
5
1 -7.071068e-01 0.4082483
6
1 -9.073264e-17 -0.8164966
7
1 -9.073264e-17 -0.8164966
8
1 -9.073264e-17 -0.8164966
9
1 -9.073264e-17 -0.8164966
10
1 -9.073264e-17 -0.8164966
11
1 7.071068e-01 0.4082483
12
1 7.071068e-01 0.4082483
13
1 7.071068e-01 0.4082483
14
1 7.071068e-01 0.4082483
15
1 7.071068e-01 0.4082483
attr(,"assign")
[1] 0 1 1
attr(,"contrasts")
attr(,"contrasts")$trt
[1] "contr.poly"

> # Confidence intervals for individual means


>
> g <- lm(drywt~trt -1)
> confint(g,level=0.95) # t-based CI's for lm objects
2.5 %
97.5 %
trt1 0.710149 1.037851
trt2 0.872149 1.199851
trt3 1.008149 1.335851

> # Confidence intervals for contrasts the hard way: compare trt 1
to the average of trt 2 and trt 3.
>
> c <- c(1,-0.5,-0.5)
> est <- t(c)%*%coef(g)
> se.c <- sqrt(t(c)%*%vcov(g)%*%c)
> tval <- qt(0.975,df.residual(g))
> lcl <- est-tval*se.c
> ucl <- est+tval*se.c
> est
[,1]
[1,] -0.23
> lcl
[,1]
[1,] -0.4306756
> ucl
[,1]
[1,] -0.02932436

0.6
0.0

0.2

0.4

drywt

0.8

1.0

>
> # Comparing all pairs of individual means
>
> mn <- tapply(drywt,trt,mean)
#computes a table of means
> barplot(mn,xlab="trt",ylab="drywt")

trt

>
> pairwise.t.test(drywt,trt, p.adjust.method="none")
the model

# does not use

Pairwise comparisons using t tests with pooled SD


data:

drywt and trt

1
2
2 0.154 3 0.016 0.225
P value adjustment method: none

10

Das könnte Ihnen auch gefallen