-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunderstanding_parameters_lm_anova.Rmd
78 lines (56 loc) · 1.58 KB
/
understanding_parameters_lm_anova.Rmd
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
Interpreting the parameters of a linear model in a simple 2x2 anova design
===========================================================================
# Time-stamp: <2012-03-13 09:57 [email protected]>
Comparison of two groups
========================
```{r}
meang1 <- 100
meang2 <- 110
g1 <- meang1 + scale(rnorm(30,))*10
g2 <- meang2 + scale(rnorm(30))*10
```
```
boxplot(cbind(g1, g2))
t.test(g1, g2, var.equal=TRUE)
```
```{r}
```
First, let us create a 2x2 design, resulting from the crossing of binary factors 'a' and 'b':
```{r}
a <- gl(2, 1, 4)
b <- gl(2, 2, 4)
means <- c(4, 6, 10, 15)
interaction.plot(a,b,means,ylim=c(0,17),legend=F,type='b',pch=16,bty="l")
```
First, we fit an additive model without the interaction term.
```{r}
tapply(means, list(a=a, b=b), mean)
diff(tapply(means, list(a), mean)) # main effect of a
diff(tapply(means, list(b), mean)) # main effect of b
summary(mod2<-lm(means~a+b))
model.matrix(mod2)
model.tables(aov(means~a+b))
model.tables(aov(means~a*b))
```
Then, we fit a model including the interaction:
```{r, fig=TRUE}
par(las=1)
interaction.plot(a,b,means,ylim=c(0,17),legend=F,type='b',pch=16,bty="l")
eps=.9
text(1,4+eps,"a1b1")
text(2,6+eps,"a2b1")
text(1,10+eps,"a1b2")
text(2,15+eps,"a2b2")
lines(c(1,2),c(10,12),lty=3)
arrows(1,0,1,4, code=3,length=.1)
arrows(2,4,2,6, code=3, length=.1)
arrows(1,4,1,10, code=3, length=.1)
arrows(2,12,2,15, code=3, length=.1)
mod1 = lm(means~a*b)
model.matrix(mod1)
summary(mod1)
text(1.08,2,"Intercept") # intercept
text(2.08,5,"a2") # a2
text(1.08,7.5,"b2") # b2
text(2.08,13,"a2:b2") # a2:b2
```