-
Notifications
You must be signed in to change notification settings - Fork 15
/
mcse_dotplots.Rmd
executable file
·337 lines (286 loc) · 10.3 KB
/
mcse_dotplots.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
---
title: "Quantile dotplots with Monte Carlo error"
output: github_document
---
## Introduction
This is an attempt to play with adding Monte Carlo standard errors to quantile
dotplots by "blurring" (sort of) each quantile according to its error.
## Setup
```{r setup, message = FALSE, warning = FALSE}
library(tidyverse)
library(ggplot2)
library(posterior) # remotes::install_github("stan-dev/posterior")
library(ggdist)
library(patchwork)
library(distributional)
theme_set(theme_ggdist())
knitr::opts_chunk$set(
dev.args = list(png = list(type = "cairo"))
)
```
## The problem
[Quantile dotplots](https://github.com/mjskay/when-ish-is-my-bus/blob/master/quantile-dotplots.md)
are a useful way to summarize a distribution by plotting a number of
quantiles (often 20, 50, or 100). However, when obtaining samples from Bayesian
posterior distributions using Monte Carlo methods, there is uncertainty not
captured purely by looking at the posterior quantiles: the quantiles themselves
have error associated with them due to the Monte Carlo sampling process.
The (currently experimental) [posterior](https://mc-stan.org/posterior/) package allows calculation of this
error using [`posterior::mcse_quantile()`](https://mc-stan.org/posterior/reference/mcse_quantile.html),
but it is hard to show **two levels** of uncertainty at the same time.
This is one attempt to do that, by showing both 50 quantiles from a sample from
a distribution *and* the uncertainty in those quantiles. Here I am "blurring"
each quantile by overlaying 20 semitransparent quantiles of each quantile taken
according to the Monte Carlo standard error of each quantile:
```{r mcse_dotplot, fig.width = 5, fig.height = 10}
n_dot = 50 # number of dots (quantiles) of the sample to take
n_rep = 20 # number of replicates (quantiles) of each dot to create "blur"
annotation_color = "gray25"
scales = function(n) list(
labs(
y = NULL,
x = NULL,
subtitle = paste0("Sample size = ", n)
),
scale_x_continuous(limits = c(-3.5, 3.5)),
scale_y_continuous(breaks = NULL)
)
mcse_dotplot = function(n = 500, annotate_mcse = TRUE) {
set.seed(1234)
x = rnorm(n)
df = tibble(
dot = 1:n_dot,
q_mean = quantile(x, probs = ppoints(n_dot)),
q_se = mcse_quantile(x, ppoints(n_dot), names = FALSE)
) %>%
group_by(dot) %>%
summarise(.groups = "drop",
rep = 1:n_rep,
q = qnorm(ppoints(n_rep), q_mean, q_se)
)
df %>%
ggplot(aes(y = 0, x = q, group = rep)) +
stat_dots(layout = "bin", alpha = 1/n_rep, color = NA) +
(if (annotate_mcse) list(
geom_line(
aes(group = NA), y = 0.14, data = df %>% filter(dot == 1, rep %in% range(rep)),
color = annotation_color
),
geom_text(
y = 0.18, vjust = 0, label = "uncertainty in\nthe 1st %ile\ndue to Monte\nCarlo error",
data = df %>% filter(dot == 1, rep == floor(n_rep/2)),
lineheight = 1,
color = annotation_color,
# fontface = "bold",
size = 3.5
)
)) +
scales(n)
}
theoretical_plot = tibble(mean = 0) %>%
ggplot(aes(y = 0, dist = dist_normal(mean, 1))) +
stat_dist_dots(color = NA, quantiles = n_dot, fill = "gray75") +
scales("infinity")
mcse_dotplot(annotate_mcse = TRUE) /
mcse_dotplot(n = 1000, annotate_mcse = FALSE) /
mcse_dotplot(n = 5000, annotate_mcse = FALSE) /
theoretical_plot +
plot_annotation(
title = "Dotplots of 50 quantiles of samples from N(0,1)",
subtitle = "Dots are 'blurred' according to the Monte Carlo error of that quantile",
)
```
## Histogram version
A histogram version based on a [suggestion from Dan Simpson](https://twitter.com/dan_p_simpson/status/1401051206009561090?s=20).
We'll bin percentiles using a rolling window based on which quantiles
are within 2 SEs of each other:
```{r mcse_histogram, fig.width = 15, fig.height = 10}
mcse_histogram = function(
n = 500,
n_max_bin = 100 # maximum number of bins (quantiles)
) {
set.seed(1234)
x = rnorm(n)
se_mult = abs(qnorm(1/n_max_bin/2))
probs = ppoints(n_max_bin, a = 0.5)
df = tibble(
q_mean = quantile(x, probs = probs),
q_se = mcse_quantile(x, probs, names = FALSE)
)
if (all(is.na(df$q_se))) {
df$q_se = 1
} else {
df$q_se[is.na(df$q_se)] = min(df$q_se, na.rm = TRUE)
}
# bin the quantiles based on which ones are within 2 SEs of each other
next_split = -Inf
bin = 0
df$bin = sapply(seq_len(nrow(df)), function(i) {
if (df$q_mean[[i]] > next_split) {
next_split <<- with(df[i,], q_mean + se_mult * q_se)
bin <<- bin + 1
}
bin
})
# Then we'll figure out non-overlapping boundaries between bins:
df_bins = df %>%
mutate(
lower = q_mean - se_mult * q_se,
upper = q_mean + se_mult * q_se,
) %>%
group_by(bin) %>%
summarise(
lower = min(lower),
upper = max(upper)
) %>%
mutate(
adj_lower = rowMeans(cbind(lag(upper), lower), na.rm = TRUE),
adj_upper = rowMeans(cbind(upper, lead(lower)), na.rm = TRUE)
)
# hack to make sure data is covered
df_bins$adj_lower[[1]] = min(x, df_bins$adj_lower[[1]])
df_bins$adj_upper[[nrow(df_bins)]] = max(x, df_bins$adj_upper[[nrow(df_bins)]]) + .Machine$double.eps
# Now we'll count how many data points in the sample are actually in these
# bins to determine (1) densities and (2) actual bin boundaries:
df_bins_densities = df_bins %>%
ungroup() %>%
mutate(
data = map2(adj_lower, adj_upper, ~ x[.x <= x & x < .y]),
n_in_bin = lengths(data),
p = n_in_bin / n(),
# suppress warnings for min/max if bin is empty
lower = suppressWarnings(sapply(data, min)),
upper = suppressWarnings(sapply(data, max))
) %>%
mutate(
# make sure bin edges touch
adj_lower = rowMeans(cbind(lag(upper), lower), na.rm = TRUE),
adj_upper = rowMeans(cbind(upper, lead(lower)), na.rm = TRUE),
density = p / (adj_upper - adj_lower)
)
tibble(
# interleave lower and upper to draw histograms edges
x = with(df_bins_densities, as.vector(rbind(adj_lower, adj_lower, adj_upper, adj_upper))),
density = with(df_bins_densities, as.vector(rbind(0, density, density, 0)))
) %>%
ggplot(aes(x = x)) +
geom_ribbon(aes(ymin = 0, ymax = density), color = "gray75") +
geom_rug(data = data.frame(x)) +
# geom_function(fun = dnorm, color = "red") +
labs(
y = NULL,
x = NULL,
subtitle = paste0("Sample size = ", n, ", max bins = ", n_max_bin)
) +
coord_cartesian(xlim = c(-3.5, 3.5)) +
scale_y_continuous(breaks = NULL)
}
row = function(n) (mcse_histogram(n) + mcse_histogram(n, 50) + mcse_histogram(n, 20))
row(500) /
row(1000) /
row(5000) /
row(10000) +
plot_annotation(
title = "Histograms of samples from N(0,1)",
subtitle = paste0("Binned to keep quantiles together if they are within 2*MCSE"),
)
```
```{r}
n = 500
n_max_bin = 100
set.seed(1234)
# x = rbeta(n, 1, 8)
x = rnorm(n)
mcse_quantile_ = function(x, probs) {
se = mcse_quantile(x, probs, names = FALSE)
ifelse(is.na(se), 0, se)
}
se_mult = 2#abs(qnorm(1/20/2))
probs = ppoints(n_max_bin, a = 0.9)
qs = quantile(x, probs = probs)
ses = mcse_quantile_(x, probs)
current_prob = probs[[1]]
current_q = qs[[1]]
current_se = ses[[1]]
current_n = 1
selected_qs = double()
selected_ses = double()
for (i in seq_along(probs)[-1]) {
# diff_se = sqrt(current_se^2 + ses[[i]]^2)
# if ((qs[[i]] - current_q)/diff_se > se_mult) {
if (current_q + se_mult*current_se < qs[[i]] - se_mult*ses[[i]]) {
# new split
new_i = length(selected_qs) + 1
selected_qs[[new_i]] = current_q
selected_ses[[new_i]] = current_se
current_prob = probs[[i]]
current_q = qs[[i]]
current_se = ses[[i]]
current_n = 1
} else {
# no split => merge quantiles and calculate new se
current_prob = (current_prob * current_n + probs[[i]]) / (current_n + 1)
current_q = quantile(x, current_prob)
current_se = mcse_quantile_(x, current_prob)
current_n = current_n + 1
}
}
# final split
new_i = length(selected_qs) + 1
selected_qs[[new_i]] = current_q
selected_ses[[new_i]] = current_se
# Then we'll figure out non-overlapping boundaries between bins:
lower = selected_qs - se_mult * selected_ses
upper = selected_qs + se_mult * selected_ses
adj_lower = rowMeans(cbind(lag(upper), lower), na.rm = TRUE)
adj_upper = rowMeans(cbind(upper, lead(lower)), na.rm = TRUE)
# hack to make sure data is covered
adj_lower[[1]] = min(x, adj_lower)
adj_upper[[length(adj_upper)]] = max(x, adj_upper) + .Machine$double.eps * 2
# Now we'll count how many data points in the sample are actually in these
# bins to determine (1) densities and (2) actual bin boundaries:
data_in_bin = map2(adj_lower, adj_upper, ~ x[.x <= x & x < .y])
n_in_bin = lengths(data_in_bin)
# suppress warnings for min/max if bin is empty
lower = suppressWarnings(sapply(data_in_bin, min))
upper = suppressWarnings(sapply(data_in_bin, max))
# make sure bin edges touch
adj_lower = rowMeans(cbind(lag(upper), lower), na.rm = TRUE)
adj_upper = rowMeans(cbind(upper, lead(lower)), na.rm = TRUE)
# make sure bin edges are finite
adj_lower = ifelse(adj_lower == -Inf, min(x), adj_lower)
adj_upper = ifelse(adj_lower == Inf, max(x), adj_upper)
density = n_in_bin / (adj_upper - adj_lower) / n
hist_plot = tibble(
# interleave lower and upper to draw histograms edges
x = as.vector(rbind(adj_lower, adj_lower, adj_upper, adj_upper)),
density = as.vector(rbind(0, density, density, 0))
# x = as.vector(rbind(adj_lower, adj_upper)),
# density = as.vector(rbind(density, density))
) %>%
ggplot(aes(x = x)) +
geom_ribbon(aes(ymin = 0, ymax = density), color = "gray75") +
geom_rug(data = data.frame(x)) +
# geom_function(fun = ~ dbeta(.x, 1, 8), color = "red") +
geom_function(fun = dnorm, color = "red") +
labs(
y = NULL,
x = NULL,
subtitle = paste0("Sample size = ", n, ", max bins = ", n_max_bin)
) +
coord_cartesian(xlim = c(-3.5, 3.5)) +
# coord_cartesian(xlim = c(0, 1)) +
scale_y_continuous(breaks = NULL)
q_plot = tibble(
i = seq_along(selected_qs),
q = selected_qs,
se = selected_ses,
) %>%
ggplot(aes(x = q)) +
geom_segment(aes(y = 0, yend = max(i) + 1, xend = q), color = "gray75") +
geom_linerange(aes(y = i, xmin = q - se_mult*se, xmax = q + se_mult*se), size = 0.5) +
geom_rug(data = data.frame(q = qs)) +
coord_cartesian(xlim = c(-3.5, 3.5))
# coord_cartesian(xlim = c(0, 1))
hist_plot / q_plot
```