@@ -225,65 +225,73 @@ mcmc_recover_scatter <-
225
225
size = 3 ,
226
226
alpha = 1 ) {
227
227
228
- check_ignored_arguments(... )
229
- x <- merge_chains(prepare_mcmc_array(x ))
228
+ check_ignored_arguments(... )
229
+ x <- merge_chains(prepare_mcmc_array(x ))
230
230
231
- stopifnot(
232
- is.numeric(true ),
233
- ncol(x ) == length(true ),
234
- length(batch ) == length(true )
235
- )
236
- all_separate <- length(unique(batch )) == length(true )
237
- point_est <- match.arg(point_est )
238
- plot_data <- data.frame (
239
- Parameter = colnames(x ),
240
- Point = apply(x , 2 , point_est ),
241
- True = true
242
- )
243
- if (! all_separate ) {
244
- plot_data $ Batch <- factor (batch , levels = unique(batch ))
245
- } else {
246
- plot_data $ Batch <-
247
- factor (colnames(x ), levels = colnames(x )[as.integer(as.factor(batch ))])
248
- }
231
+ stopifnot(
232
+ is.numeric(true ),
233
+ ncol(x ) == length(true ),
234
+ length(batch ) == length(true )
235
+ )
249
236
250
- facet_args [[" facets" ]] <- ~ Batch
251
- if (is.null(facet_args [[" strip.position" ]]))
252
- facet_args [[" strip.position" ]] <- " top"
253
- if (is.null(facet_args [[" scales" ]]))
254
- facet_args [[" scales" ]] <- " free"
237
+ one_true_per_batch <- length(unique(batch )) == length(true )
238
+ one_batch <- length(unique(batch )) == 1
255
239
256
- graph <- ggplot(plot_data , aes_(x = ~ True , y = ~ Point )) +
257
- geom_abline(
258
- slope = 1 ,
259
- intercept = 0 ,
260
- linetype = 2 ,
261
- color = " black"
262
- ) +
263
- geom_point(
264
- shape = 21 ,
265
- color = get_color(" mh" ),
266
- fill = get_color(" m" ),
267
- size = size ,
268
- alpha = alpha
269
- ) +
270
- do.call(" facet_wrap" , facet_args ) +
271
- labs(y = " Estimated" , x = " True" ) +
272
- bayesplot_theme_get()
240
+ point_est <- match.arg(point_est )
241
+ plot_data <- data.frame (
242
+ Parameter = colnames(x ),
243
+ Point = apply(x , 2 , point_est ),
244
+ True = true
245
+ )
273
246
274
- if (length(unique( batch )) == 1 ) {
275
- g <- ggplot_build( graph )
276
- xylim <- g $ layout $ panel_ranges [[ 1 ]]
277
- xylim <- range( xylim $ y.range , xylim $ x.range )
278
- graph <- graph + coord_fixed( xlim = xylim , ylim = xylim )
279
- }
247
+ if (! one_true_per_batch ) {
248
+ plot_data $ Batch <- factor ( batch , levels = unique( batch ) )
249
+ } else {
250
+ plot_data $ Batch <-
251
+ factor (colnames( x ), levels = colnames( x )[as.integer(as.factor( batch ))] )
252
+ }
280
253
281
- if (all_separate )
282
- return (graph )
254
+ facet_args [[" facets" ]] <- " Batch"
255
+ facet_args [[" strip.position" ]] <- facet_args [[" strip.position" ]] %|| % " top"
256
+ facet_args [[" scales" ]] <- facet_args [[" scales" ]] %|| % " free"
283
257
284
- graph + facet_text(FALSE )
258
+ # To ensure that the x and y scales have the same range, find the min and max
259
+ # value on each coordinate. plot them invisibly with geom_blank() later on.
260
+ corners <- plot_data %> %
261
+ group_by(.data $ Batch ) %> %
262
+ summarise(
263
+ min = min(pmin(.data $ Point , .data $ True )),
264
+ max = max(pmax(.data $ Point , .data $ True ))
265
+ )
266
+
267
+ graph <-
268
+ ggplot(plot_data , aes_(x = ~ True , y = ~ Point )) +
269
+ geom_abline(
270
+ slope = 1 ,
271
+ intercept = 0 ,
272
+ linetype = 2 ,
273
+ color = " black"
274
+ ) +
275
+ geom_point(
276
+ shape = 21 ,
277
+ color = get_color(" mh" ),
278
+ fill = get_color(" m" ),
279
+ size = size ,
280
+ alpha = alpha
281
+ ) +
282
+ geom_blank(aes(x = min , y = min ), data = corners ) +
283
+ geom_blank(aes(x = max , y = max ), data = corners ) +
284
+ do.call(" facet_wrap" , facet_args ) +
285
+ labs(x = " True" , y = " Estimated" ) +
286
+ bayesplot_theme_get()
287
+
288
+ if (one_batch ) {
289
+ graph <- graph + facet_text(FALSE )
285
290
}
286
291
292
+ graph
293
+ }
294
+
287
295
288
296
# ' @rdname MCMC-recover
289
297
# ' @export
0 commit comments