The $summary() method runs summarise_draws() from the posterior package and returns the output. For MCMC, only post-warmup draws are included in the summary.

There is also a $print() method that prints the same summary stats but removes the extra formatting used for printing tibbles and returns the fitted model object itself. The $print() method may also be faster than $summary() because it is designed to only compute the summary statistics for the variables that will actually fit in the printed output whereas $summary() will compute them for all of the specified variables in order to be able to return them to the user. See Examples.

summary(variables = NULL, ...)

Arguments

variables

(character vector) The variables to include.

...

Optional arguments to pass to posterior::summarise_draws().

Value

The $summary() method returns the tibble data frame created by posterior::summarise_draws().

The $print() method returns the fitted model object itself (invisibly), which is the standard behavior for print methods in R.

Examples

# \dontrun{
fit <- cmdstanr_example("logistic")
fit$summary()
#> # A tibble: 105 × 10
#>    variable      mean  median     sd    mad       q5      q95  rhat ess_bulk
#>    <chr>        <dbl>   <dbl>  <dbl>  <dbl>    <dbl>    <dbl> <dbl>    <dbl>
#>  1 lp__       -66.0   -65.7   1.48   1.23   -68.9    -64.3     1.00    1956.
#>  2 alpha        0.382   0.381 0.221  0.222    0.0263   0.744   1.00    3982.
#>  3 beta[1]     -0.665  -0.661 0.252  0.253   -1.09    -0.264   1.00    4131.
#>  4 beta[2]     -0.274  -0.280 0.226  0.226   -0.640    0.0976  1.00    3816.
#>  5 beta[3]      0.675   0.673 0.268  0.265    0.238    1.12    1.00    3963.
#>  6 log_lik[1]  -0.514  -0.508 0.0987 0.0980  -0.686   -0.364   1.00    3980.
#>  7 log_lik[2]  -0.408  -0.385 0.153  0.142   -0.690   -0.197   1.00    4263.
#>  8 log_lik[3]  -0.498  -0.467 0.217  0.204   -0.889   -0.206   1.00    3904.
#>  9 log_lik[4]  -0.453  -0.436 0.152  0.148   -0.732   -0.236   1.00    3847.
#> 10 log_lik[5]  -1.18   -1.16  0.285  0.281   -1.68    -0.749   1.00    4407.
#> # ℹ 95 more rows
#> # ℹ 1 more variable: ess_tail <dbl>
fit$print()
#>    variable   mean median   sd  mad     q5    q95 rhat ess_bulk ess_tail
#>  lp__       -66.00 -65.68 1.48 1.23 -68.89 -64.30 1.00     1956     2665
#>  alpha        0.38   0.38 0.22 0.22   0.03   0.74 1.00     3981     3124
#>  beta[1]     -0.66  -0.66 0.25 0.25  -1.09  -0.26 1.00     4131     3206
#>  beta[2]     -0.27  -0.28 0.23 0.23  -0.64   0.10 1.00     3815     2736
#>  beta[3]      0.67   0.67 0.27 0.26   0.24   1.12 1.00     3963     3257
#>  log_lik[1]  -0.51  -0.51 0.10 0.10  -0.69  -0.36 1.00     3979     2841
#>  log_lik[2]  -0.41  -0.39 0.15 0.14  -0.69  -0.20 1.00     4263     3051
#>  log_lik[3]  -0.50  -0.47 0.22 0.20  -0.89  -0.21 1.00     3904     3141
#>  log_lik[4]  -0.45  -0.44 0.15 0.15  -0.73  -0.24 1.00     3847     2904
#>  log_lik[5]  -1.18  -1.16 0.28 0.28  -1.68  -0.75 1.00     4406     3095
#> 
#>  # showing 10 of 105 rows (change via 'max_rows' argument or 'cmdstanr_max_rows' option)
fit$print(max_rows = 2) # same as print(fit, max_rows = 2)
#>  variable   mean median   sd  mad     q5    q95 rhat ess_bulk ess_tail
#>     lp__  -66.00 -65.68 1.48 1.23 -68.89 -64.30 1.00     1956     2665
#>     alpha   0.38   0.38 0.22 0.22   0.03   0.74 1.00     3981     3124
#> 
#>  # showing 2 of 105 rows (change via 'max_rows' argument or 'cmdstanr_max_rows' option)

# include only certain variables
fit$summary("beta")
#> # A tibble: 3 × 10
#>   variable   mean median    sd   mad     q5     q95  rhat ess_bulk ess_tail
#>   <chr>     <dbl>  <dbl> <dbl> <dbl>  <dbl>   <dbl> <dbl>    <dbl>    <dbl>
#> 1 beta[1]  -0.665 -0.661 0.252 0.253 -1.09  -0.264   1.00    4131.    3207.
#> 2 beta[2]  -0.274 -0.280 0.226 0.226 -0.640  0.0976  1.00    3816.    2736.
#> 3 beta[3]   0.675  0.673 0.268 0.265  0.238  1.12    1.00    3963.    3257.
fit$print(c("alpha", "beta[2]"))
#>  variable  mean median   sd  mad    q5  q95 rhat ess_bulk ess_tail
#>   alpha    0.38   0.38 0.22 0.22  0.03 0.74 1.00     3981     3124
#>   beta[2] -0.27  -0.28 0.23 0.23 -0.64 0.10 1.00     3815     2736

# include all variables but only certain summaries
fit$summary(NULL, c("mean", "sd"))
#> # A tibble: 105 × 3
#>    variable      mean     sd
#>    <chr>        <dbl>  <dbl>
#>  1 lp__       -66.0   1.48  
#>  2 alpha        0.382 0.221 
#>  3 beta[1]     -0.665 0.252 
#>  4 beta[2]     -0.274 0.226 
#>  5 beta[3]      0.675 0.268 
#>  6 log_lik[1]  -0.514 0.0987
#>  7 log_lik[2]  -0.408 0.153 
#>  8 log_lik[3]  -0.498 0.217 
#>  9 log_lik[4]  -0.453 0.152 
#> 10 log_lik[5]  -1.18  0.285 
#> # ℹ 95 more rows

# can use functions created from formulas
# for example, calculate Pr(beta > 0)
fit$summary("beta", prob_gt_0 = ~ mean(. > 0))
#> # A tibble: 3 × 2
#>   variable prob_gt_0
#>   <chr>        <dbl>
#> 1 beta[1]    0.00325
#> 2 beta[2]    0.114  
#> 3 beta[3]    0.997  

# can combine user-specified functions with
# the default summary functions
fit$summary(variables = c("alpha", "beta"),
  posterior::default_summary_measures()[1:4],
  quantiles = ~ quantile2(., probs = c(0.025, 0.975)),
  posterior::default_convergence_measures()
  )
#> # A tibble: 4 × 10
#>   variable   mean median    sd   mad    q2.5  q97.5  rhat ess_bulk ess_tail
#>   <chr>     <dbl>  <dbl> <dbl> <dbl>   <dbl>  <dbl> <dbl>    <dbl>    <dbl>
#> 1 alpha     0.382  0.381 0.221 0.222 -0.0413  0.821  1.00    3982.    3124.
#> 2 beta[1]  -0.665 -0.661 0.252 0.253 -1.18   -0.185  1.00    4131.    3207.
#> 3 beta[2]  -0.274 -0.280 0.226 0.226 -0.715   0.168  1.00    3816.    2736.
#> 4 beta[3]   0.675  0.673 0.268 0.265  0.170   1.20   1.00    3963.    3257.

# the functions need to calculate the appropriate
# value for a matrix input
fit$summary(variables = "alpha", dim)
#> # A tibble: 1 × 3
#>   variable dim.1 dim.2
#>   <chr>    <int> <int>
#> 1 alpha     1000     4

# the usual [stats::var()] is therefore not directly suitable as it
# will produce a covariance matrix unless the data is converted to a vector
fit$print(c("alpha", "beta"), var2 = ~var(as.vector(.x)))
#>  variable var2
#>   alpha   0.05
#>   beta[1] 0.06
#>   beta[2] 0.05
#>   beta[3] 0.07

# }