-
Notifications
You must be signed in to change notification settings - Fork 0
/
comparing-the-means-several-conditions.Rmd
72 lines (52 loc) · 1.54 KB
/
comparing-the-means-several-conditions.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
Comparing several means, iwhtin subject
===========================================
christophe@pallier.org
```{r}
rm(list=ls())
require(ez)
require(gplots)
require(lme4)
```
## Creation of simulated data
```{r simulation}
nsub <- 20 # number of subjects (statistical units)
nconds <- 5 # number of conditions
effects <- c(110, 110, 120, 140, 100)
sd_between_sub <- 10
sd_within_sub <- 4
ot <- data.frame(sub = factor(rep(paste('s',1:nsub,sep=''), each=nconds)),
cond = factor(rep(paste('cond',1:nconds,sep=''), nsub)),
y = effects + rep(rnorm(nsub, sd=sd_between_sub), each=nconds) + rnorm(nsub * nconds, sd=sd_within_sub))
```
### Exploration plots
```{r exploration_plots}
with(ot, interaction.plot(cond, sub, y, main='Cond * Subject plot', legend=FALSE))
ot$ycorr <- ot$y + mean(ot$y) - tapply(ot$y, ot$sub, mean)[ot$sub]
with(ot, interaction.plot(cond, sub, ycorr, main='Cond * Sub after removing Sub main effect', legend=FALSE))
```
### Classical analysis of variance model:
```{r anova}
require(ez)
#summary(aov_model <- aov(y ~ cond + Error(sub/cond), data=ot))
ez_model <- ezANOVA(data=ot,
dv=y,
wid=sub,
within = cond)
print(ez_model)
ezPlot(data=ot,
dv=y,
wid=sub,
within = cond,
x = cond)
```
```{r lmer}
require(lme4)
summary(lmer_model <- lmer(y ~ cond + (1 | sub), data=ot))
anova(lmer_model)
require(car)
Anova(lmer_model)
```
```{r}
# plotmeans(y ~ cond, data=ot, gap=0.1)
plotmeans(ycorr ~ cond, data=ot, gap=0.1)
```