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.6   1.45   1.26   -68.7    -64.3     1.00    1922.
#>  2 alpha        0.378   0.373 0.221  0.218    0.0220   0.750   1.00    4329.
#>  3 beta[1]     -0.669  -0.660 0.252  0.257   -1.09    -0.264   1.00    3807.
#>  4 beta[2]     -0.279  -0.273 0.223  0.223   -0.649    0.0786  1.00    3937.
#>  5 beta[3]      0.677   0.669 0.267  0.257    0.248    1.13    1.00    3914.
#>  6 log_lik[1]  -0.516  -0.510 0.0985 0.0955  -0.690   -0.361   1.00    4145.
#>  7 log_lik[2]  -0.404  -0.386 0.148  0.137   -0.677   -0.198   1.00    4343.
#>  8 log_lik[3]  -0.502  -0.466 0.215  0.206   -0.908   -0.213   1.00    4197.
#>  9 log_lik[4]  -0.447  -0.432 0.150  0.147   -0.720   -0.233   1.00    3438.
#> 10 log_lik[5]  -1.18   -1.16  0.280  0.281   -1.68    -0.753   1.00    4471.
#> # ℹ 95 more rows
#> # ℹ 1 more variable: ess_tail <dbl>
fit$print()
#>    variable   mean median   sd  mad     q5    q95 rhat ess_bulk ess_tail
#>  lp__       -65.96 -65.63 1.45 1.26 -68.70 -64.28 1.00     1922     2690
#>  alpha        0.38   0.37 0.22 0.22   0.02   0.75 1.00     4329     2617
#>  beta[1]     -0.67  -0.66 0.25 0.26  -1.09  -0.26 1.00     3806     3246
#>  beta[2]     -0.28  -0.27 0.22 0.22  -0.65   0.08 1.00     3937     2974
#>  beta[3]      0.68   0.67 0.27 0.26   0.25   1.13 1.00     3913     3133
#>  log_lik[1]  -0.52  -0.51 0.10 0.10  -0.69  -0.36 1.00     4145     3089
#>  log_lik[2]  -0.40  -0.39 0.15 0.14  -0.68  -0.20 1.00     4342     3107
#>  log_lik[3]  -0.50  -0.47 0.22 0.21  -0.91  -0.21 1.00     4196     3253
#>  log_lik[4]  -0.45  -0.43 0.15 0.15  -0.72  -0.23 1.00     3437     2969
#>  log_lik[5]  -1.18  -1.16 0.28 0.28  -1.68  -0.75 1.00     4471     3204
#> 
#>  # 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__  -65.96 -65.63 1.45 1.26 -68.70 -64.28 1.00     1922     2690
#>     alpha   0.38   0.37 0.22 0.22   0.02   0.75 1.00     4329     2617
#> 
#>  # 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.669 -0.660 0.252 0.257 -1.09  -0.264   1.00    3807.    3246.
#> 2 beta[2]  -0.279 -0.273 0.223 0.223 -0.649  0.0786  1.00    3937.    2974.
#> 3 beta[3]   0.677  0.669 0.267 0.257  0.248  1.13    1.00    3914.    3133.
fit$print(c("alpha", "beta[2]"))
#>  variable  mean median   sd  mad    q5  q95 rhat ess_bulk ess_tail
#>   alpha    0.38   0.37 0.22 0.22  0.02 0.75 1.00     4329     2617
#>   beta[2] -0.28  -0.27 0.22 0.22 -0.65 0.08 1.00     3937     2974

# 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.45  
#>  2 alpha        0.378 0.221 
#>  3 beta[1]     -0.669 0.252 
#>  4 beta[2]     -0.279 0.223 
#>  5 beta[3]      0.677 0.267 
#>  6 log_lik[1]  -0.516 0.0985
#>  7 log_lik[2]  -0.404 0.148 
#>  8 log_lik[3]  -0.502 0.215 
#>  9 log_lik[4]  -0.447 0.150 
#> 10 log_lik[5]  -1.18  0.280 
#> # ℹ 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.0015
#> 2 beta[2]     0.102 
#> 3 beta[3]     0.993 

# 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.378  0.373 0.221 0.218 -0.0434  0.812  1.00    4329.    2618.
#> 2 beta[1]  -0.669 -0.660 0.252 0.257 -1.17   -0.191  1.00    3807.    3246.
#> 3 beta[2]  -0.279 -0.273 0.223 0.223 -0.728   0.143  1.00    3937.    2974.
#> 4 beta[3]   0.677  0.669 0.267 0.257  0.164   1.22   1.00    3914.    3133.

# 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

# }