forked from stan-dev/bayesplot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcmc-recover.R
347 lines (318 loc) · 10.5 KB
/
mcmc-recover.R
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
338
339
340
341
342
343
344
345
346
347
#' Compare MCMC estimates to "true" parameter values
#'
#' Plots comparing MCMC estimates to "true" parameter values. Before fitting a
#' model to real data it is useful to simulate data according to the model using
#' known (fixed) parameter values and to check that these "true" parameter
#' values are (approximately) recovered by fitting the model to the simulated
#' data. See the **Plot Descriptions** section, below, for details on the
#' available plots.
#'
#' @name MCMC-recover
#' @family MCMC
#'
#' @template args-mcmc-x
#' @template args-facet_args
#' @param true A numeric vector of "true" values of the parameters in `x`.
#' There should be one value in `true` for each parameter included in
#' `x` and the order of the parameters in `true` should be the same
#' as the order of the parameters in `x`.
#' @param batch Optionally, a vector-like object (numeric, character, integer,
#' factor) used to split the parameters into batches. If `batch` is
#' specified, it must have the same length as `true` and be in the same
#' order as `true`. Parameters in the same batch will be grouped together
#' in the same facet in the plot (see the **Examples** section, below).
#' The default is to group all parameters together into a single batch.
#' Changing the default is most useful when parameters are on very different
#' scales, in which case `batch` can be used to group them into batches
#' within which it makes sense to use the same y-axis.
#' @param ... Currently unused.
#' @param prob The probability mass to include in the inner interval. The
#' default is `0.5` (50% interval).
#' @param prob_outer The probability mass to include in the outer interval. The
#' default is `0.9` (90% interval).
#' @param point_est The point estimate to show. Either `"median"` (the
#' default), `"mean"`, or `"none"`.
#' @param size,alpha Passed to [ggplot2::geom_point()] to control the
#' appearance of plotted points.
#'
#' @template return-ggplot
#'
#' @section Plot Descriptions:
#' \describe{
#' \item{`mcmc_recover_intervals()`}{
#' Central intervals and point estimates computed from MCMC draws, with
#' "true" values plotted using a different shape.
#' }
#' \item{`mcmc_recover_scatter()`}{
#' Scatterplot of posterior means (or medians) against "true" values.
#' }
#' \item{`mcmc_recover_hist()`}{
#' Histograms of the draws for each parameter with the "true" value overlaid
#' as a vertical line.
#' }
#' }
#'
#' @examples
#' \dontrun{
#' library(rstanarm)
#' alpha <- 1; beta <- rnorm(10, 0, 3); sigma <- 2
#' X <- matrix(rnorm(1000), 100, 10)
#' y <- rnorm(100, mean = c(alpha + X %*% beta), sd = sigma)
#' fit <- stan_glm(y ~ ., data = data.frame(y, X), refresh = 0)
#' draws <- as.matrix(fit)
#' print(colnames(draws))
#' true <- c(alpha, beta, sigma)
#'
#' mcmc_recover_intervals(draws, true)
#'
#' # put the coefficients on X into the same batch
#' mcmc_recover_intervals(draws, true, batch = c(1, rep(2, 10), 1))
#' # equivalent
#' mcmc_recover_intervals(draws, true, batch = grepl("X", colnames(draws)))
#' # same but facets stacked vertically
#' mcmc_recover_intervals(draws, true,
#' batch = grepl("X", colnames(draws)),
#' facet_args = list(ncol = 1),
#' size = 3)
#'
#' # each parameter in its own facet
#' mcmc_recover_intervals(draws, true, batch = 1:ncol(draws))
#' # same but in a different order
#' mcmc_recover_intervals(draws, true, batch = c(1, 3, 4, 2, 5:12))
#' # present as bias by centering with true values
#' mcmc_recover_intervals(sweep(draws, 2, true), rep(0, ncol(draws))) + hline_0()
#'
#'
#' # scatterplot of posterior means vs true values
#' mcmc_recover_scatter(draws, true, point_est = "mean")
#'
#'
#' # histograms of parameter draws with true value added as vertical line
#' color_scheme_set("brightblue")
#' mcmc_recover_hist(draws[, 1:4], true[1:4])
#' }
#'
NULL
#' @rdname MCMC-recover
#' @export
mcmc_recover_intervals <-
function(x,
true,
batch = rep(1, length(true)),
...,
facet_args = list(),
prob = 0.5,
prob_outer = 0.9,
point_est = c("median", "mean", "none"),
size = 4,
alpha = 1) {
check_ignored_arguments(...)
x <- merge_chains(prepare_mcmc_array(x))
stopifnot(
is.numeric(true),
ncol(x) == length(true),
length(batch) == length(true),
prob_outer >= prob,
prob > 0,
prob_outer <= 1
)
all_separate <- length(unique(batch)) == length(true)
point_est <- match.arg(point_est)
if (point_est == "none") {
point_est <- NULL
}
alpha1 <- (1 - prob) / 2
alpha2 <- (1 - prob_outer) / 2
probs <- sort(c(alpha1, 1 - alpha1, alpha2, 1 - alpha2))
intervals <- t(apply(x, 2, quantile, probs = probs))
colnames(intervals) <- c("ll", "l", "u", "uu")
plot_data <- data.frame(
Parameter = rownames(intervals),
True = true,
Point = apply(x, 2, point_est %||% function(x) NA),
intervals
)
if (!all_separate) {
plot_data$Batch <- factor(batch, levels = unique(batch))
} else {
plot_data$Batch <-
factor(rownames(intervals),
levels = rownames(intervals)[as.integer(as.factor(batch))])
}
facet_args[["facets"]] <- "Batch"
facet_args[["strip.position"]] <- facet_args[["strip.position"]] %||% "top"
facet_args[["scales"]] <- facet_args[["scales"]] %||% "free"
plot_caption <- paste0("Showing ", round(prob * 100, 1), "% and ",
round(prob_outer * 100, 1), "% intervals")
graph <- ggplot(plot_data, aes_(x = ~ Parameter, xend = ~ Parameter)) +
geom_segment(
aes_(y = ~ ll, yend = ~ uu, color = "Estimated"),
lineend = "round",
show.legend = FALSE
) +
geom_segment(
aes_(y = ~ l, yend = ~ u, color = "Estimated"),
size = 2,
lineend = "round",
show.legend = FALSE
) +
bayesplot_theme_get()
if (!is.null(point_est)) {
graph <- graph +
geom_point(
aes_(y = ~ Point, shape = "Estimated",
color = "Estimated", fill = "Estimated"),
size = size
)
}
graph <- graph +
geom_point(
aes_(y = ~ True, shape = "True",
color = "True", fill = "True"),
size = size,
alpha = alpha
) +
scale_color_manual(
name = "",
values = c(Estimated = get_color("d"), True = get_color("dh")),
guide = if (is.null(point_est)) "none" else "legend"
) +
scale_fill_manual(
name = "",
values = c(Estimated = get_color("d"), True = get_color("l"))
) +
scale_shape_manual(
name = "",
values = c(Estimated = 21, True = 24)
) +
do.call("facet_wrap", facet_args) +
labs(y = "Value", x = "Parameter", subtitle = plot_caption) +
theme(plot.caption = element_text(hjust = 0)) +
xaxis_title(FALSE) +
yaxis_title(FALSE)
if (all_separate) {
return(
graph +
theme(axis.line.x = element_blank()) +
xaxis_ticks(FALSE) +
xaxis_text(FALSE)
)
}
graph +
xaxis_text(face = "bold") +
facet_text(FALSE)
}
#' @rdname MCMC-recover
#' @export
mcmc_recover_scatter <-
function(x,
true,
batch = rep(1, length(true)),
...,
facet_args = list(),
point_est = c("median", "mean"),
size = 3,
alpha = 1) {
check_ignored_arguments(...)
x <- merge_chains(prepare_mcmc_array(x))
stopifnot(
is.numeric(true),
ncol(x) == length(true),
length(batch) == length(true)
)
one_true_per_batch <- length(unique(batch)) == length(true)
one_batch <- length(unique(batch)) == 1
point_est <- match.arg(point_est)
plot_data <- data.frame(
Parameter = colnames(x),
Point = apply(x, 2, point_est),
True = true
)
if (!one_true_per_batch) {
plot_data$Batch <- factor(batch, levels = unique(batch))
} else {
plot_data$Batch <-
factor(colnames(x), levels = colnames(x)[as.integer(as.factor(batch))])
}
facet_args[["facets"]] <- "Batch"
facet_args[["strip.position"]] <- facet_args[["strip.position"]] %||% "top"
facet_args[["scales"]] <- facet_args[["scales"]] %||% "free"
# To ensure that the x and y scales have the same range, find the min and max
# value on each coordinate. plot them invisibly with geom_blank() later on.
corners <- plot_data %>%
group_by(.data$Batch) %>%
summarise(
min = min(pmin(.data$Point, .data$True)),
max = max(pmax(.data$Point, .data$True))
)
graph <-
ggplot(plot_data, aes_(x = ~ True, y = ~ Point)) +
geom_abline(
slope = 1,
intercept = 0,
linetype = 2,
color = "black"
) +
geom_point(
shape = 21,
color = get_color("mh"),
fill = get_color("m"),
size = size,
alpha = alpha
) +
geom_blank(aes(x = min, y = min), data = corners) +
geom_blank(aes(x = max, y = max), data = corners) +
do.call("facet_wrap", facet_args) +
labs(x = "True", y = "Estimated") +
bayesplot_theme_get()
if (one_batch) {
graph <- graph + facet_text(FALSE)
}
graph
}
#' @rdname MCMC-recover
#' @export
#' @template args-hist
mcmc_recover_hist <-
function(x,
true,
...,
facet_args = list(),
binwidth = NULL,
breaks = NULL) {
check_ignored_arguments(...)
x <- merge_chains(prepare_mcmc_array(x))
stopifnot(
is.numeric(true),
ncol(x) == length(true)
)
vline_data <- data.frame(Parameter = colnames(x), True = true)
hist_data <- melt_mcmc(x)[, -1]
facet_args[["facets"]] <- "Parameter"
facet_args[["scales"]] <- facet_args[["scales"]] %||% "free"
ggplot() +
geom_histogram(
aes_(x = ~ Value, fill = "Estimated"),
data = hist_data,
color = get_color("lh"),
size = .25,
binwidth = binwidth,
breaks = breaks
) +
geom_vline(
aes_(xintercept = ~ True, color = "True"),
data = vline_data,
size = 1.5
) +
do.call("facet_wrap", facet_args) +
scale_fill_manual("", values = get_color("l")) +
scale_color_manual("", values = get_color("dh")) +
guides(color = guide_legend(), fill = guide_legend(order = 1)) +
dont_expand_y_axis() +
bayesplot_theme_get() +
reduce_legend_spacing(0.25) +
xaxis_title(FALSE) +
yaxis_text(FALSE) +
yaxis_ticks(FALSE) +
yaxis_title(FALSE)
}