-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvariance-components.plots.Rmd
94 lines (77 loc) · 3 KB
/
variance-components.plots.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# Variance components
```{r}
set.seed(1234)
df <- expand.grid(person=1:3, trial = 1:5) %>%
group_by(person) %>%
mutate(u = person*2,
u_x = person*3, x = round( u_x + trial)) %>%
mutate(y = u + -.5*trial + .5*x + rnorm(n())) %>%
arrange(person, trial) %>%
ungroup() %>%
mutate(person = LETTERS[person]) %>%
mutate(grandmean = mean(y)+1) %>%
group_by(person) %>%
mutate(person.mean = mean(y), xmin=min(x)-1, xmax=max(x)+1, person.x.mean = mean(x)) %>%
ungroup()
```
```{r}
df %>%
ungroup() %>%
mutate(grandmean = mean(y)) %>%
group_by(person) %>%
mutate(person.mean = mean(y), xmin=min(x)-1, xmax=max(x)+1) %>%
ungroup() %>%
ggplot(aes(x, y, color=person)) +
geom_point() + coord_fixed()
ggsave('images/variance-components-1-nolines.pdf')
```
```{r}
df %>%
ggplot(aes(x, y, color=person)) +
geom_point() +
geom_hline(aes(yintercept=grandmean), linetype="dotted") +
geom_rect(aes(xmin=x, xmax=x+(y-grandmean), ymin=grandmean, ymax=y, fill=person), alpha=.3, color=F) +
coord_fixed(xlim=c(0,16)) + guides(fill=F, color=F)
ggsave('images/variance-components-2-sumssquares.pdf')
```
```{r}
df %>%
ggplot(aes(x, y, color=person)) +
geom_point() +
geom_point(aes(x=person.x.mean, y=person.mean), shape=3, size=2)+
geom_segment(aes(x=xmin+2, y=person.mean, yend=person.mean, xend=xmax-2)) +
geom_rect(aes(ymin=person.mean,
xmin= person.x.mean - (grandmean-person.mean),
ymax=grandmean,
xmax = person.x.mean, fill=person), alpha=.2, color=F) +
geom_hline(aes(yintercept=grandmean), linetype="dotted") +
coord_fixed(xlim=c(0,16)) + guides(fill=F, color=F)
ggsave('images/variance-components-3-sumssquaresbetween.pdf')
```
```{r}
df %>%
ggplot(aes(x, y, color=person)) +
geom_point() +
geom_point(aes(x=person.x.mean, y=person.mean), shape=3, size=2)+
geom_segment(aes(x=xmin-1, y=person.mean, yend=person.mean, xend=xmax+1)) +
geom_rect(aes(xmin=x, xmax=x+(y-person.mean), ymin=person.mean, ymax=y, fill=person), alpha=.4, color=F) +
geom_hline(aes(yintercept=grandmean), linetype="dotted") +
coord_fixed(xlim=c(0,16)) + guides(fill=F, color=F)
ggsave('images/variance-components-3-sumssquareswithin.pdf')
```
```{r}
df %>%
ggplot(aes(x, y, color=person)) +
geom_point() +
geom_point(aes(x=person.x.mean, y=person.mean), shape=3, size=2)+
geom_segment(aes(x=xmin-1, y=person.mean, yend=person.mean, xend=xmax+1)) +
geom_rect(aes(xmin=x, xmax=x+(y-person.mean), ymin=person.mean, ymax=y, fill=person), alpha=.4, color=F) +
geom_rect(aes(ymin=person.mean,
xmin= person.x.mean - (grandmean-person.mean),
ymax=grandmean,
xmax = person.x.mean), fill="black", alpha=.2, color=F) +
geom_hline(aes(yintercept=grandmean), linetype="dotted") +
coord_fixed(xlim=c(0,16)) + guides(fill=F, color=F)
ggsave('images/variance-components-3-sumssquareswithinandbetween.pdf')
ggsave('images/variance-components-3-sumssquareswithinandbetween.png')
```