-
Notifications
You must be signed in to change notification settings - Fork 0
/
anscombe_triptychs.Rmd
254 lines (207 loc) · 8.75 KB
/
anscombe_triptychs.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# Anscombe's triptychs
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, error = TRUE)
```
## Generate data
We attach the tidyverse packages to simulate the data and the brms package.
```{r}
library(tidyverse)
library(brms)
```
## Basic model
We first generate the predictors, M and X for 900 rows.
We set a seed, so that the simulated random numbers are reproducible.
The following assumptions are made:
- `M` takes one of three values (0, 1, 2) that are equally probable.
- `X` depends on M to some degree (a coefficient of `0.8`) and varies substantially in addition to that.
- `X` has a standard deviation of ~1.
- We already simulate the residual variation for the outcome variable `Y`.
```{r}
cases <- list()
n <- 900
set.seed(20191005)
moderation <- tibble(
M = sample(0:2, n, replace = TRUE),
M_f = factor(M, 0:2, c("low", "medium", "high")),
X = 0.8 * (M-1) + rnorm(n, sd = 0.77),
residual = rnorm(n)
)
sd(moderation$M)
sd(moderation$X)
```
## "Ideal" case
Here, we simulate the "ideal" moderation scenario that most people will assume, i.e. there is a causal interaction between X and M, on top of main effects for both X and M. The interaction takes the form of an attenuation of the main effect for X (or, respectively, M).
```{r}
moderation <- moderation %>%
mutate(
Y = -0.66 + 0.1*X + 0.5*M + 0.3 * X * M + 0.69 * residual
)
sd(moderation$X)
sd(moderation$Y)
```
```{r}
cases$assumed <- moderation
round(coef(lm(Y ~ X*M, cases$assumed)),2)
ggplot(cases$assumed,
aes(X, Y)) +
geom_point(aes(color = M), alpha = 0.1) +
geom_smooth(method = "lm") +
scale_color_viridis_c(guide = F) +
facet_wrap(~ M_f)
```
## Floor effect
```{r}
moderation <- cases$assumed %>%
mutate(
Y = -1.1 + 0.68*X + 0.8*M + 0.95 * residual,
Y = -0.66 + if_else(Y < -0.7, -0.7, Y) + 0.52
)
sd(moderation$Y)
round(coef(lm(Y ~ X*M, moderation)),2)
round(coef(lm(Y ~ X*M, cases$assumed)),2)
```
```{r}
cases$floor <- moderation
ggplot(cases$floor,
aes(X, Y)) +
geom_point(aes(color = M), alpha = 0.1) +
geom_smooth(method = "lm") +
scale_color_viridis_c(guide = F) +
facet_wrap(~ M_f)
```
## Residual variance
```{r}
moderation <- cases$assumed %>%
mutate(
Y = -0.66 + 0.1*X + 0.5*M + 0.3 * X * M + 0.49*((exp(M)/3-0.2))*residual
)
sd(moderation$Y)
mean(moderation$Y)
round(coef(lm(Y ~ X*M, moderation)),2)
round(coef(lm(Y ~ X*M, cases$assumed)),2)
```
```{r}
cases$residual <- moderation
moderation %>% group_by(M_f) %>%
do(broom::tidy(cor.test(.$X, .$Y)))
moderation %>% group_by(M_f) %>%
do(broom::tidy(cor.test(.$X, resid(lm(.$Y ~ .$M)))))
# plot(lm(Y ~ X*M, moderation))
round(coef(lm(Y ~ X*M, moderation)),2)
ggplot(cases$residual,
aes(X, Y)) +
geom_point(aes(color = M), alpha = 0.1) +
geom_smooth(method = "lm") +
scale_color_viridis_c(guide = F) +
facet_wrap(~ M_f)
```
## Triptychs
```{r}
options(digits = 2)
options(pillar.sigfig = 3)
```
### Model fitting
First, we fit simple linear models without account for the particulars of our measure.
```{r}
model <- list()
model$assumed <- brm(Y ~ X*M_f, data = cases$assumed, cores = 4, backend = "cmdstanr", file = "models/assumed")
model$floor <- brm(Y ~ X*M_f, data = cases$floor, cores = 4, backend = "cmdstanr", file = "models/floor")
model$residual <- brm(Y ~ X*M_f, data = cases$residual, cores = 4, backend = "cmdstanr", file = "models/residual")
```
Second, we fit models that account for the floor effect and the heteroskedastic residual variance, respectively.
```{r}
fixed_model <- list()
fixed_model$assumed <- brm(Y ~ X*M_f, data = cases$assumed, cores = 4, backend = "cmdstanr", file = "models/assumed")
fixed_model$floor <- brm(Y | cens(censored) ~ X*M_f, data = cases$floor %>% mutate(censored = if_else(Y < -0.83, "left", "none")), cores = 4, backend = "cmdstanr", file = "models/fixed_floor")
fixed_model$residual <- brm(bf(Y ~ X*M_f, sigma ~ M_f), data = cases$residual, cores = 4, backend = "cmdstanr", file = "models/fixed_residual")
```
We generate fitted effects for the improper models.
```{r}
library(bayesplot)
library(cowplot)
cases$assumed[,c("estimate__", "std.error__", "lower__", "upper__")] <- fitted(model$assumed)
cases$floor[,c("estimate__", "std.error__", "lower__", "upper__")] <- fitted(model$floor)
cases$residual[,c("estimate__", "std.error__", "lower__", "upper__")] <- fitted(model$residual)
three_cases <- bind_rows(cases, .id = "case")
three_cases %>% select(-M_f, -ends_with("__")) %>% group_by(case) %>% summarise_all(list(~mean(.), ~sd(.)))
three_cases %>% group_by(case) %>% do(broom::tidy(lm(Y ~ X*M, .))) %>% select(case, term, estimate) %>% tidyr::pivot_wider(id_cols = case, names_from = term, values_from = estimate)
```
### Typical plot without raw data
```{r}
ggplot(three_cases,
aes(X, Y)) +
# geom_point(aes(color = M), alpha = 0.1) +
geom_line(aes(X, estimate__)) +
scale_color_viridis_c(guide = F) +
facet_grid(case ~ M_f) +
theme_minimal()
```
### Suggested plot with raw data
```{r}
(cors <- three_cases %>% group_by(case, M_f) %>%
do(broom::tidy(cor.test(.$X, .$Y))))
ggplot(three_cases,
aes(X, Y)) +
geom_point(aes(color = M), alpha = 0.1) +
geom_smooth(aes(X, estimate__, ymin = lower__, ymax = upper__),
color = "black", stat = 'identity') +
scale_color_viridis_c(guide = F, begin = 0.2, end = 0.8) +
facet_grid(case ~ M_f, labeller = labeller(
case = c("assumed" = '"ideal" case',
"floor" = "floor effect,\nno latent interaction",
"residual" = "smaller correlation\nat largest slope"),
M_f = c("low" = "Moderator: low",
"medium" = "Moderator: medium",
"high" = "Moderator: high"))) +
geom_text(aes(label = sprintf('italic("r")==%.2f', estimate, conf.low, conf.high)), x= -2.5, y=2.5, data = cors, hjust = 0, parse = TRUE, size = 3) +
ggthemes::theme_clean() +
coord_cartesian(ylim = c(-2.5,3.6))
ggsave("plots/anscombe_triptychs.pdf", width = 6, height = 6)
ggsave("plots/anscombe_triptychs.png", width = 6, height = 6)
```
## Posterior predictive checks
```{r}
plot_grid(
pp_check(model$assumed) + guides(color = F), # pp_check(model$assumed, type = "error_scatter_avg", alpha = 0.1),
pp_check(model$floor) + theme(legend.position = c(1,0.9), legend.justification = "right"), # pp_check(model$floor, type = "error_scatter_avg", alpha = 0.1),
pp_check(model$residual) + guides(color = F),
# pp_check(model$residual, type = "error_scatter_avg", alpha = 0.1),
nrow = 1, labels = "AUTO", label_size = 6, axis = "lb", align = "hv")
ggsave("plots/three_dgms_ppc.pdf", width = 5, height = 2)
ggsave("plots/three_dgms_ppc.png", width = 5, height = 2)
plot_grid(
pp_check(fixed_model$assumed) + guides(color = F), #pp_check(fixed_model$assumed, type = "error_scatter_avg", alpha = 0.1),
pp_check(fixed_model$floor) + theme(legend.position = c(1,0.9), legend.justification = "right"),# pp_check(fixed_model$floor, type = "error_scatter_avg", alpha = 0.1),
pp_check(fixed_model$residual) + guides(color = F),
# pp_check(fixed_model$residual, type = "error_scatter_avg", alpha = 0.1),
nrow = 1, labels = "AUTO", label_size = 6, axis = "lb", align = "hv")
ggsave("plots/fixed3_dgms_ppc.pdf", width = 5, height = 2)
ggsave("plots/fixed3_dgms_ppc.png", width = 5, height = 2)
```
### Triptychs with proper models
```{r}
cases$assumed[,c("estimate__", "std.error__", "lower__", "upper__")] <- fitted(fixed_model$assumed)
cases$floor[,c("estimate__", "std.error__", "lower__", "upper__")] <- fitted(fixed_model$floor)
cases$residual[,c("estimate__", "std.error__", "lower__", "upper__")] <- fitted(fixed_model$residual)
three_cases <- bind_rows(cases, .id = "case")
three_cases %>% select(-M_f, -ends_with("__")) %>% group_by(case) %>% summarise_all(list(~mean(.), ~sd(.)))
three_cases %>% group_by(case) %>% do(broom::tidy(lm(Y ~ X*M, .))) %>% select(case, term, estimate) %>% tidyr::pivot_wider(id_cols = case, names_from = term, values_from = estimate)
(cors <- three_cases %>% group_by(case, M_f) %>%
do(broom::tidy(cor.test(.$X, .$Y))))
ggplot(three_cases,
aes(X, Y)) +
geom_point(aes(color = M), alpha = 0.1) +
geom_smooth(aes(X, estimate__, ymin = lower__, ymax = upper__),
color = "black", stat = 'identity') +
scale_color_viridis_c(guide = F, begin = 0.2, end = 0.8) +
facet_grid(case ~ M_f, labeller = labeller(
case = c("assumed" = '"ideal" case',
"floor" = "floor effect,\nno latent interaction",
"residual" = "smaller correlation\nat largest slope"),
M_f = c("low" = "Moderator: low",
"medium" = "Moderator: medium",
"high" = "Moderator: high"))) +
geom_text(aes(label = sprintf('italic("r")==%.2f', estimate, conf.low, conf.high)), x= -2.5, y=2.5, data = cors, hjust = 0, parse = TRUE, size = 3) +
ggthemes::theme_clean() +
coord_cartesian(ylim = c(-2.5,3.6))
```