1
- # ' Trace plots (time series plot) of MCMC draws
1
+ # ' Trace plots of MCMC draws
2
2
# '
3
3
# ' Trace plot (or traceplot) of MCMC draws. See the **Plot Descriptions**
4
4
# ' section, below, for details.
12
12
# ' @template args-facet_args
13
13
# ' @param ... Currently ignored.
14
14
# ' @param size An optional value to override the default line size
15
- # ' (`mcmc_trace()`) or the default point size
16
- # ' (`mcmc_trace_highlight()`).
15
+ # ' for `mcmc_trace()` or the default point size for `mcmc_trace_highlight()`.
17
16
# ' @param alpha For `mcmc_trace_highlight()`, passed to
18
17
# ' [ggplot2::geom_point()] to control the transparency of the points
19
18
# ' for the chains not highlighted.
27
26
# ' if `n_warmup` is also set to a positive value.
28
27
# ' @param window An integer vector of length two specifying the limits of a
29
28
# ' range of iterations to display.
30
- # ' @param np For models fit using [NUTS] (more generally, any [symplectic
31
- # ' integrator](https://en.wikipedia.org/wiki/Symplectic_integrator)), an
32
- # ' optional data frame providing NUTS diagnostic information. The data frame
33
- # ' should be the object returned by [nuts_params()] or one with the same
29
+ # ' @param np For models fit using [NUTS] (more generally, any
30
+ # ' [symplectic integrator](https://en.wikipedia.org/wiki/Symplectic_integrator)),
31
+ # ' an optional data frame providing NUTS diagnostic information. The data
32
+ # ' frame should be the object returned by [nuts_params()] or one with the same
34
33
# ' structure. If `np` is specified then tick marks are added to the bottom of
35
34
# ' the trace plot indicating within which iterations there was a divergence
36
35
# ' (if there were any). See the end of the **Examples** section, below.
40
39
# ' @param divergences Deprecated. Use the `np` argument instead.
41
40
# '
42
41
# ' @template return-ggplot-or-data
42
+ # ' @return `mcmc_trace_data()` returns the data for the trace *and* rank plots
43
+ # ' in the same data frame.
43
44
# '
44
45
# ' @section Plot Descriptions:
45
46
# ' \describe{
@@ -208,10 +209,9 @@ mcmc_trace <-
208
209
}
209
210
210
211
# ' @rdname MCMC-traces
212
+ # ' @export
211
213
# ' @param highlight For `mcmc_trace_highlight()`, an integer specifying one
212
214
# ' of the chains that will be more visible than the others in the plot.
213
- # ' @export
214
- # ' @md
215
215
mcmc_trace_highlight <- function (x ,
216
216
pars = character (),
217
217
regex_pars = character (),
@@ -242,13 +242,13 @@ mcmc_trace_highlight <- function(x,
242
242
243
243
244
244
# ' @rdname MCMC-traces
245
+ # ' @export
245
246
# ' @param div_color,div_size,div_alpha Optional arguments to the
246
247
# ' `trace_style_np()` helper function that are eventually passed to
247
248
# ' [ggplot2::geom_rug()] if the `np` argument is also specified. They control
248
249
# ' the color, size, and transparency specifications for showing divergences in
249
250
# ' the plot. The default values are displayed in the **Usage** section above.
250
- # ' @export
251
- # ' @md
251
+ # '
252
252
trace_style_np <- function (div_color = " red" , div_size = 0.25 , div_alpha = 1 ) {
253
253
stopifnot(
254
254
is.character(div_color ),
@@ -466,10 +466,12 @@ mcmc_trace_data <- function(x,
466
466
data $ n_parameters <- num_params(data )
467
467
data <- rlang :: set_names(data , tolower )
468
468
469
+ first_cols <- syms(c(" parameter" , " value" , " value_rank" ))
469
470
data <- data %> %
470
471
group_by(.data $ parameter ) %> %
471
472
mutate(value_rank = dplyr :: row_number(.data $ value )) %> %
472
- ungroup()
473
+ ungroup() %> %
474
+ select(!!! first_cols , dplyr :: everything())
473
475
474
476
data $ highlight <- if (! is.null(highlight )) {
475
477
data $ chain == highlight
@@ -478,8 +480,7 @@ mcmc_trace_data <- function(x,
478
480
}
479
481
480
482
data $ warmup <- data $ iteration < = n_warmup
481
- data $ iteration <- data $ iteration + iter1
482
-
483
+ data $ iteration <- data $ iteration + as.integer(iter1 )
483
484
tibble :: as_tibble(data )
484
485
}
485
486
0 commit comments