forked from mjskay/uncertainty-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultivariate-regression.Rmd
executable file
·300 lines (239 loc) · 8.41 KB
/
multivariate-regression.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
---
title: "Multivariate regression"
output:
github_document:
toc: true
---
## Setup
Libraries that might be of help:
```{r setup, message = FALSE, warning = FALSE}
library(tidyverse)
library(magrittr)
library(ggplot2)
library(rstan)
library(brms)
library(modelr)
library(tidybayes)
library(ggridges)
library(patchwork) # devtools::install_github("thomasp85/patchwork")
theme_set(theme_light())
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())
```
### Data
```{r}
set.seed(1234)
df = data_frame(
y1 = rnorm(20),
y2 = rnorm(20, y1),
y3 = rnorm(20, -y1)
)
```
### Data plot
```{r}
df %>%
gather(.variable, .value) %>%
gather_pairs(.variable, .value) %>%
ggplot(aes(.x, .y)) +
geom_point() +
facet_grid(.row ~ .col)
```
### Model
```{r, cache = TRUE}
m = brm(cbind(y1, y2, y3) ~ 1, data = df)
```
### Correlations from the model
A plot of the `rescor` coefficients from the model:
```{r}
m %>%
gather_draws(`rescor.*`, regex = TRUE) %>%
separate(.variable, c(".rescor", ".row", ".col"), sep = "__") %>%
ggplot(aes(x = .value, y = 0)) +
geom_halfeyeh() +
xlim(c(-1, 1)) +
xlab("rescor") +
ylab(NULL) +
facet_grid(.row ~ .col)
```
### Altogether
I'm not sure I like this (we're kind of streching the limits of `facet_grid` here...) but if you absolutely must have a combined plot, this sort of thing could work...
```{r}
correlations = m %>%
gather_draws(`rescor.*`, regex = TRUE) %>%
separate(.variable, c(".rescor", ".row", ".col"), sep = "__")
df %>%
gather(.variable, .value) %>%
gather_pairs(.variable, .value) %>%
ggplot(aes(.x, .y)) +
# scatterplots
geom_point() +
# correlations
geom_halfeyeh(aes(x = .value, y = 0), data = correlations) +
geom_vline(aes(xintercept = x), data = correlations %>% data_grid(nesting(.row, .col), x = c(-1, 0, 1))) +
facet_grid(.row ~ .col)
```
### Or side-by-side
Actually, it occurs to me that the traditional "flipped on the axis" double-scatterplot-matrix can be hard to read, because it is hard to mentally do the diagonal-mirroring operation to figure out which cell on one side goes with the other. I find it easier to just map from the same cell in one matrix onto another, which suggests something like this might be better:
```{r, fig.width = 10, fig.height = 5}
data_plot = df %>%
gather(.variable, .value) %>%
gather_pairs(.variable, .value) %>%
ggplot(aes(.x, .y)) +
geom_point(size = 1.5) +
facet_grid(.row ~ .col) +
theme(panel.grid.minor = element_blank()) +
xlab(NULL)+
ylab(NULL)
rescor_plot = m %>%
gather_draws(`rescor.*`, regex = TRUE) %>%
separate(.variable, c(".rescor", ".col", ".row"), sep = "__") %>%
ggplot(aes(x = .value, y = 0)) +
geom_halfeyeh() +
xlim(c(-1, 1)) +
xlab("rescor") +
ylab(NULL) +
facet_grid(.row ~ .col) +
xlab("correlation") +
scale_y_continuous(breaks = NULL)
data_plot + rescor_plot
```
### More heatmap-y
Some other things possibly worth improving:
- adding a color encoding back in for that high-level gist
- making "up" be positive correlation and "down" be negative
- 0 line
```{r, fig.width = 10, fig.height = 5, warning = FALSE, message = FALSE}
rescor_plot_heat = m %>%
gather_draws(`rescor.*`, regex = TRUE) %>%
separate(.variable, c(".rescor", ".col", ".row"), sep = "__") %>%
ggplot(aes(x = .value, y = 0)) +
geom_density_ridges_gradient(aes(fill = stat(x)), color = NA) +
geom_vline(xintercept = 0, color = "gray65", linetype = "dashed") +
stat_pointintervalh() +
xlim(c(-1, 1)) +
xlab("correlation") +
ylab(NULL) +
scale_y_continuous(breaks = NULL) +
scale_fill_distiller(type = "div", palette = "RdBu", direction = 1, limits = c(-1, 1), guide = FALSE) +
coord_flip() +
facet_grid(.row ~ .col)
data_plot + rescor_plot_heat
```
## Okay, but does it scale?
Let's add some more variables...
```{r}
set.seed(1234)
df_large = data_frame(
y1 = rnorm(20),
y2 = rnorm(20, y1),
y3 = rnorm(20, -y1),
y4 = rnorm(20, 0.5 * y1),
y5 = rnorm(20),
y6 = rnorm(20, -.25 * y1),
y7 = rnorm(20, -y5),
y8 = rnorm(20, -0.5 * y5)
)
```
```{r}
data_plot_large = df_large %>%
gather(.variable, .value) %>%
gather_pairs(.variable, .value) %>%
ggplot(aes(.x, .y)) +
geom_point(size = 1) +
facet_grid(.row ~ .col) +
theme(panel.grid.minor = element_blank()) +
xlab(NULL) +
ylab(NULL)
data_plot_large
```
```{r, cache = TRUE}
m_large = brm(cbind(y1, y2, y3, y4, y5, y6, y7, y8) ~ 1, data = df_large)
```
### Density version
I've dropped the intervals for this (they start to become illegible) and did a few other minor tweaks for clarity:
```{r, fig.width = 12, fig.height = 6, warning = FALSE, message = FALSE}
rescor_plot_heat_large = m_large %>%
gather_draws(`rescor.*`, regex = TRUE) %>%
separate(.variable, c(".rescor", ".col", ".row"), sep = "__") %>%
ggplot(aes(x = .value, y = 0)) +
geom_density_ridges_gradient(aes(fill = stat(x)), color = NA) +
geom_vline(xintercept = 0, color = "white", size = 1) +
xlim(c(-1, 1)) +
xlab("correlation") +
ylab(NULL) +
scale_y_continuous(breaks = NULL) +
scale_x_continuous(breaks = NULL) +
scale_fill_distiller(type = "div", palette = "RdBu", direction = 1, limits = c(-1, 1), guide = FALSE) +
coord_flip() +
facet_grid(.row ~ .col)
data_plot_large + rescor_plot_heat_large
```
You can still pick out the high/low correlations by color, though it isn't quite as easy.
### Dither approach
A different, more frequency-framing approach, would be to use dithering to show uncertainty (see e.g. Figure 4 from [this paper](http://doi.wiley.com/10.1002/sta4.150)). This is akin to something like an icon array. You should still be able to see the average color (thanks to the human visual system's ensembling processing), but also get a sense of the uncertainty by how "dithered" a square looks:
```{r, fig.width = 12.75, fig.height = 6}
w = 60
h = 60
rescor_plot_heat_dither = m_large %>%
gather_draws(`rescor.*`, regex = TRUE) %>%
separate(.variable, c(".rescor", ".col", ".row"), sep = "__") %>%
group_by(.row, .col) %>%
summarise(
.value = list(sample(.value, w * h)),
x = list(rep(1:w, times = h)),
y = list(rep(1:h, each = w))
) %>%
unnest() %>%
ggplot(aes(x, y, fill = .value)) +
geom_raster() +
facet_grid(.row ~ .col) +
scale_fill_distiller(type = "div", palette = "RdBu", direction = 1, limits = c(-1, 1), name = "corr.") +
scale_y_continuous(breaks = NULL) +
scale_x_continuous(breaks = NULL) +
xlab(NULL) +
ylab(NULL) +
coord_cartesian(expand = FALSE)
data_plot_large + rescor_plot_heat_dither
```
### Densities with heatmaps?
Going back to densities, what if the point estimate is used to set the cell backgorund --- maybe that will help that format have a high-level gist while retaining its more accurate depiction of the uncertainty:
```{r, fig.width = 12, fig.height = 6, warning = FALSE, message = FALSE}
rescor_plot_heat_large = m_large %>%
gather_draws(`rescor.*`, regex = TRUE) %>%
separate(.variable, c(".rescor", ".col", ".row"), sep = "__") %>%
ggplot(aes(x = .value, y = 0)) +
geom_tile(aes(x = 0, y = 0.5, width = 2, height = 1, fill = .value),
data = function(df) df %>% group_by(.row, .col) %>% median_qi(.value)) +
geom_density_ridges_gradient(aes(height = stat(ndensity), fill = stat(x)), color = NA, scale = 1) +
geom_vline(xintercept = 0, color = "white", alpha = .5) +
geom_density_ridges(aes(height = stat(ndensity)), fill = NA, color = "gray50", scale = 1) +
xlim(c(-1, 1)) +
xlab("correlation") +
ylab(NULL) +
scale_y_continuous(breaks = NULL) +
scale_x_continuous(breaks = NULL) +
scale_fill_distiller(type = "div", palette = "RdBu", direction = 1, limits = c(-1, 1), guide = FALSE) +
coord_flip(expand = FALSE) +
facet_grid(.row ~ .col)
data_plot_large + rescor_plot_heat_large
```
This is, admittedly, a bit weird...
## The no-uncertainty heatmap
For reference:
```{r, fig.width = 12, fig.height = 6, warning = FALSE, message = FALSE}
rescor_plot_heat_large = m_large %>%
gather_draws(`rescor.*`, regex = TRUE) %>%
separate(.variable, c(".rescor", ".col", ".row"), sep = "__") %>%
group_by(.row, .col) %>%
median_qi(.value) %>%
ggplot(aes(x = 0, y = 0, fill = .value)) +
geom_raster() +
xlab("correlation") +
ylab(NULL) +
scale_y_continuous(breaks = NULL) +
scale_x_continuous(breaks = NULL) +
scale_fill_distiller(type = "div", palette = "RdBu", direction = 1, limits = c(-1, 1), guide = FALSE) +
coord_flip(expand = FALSE) +
facet_grid(.row ~ .col)
data_plot_large + rescor_plot_heat_large
```