Extract sampler diagnostics after MCMC — fit-method-sampler_diagnostics • cmdstanr
Description: Extract the values of sampler diagnostics for each iteration and chain of MCMC. To instead get summaries of these diagnostics and associated warning messages use the $diagnostic_summary() method.

# Extract sampler diagnostics after MCMC

Source: `R/fit.R`
`fit-method-sampler_diagnostics.Rd`

Extract the values of sampler diagnostics for each iteration and chain of MCMC. To instead get summaries of these diagnostics and associated warning messages use the `$diagnostic_summary()` method.

    sampler_diagnostics(
      inc_warmup = FALSE,
      format = getOption("cmdstanr_draws_format", "draws_array")
    )

## Arguments

inc_warmup

  (logical) Should warmup draws be included? Defaults to `FALSE`.

format

  (string) The draws format to return. See draws for details.

## Value

Depends on `format`, but the default is a 3-D `draws_array` object (iteration x chain x variable). The variables for Stan's default MCMC algorithm are `"accept_stat__"`, `"stepsize__"`, `"treedepth__"`, `"n_leapfrog__"`, `"divergent__"`, `"energy__"`.

## See also

`CmdStanMCMC`

## Examples

    # \dontrun{
    fit <- cmdstanr_example("logistic")
    sampler_diagnostics <- fit$sampler_diagnostics()
    str(sampler_diagnostics)
    #>  'draws_array' num [1:1000, 1:4, 1:6] 1 3 2 2 2 3 2 2 3 2 ...
    #>  - attr(*, "dimnames")=List of 3
    #>   ..$ iteration: chr [1:1000] "1" "2" "3" "4" ...
    #>   ..$ chain    : chr [1:4] "1" "2" "3" "4"
    #>   ..$ variable : chr [1:6] "treedepth__" "divergent__" "energy__" "accept_stat__" ...

    library(posterior)
    as_draws_df(sampler_diagnostics)
    #> # A draws_df: 1000 iterations, 4 chains, and 6 variables
    #>    treedepth__ divergent__ energy__ accept_stat__ stepsize__ n_leapfrog__
    #> 1            1           0       66          0.91       0.67            3
    #> 2            3           0       66          0.87       0.67            7
    #> 3            2           0       72          0.65       0.67            3
    #> 4            2           0       70          1.00       0.67            3
    #> 5            2           0       66          1.00       0.67            3
    #> 6            3           0       65          0.93       0.67            7
    #> 7            2           0       69          0.70       0.67            3
    #> 8            2           0       70          0.99       0.67            3
    #> 9            3           0       66          0.98       0.67            7
    #> 10           2           0       67          0.90       0.67            3
    #> # ... with 3990 more draws
    #> # ... hidden reserved variables {'.chain', '.iteration', '.draw'}

    # or specify format to get a data frame instead of calling as_draws_df
    fit$sampler_diagnostics(format = "df")
    #> # A draws_df: 1000 iterations, 4 chains, and 6 variables
    #>    treedepth__ divergent__ energy__ accept_stat__ stepsize__ n_leapfrog__
    #> 1            1           0       66          0.91       0.67            3
    #> 2            3           0       66          0.87       0.67            7
    #> 3            2           0       72          0.65       0.67            3
    #> 4            2           0       70          1.00       0.67            3
    #> 5            2           0       66          1.00       0.67            3
    #> 6            3           0       65          0.93       0.67            7
    #> 7            2           0       69          0.70       0.67            3
    #> 8            2           0       70          0.99       0.67            3
    #> 9            3           0       66          0.98       0.67            7
    #> 10           2           0       67          0.90       0.67            3
    #> # ... with 3990 more draws
    #> # ... hidden reserved variables {'.chain', '.iteration', '.draw'}
    # }