Fit models for use in examples

cmdstanr_example(
  example = c("logistic", "schools", "schools_ncp"),
  method = c("sample", "optimize", "laplace", "variational", "pathfinder", "diagnose"),
  ...,
  quiet = TRUE,
  force_recompile = getOption("cmdstanr_force_recompile", default = FALSE)
)

print_example_program(example = c("logistic", "schools", "schools_ncp"))

Arguments

example

(string) The name of the example. The currently available examples are

  • "logistic": logistic regression with intercept and 3 predictors.

  • "schools": the so-called "eight schools" model, a hierarchical meta-analysis. Fitting this model will result in warnings about divergences.

  • "schools_ncp": non-centered parameterization of the "eight schools" model that fixes the problem with divergences.

To print the Stan code for a given example use print_example_program(example).

method

(string) Which fitting method should be used? The default is the "sample" method (MCMC).

...

Arguments passed to the chosen method. See the help pages for the individual methods for details.

quiet

(logical) If TRUE (the default) then fitting the model is wrapped in utils::capture.output().

force_recompile

Passed to the $compile() method.

Value

The fitted model object returned by the selected method.

Examples

# \dontrun{
print_example_program("logistic")
#> data {
#>   int<lower=0> N;
#>   int<lower=0> K;
#>   array[N] int<lower=0, upper=1> y;
#>   matrix[N, K] X;
#> }
#> parameters {
#>   real alpha;
#>   vector[K] beta;
#> }
#> model {
#>   target += normal_lpdf(alpha | 0, 1);
#>   target += normal_lpdf(beta | 0, 1);
#>   target += bernoulli_logit_glm_lpmf(y | X, alpha, beta);
#> }
#> generated quantities {
#>   vector[N] log_lik;
#>   for (n in 1 : N) {
#>     log_lik[n] = bernoulli_logit_lpmf(y[n] | alpha + X[n] * beta);
#>   }
#> }
fit_logistic_mcmc <- cmdstanr_example("logistic", chains = 2)
fit_logistic_mcmc$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__       -65.9   -65.6   1.43  1.23   -68.7    -64.3    1.00     890.
#>  2 alpha        0.377   0.375 0.220 0.220    0.0153   0.743  1.00    2007.
#>  3 beta[1]     -0.670  -0.663 0.247 0.244   -1.10    -0.273  1.00    1880.
#>  4 beta[2]     -0.268  -0.269 0.223 0.218   -0.637    0.106  1.00    1834.
#>  5 beta[3]      0.684   0.681 0.271 0.271    0.243    1.14   1.00    2163.
#>  6 log_lik[1]  -0.516  -0.511 0.101 0.0973  -0.694   -0.363  1.00    2030.
#>  7 log_lik[2]  -0.398  -0.38  0.143 0.141   -0.661   -0.204  1.00    2280.
#>  8 log_lik[3]  -0.493  -0.463 0.216 0.196   -0.883   -0.197  1.00    1914.
#>  9 log_lik[4]  -0.450  -0.430 0.153 0.148   -0.730   -0.229  1.00    2150.
#> 10 log_lik[5]  -1.19   -1.16  0.288 0.283   -1.70    -0.769  1.00    2194.
#> # ℹ 95 more rows
#> # ℹ 1 more variable: ess_tail <dbl>

fit_logistic_optim <- cmdstanr_example("logistic", method = "optimize")
fit_logistic_optim$summary()
#> # A tibble: 105 × 2
#>    variable   estimate
#>    <chr>         <dbl>
#>  1 lp__        -63.9  
#>  2 alpha         0.364
#>  3 beta[1]      -0.632
#>  4 beta[2]      -0.259
#>  5 beta[3]       0.649
#>  6 log_lik[1]   -0.515
#>  7 log_lik[2]   -0.394
#>  8 log_lik[3]   -0.469
#>  9 log_lik[4]   -0.442
#> 10 log_lik[5]   -1.14 
#> # ℹ 95 more rows

fit_logistic_vb <- cmdstanr_example("logistic", method = "variational")
fit_logistic_vb$summary()
#> # A tibble: 106 × 7
#>    variable       mean  median     sd    mad      q5     q95
#>    <chr>         <dbl>   <dbl>  <dbl>  <dbl>   <dbl>   <dbl>
#>  1 lp__        -66.2   -65.8   1.56   1.32   -69.2   -64.3  
#>  2 lp_approx__  -1.94   -1.61  1.34   1.15    -4.56   -0.362
#>  3 alpha         0.448   0.454 0.184  0.186    0.144   0.745
#>  4 beta[1]      -0.710  -0.711 0.297  0.296   -1.20   -0.230
#>  5 beta[2]      -0.211  -0.214 0.213  0.218   -0.546   0.131
#>  6 beta[3]       0.719   0.717 0.272  0.278    0.285   1.18 
#>  7 log_lik[1]   -0.478  -0.474 0.0839 0.0865  -0.622  -0.351
#>  8 log_lik[2]   -0.391  -0.364 0.154  0.141   -0.698  -0.180
#>  9 log_lik[3]   -0.408  -0.382 0.181  0.171   -0.755  -0.162
#> 10 log_lik[4]   -0.483  -0.471 0.145  0.142   -0.745  -0.272
#> # ℹ 96 more rows

print_example_program("schools")
#> data {
#>   int<lower=1> J;
#>   vector<lower=0>[J] sigma;
#>   vector[J] y;
#> }
#> parameters {
#>   real mu;
#>   real<lower=0> tau;
#>   vector[J] theta;
#> }
#> model {
#>   target += normal_lpdf(tau | 0, 10);
#>   target += normal_lpdf(mu | 0, 10);
#>   target += normal_lpdf(theta | mu, tau);
#>   target += normal_lpdf(y | theta, sigma);
#> }
fit_schools_mcmc <- cmdstanr_example("schools")
#> Warning: 679 of 4000 (17.0%) transitions ended with a divergence.
#> See https://mc-stan.org/misc/warnings for details.
#> Warning: 2 of 4 chains had an E-BFMI less than 0.3.
#> See https://mc-stan.org/misc/warnings for details.
fit_schools_mcmc$summary()
#> # A tibble: 11 × 10
#>    variable   mean median    sd   mad       q5   q95  rhat ess_bulk ess_tail
#>    <chr>     <dbl>  <dbl> <dbl> <dbl>    <dbl> <dbl> <dbl>    <dbl>    <dbl>
#>  1 lp__     -55.2  -56.2   7.20  8.44 -65.7    -44.5  1.35     9.26   502.  
#>  2 mu         5.92   4.83  3.74  2.79   0.0739  12.5  1.14   269.     979.  
#>  3 tau        4.29   3.34  3.72  3.44   0.650   11.7  1.35     9.24     5.53
#>  4 theta[1]   8.28   6.13  6.22  3.70   0.148   20.3  1.19   199.    1448.  
#>  5 theta[2]   6.41   5.49  5.11  3.45  -1.72    15.4  1.10   460.    1260.  
#>  6 theta[3]   5.14   4.33  5.96  3.94  -5.01    15.0  1.23   782.    1447.  
#>  7 theta[4]   5.87   4.59  5.16  3.49  -2.48    15.2  1.31   730.     948.  
#>  8 theta[5]   4.56   4.37  5.04  4.04  -4.72    12.5  1.28   643.    1270.  
#>  9 theta[6]   5.08   4.56  5.20  3.35  -3.77    13.6  1.12   659.    1302.  
#> 10 theta[7]   8.06   6.46  5.45  3.53   0.928   18.2  1.05    73.8   1255.  
#> 11 theta[8]   6.38   5.50  6.15  3.54  -2.77    17.5  1.11   260.    1699.  

print_example_program("schools_ncp")
#> data {
#>   int<lower=1> J;
#>   vector<lower=0>[J] sigma;
#>   vector[J] y;
#> }
#> parameters {
#>   real mu;
#>   real<lower=0> tau;
#>   vector[J] theta_raw;
#> }
#> transformed parameters {
#>   vector[J] theta = mu + tau * theta_raw;
#> }
#> model {
#>   target += normal_lpdf(tau | 0, 10);
#>   target += normal_lpdf(mu | 0, 10);
#>   target += normal_lpdf(theta_raw | 0, 1);
#>   target += normal_lpdf(y | theta, sigma);
#> }
fit_schools_ncp_mcmc <- cmdstanr_example("schools_ncp")
fit_schools_ncp_mcmc$summary()
#> # A tibble: 19 × 10
#>    variable         mean   median    sd   mad       q5    q95  rhat ess_bulk
#>    <chr>           <dbl>    <dbl> <dbl> <dbl>    <dbl>  <dbl> <dbl>    <dbl>
#>  1 lp__         -46.9    -46.6    2.41  2.29  -51.3    -43.5   1.00    1454.
#>  2 mu             6.51     6.48   4.11  4.18   -0.0368  13.3   1.00    3392.
#>  3 tau            4.77     3.96   3.63  3.48    0.449   11.8   1.00    2260.
#>  4 theta_raw[1]   0.377    0.379  0.975 0.954  -1.26     1.95  1.00    4065.
#>  5 theta_raw[2]   0.0526   0.0555 0.895 0.884  -1.43     1.53  1.00    3436.
#>  6 theta_raw[3]  -0.143   -0.156  0.970 0.957  -1.73     1.48  1.00    3686.
#>  7 theta_raw[4]   0.0219   0.0261 0.906 0.930  -1.48     1.50  1.00    3671.
#>  8 theta_raw[5]  -0.263   -0.285  0.920 0.904  -1.76     1.29  1.00    4086.
#>  9 theta_raw[6]  -0.130   -0.144  0.930 0.917  -1.64     1.42  1.00    4353.
#> 10 theta_raw[7]   0.369    0.376  0.918 0.911  -1.15     1.87  1.00    3860.
#> 11 theta_raw[8]   0.0659   0.0592 0.971 0.988  -1.51     1.69  1.00    3781.
#> 12 theta[1]       9.01     8.30   6.74  5.76   -0.246   21.2   1.00    3647.
#> 13 theta[2]       6.88     6.83   5.35  4.89   -1.89    15.7   1.00    4377.
#> 14 theta[3]       5.45     5.79   6.51  5.55   -5.90    15.5   1.00    3662.
#> 15 theta[4]       6.73     6.60   5.73  5.22   -2.58    16.1   1.00    3999.
#> 16 theta[5]       4.91     5.16   5.52  4.90   -4.78    13.6   1.00    4517.
#> 17 theta[6]       5.64     5.91   6.01  5.33   -4.77    15.0   1.00    4197.
#> 18 theta[7]       8.87     8.36   5.95  5.37    0.312   19.6   1.00    3846.
#> 19 theta[8]       7.06     6.77   6.80  5.70   -3.70    18.3   1.00    3866.
#> # ℹ 1 more variable: ess_tail <dbl>

# optimization fails for hierarchical model
cmdstanr_example("schools", "optimize", quiet = FALSE)
#> Initial log joint probability = -53.966 
#>     Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes  
#>       99       123.351      0.058381   2.09576e+09      0.5049      0.5049      181    
#>     Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes  
#>      184       248.826        0.4674   3.92933e+16       1e-12       0.001      384  LS failed, Hessian reset  
#> Chain 1 Optimization terminated with error: 
#> Chain 1   Line search failed to achieve a sufficient decrease, no more progress can be made
#> Warning: Fitting finished unexpectedly! Use the $output() method for more information.
#> Finished in  0.2 seconds.
#> Error: Fitting failed. Unable to print.
# }