-
Notifications
You must be signed in to change notification settings - Fork 0
/
sleepstudy_borrowingstrength_RStudio.Rmd
134 lines (105 loc) · 2.99 KB
/
sleepstudy_borrowingstrength_RStudio.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
---
title: "Borrowing Strength with Linear Mixed Models: R-based lme4"
output: html_document
editor_options:
chunk_output_type: console
---
## Setup
We produce the subject facets for the `sleepstudy` data including
+ observations
+ conditional means
+ within-subject slopes
+ estimate for pooled data
```{r}
library(lme4)
library(tidyverse)
data(sleepstudy)
sleepstudy <- mutate(sleepstudy, Subject = fct_reorder(Subject, Reaction, mean))
```
## Basics
### Fit the model
```{r}
fm <- lmer(Reaction ~ 1 + Days + (Days | Subject), sleepstudy, REML=FALSE,
control=lmerControl(calc.derivs=FALSE))
print(summary(fm))
```
## Extract conditional means
```{r}
cms <- coef(fm)[["Subject"]] %>%
tibble::rownames_to_column("Subject") %>%
rename(Day_0 = `(Intercept)`) %>%
tibble::add_column(Estimate = "Conditional mean")
cms
```
## Facet plot
```{r}
plot1 <-
sleepstudy %>%
mutate(Subject = forcats::fct_reorder(Subject, Reaction, mean)) %>%
ggplot(aes(x = Days, y = Reaction)) +
geom_point() +
geom_abline(data=cms, aes(intercept = Day_0, slope = Days)) +
scale_x_continuous("Day", breaks=seq(0,9)) +
scale_y_continuous("Reaction time [ms]", limits=c(100,500)) +
facet_wrap(~ fct_rev(fct_rev(Subject))) +
theme_bw()
plot1
```
## Demonstration of borrowing strength
### Estimates of within-subject slopes
```{r}
wss <- lmList(Reaction ~ 1 + Days | Subject, sleepstudy) %>%
coef() %>%
tibble::rownames_to_column("Subject") %>%
rename(Day_0 = `(Intercept)`) %>%
tibble::add_column(Estimate = "Within-subject")
```
### Estimate for pooled data
```{r}
pld <- tibble(
Subject = factor(levels(sleepstudy$Subject)),
Day_0 = coef(lm(Reaction ~ 1 + Days, sleepstudy))[1],
Days = coef(lm(Reaction ~ 1 + Days, sleepstudy))[2],
Estimate = "Pooled"
)
```
### Combine the estimates
```{r}
cms_wss_pld <- bind_rows(cms, wss, pld)
cms_wss_pld$Estimate <- factor(cms_wss_pld$Estimate, levels=c("Conditional mean", "Within-subject", "Pooled"))
cms_wss_pld
```
### Combined facet plot
```{r}
plot2 <-
sleepstudy %>%
ggplot(aes(x = Days, y = Reaction)) +
geom_point() +
geom_abline(data=cms_wss_pld, aes(intercept = Day_0, slope = Days, color = Estimate)) +
facet_wrap(~ fct_rev(fct_rev(Subject))) +
scale_x_continuous("Day", breaks=seq(0,9)) +
scale_y_continuous("Reaction time [ms]", limits=c(100,500)) +
theme_bw() +
theme(legend.position = c(.9, .1))
# save the plot
ggsave("plot2.svg", plot2, width = 8, height = 7)
# show the plot
plot2
```
### Shrinkage plot
```{r}
bind_rows(wss, cms) %>%
ggplot(aes(x = Day_0, y = Days, color = Estimate)) +
geom_point() +
geom_point(data = pld) +
geom_path(aes(group = Subject),
arrow = arrow(length = unit(.02, "npc"),end="last")) +
scale_x_continuous("Reaction time @ day 0 [ms]", limits=c(200,300)) +
scale_y_continuous("Change per day [ms]", limits=c(-5,25)) +
theme_bw() + theme(legend.position = "top") +
ggtitle("'Borrowing Strength' (Shrinkage)")
```
## Appendix
```{r}
sessionInfo()
```