Fit models for use in examples
(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).
(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.
(logical) If TRUE (the default) then fitting the model is
wrapped in utils::capture.output().
Passed to the model-method-compile method.
The fitted model object returned by the selected method.
# \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)
#> Model executable is up to date!
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__ -66.0 -65.7 1.46 1.25 -68.9 -64.3 1.00 786.
#> 2 alpha 0.376 0.373 0.215 0.210 0.0346 0.747 1.00 1698.
#> 3 beta[1] -0.673 -0.671 0.261 0.249 -1.11 -0.251 1.00 1889.
#> 4 beta[2] -0.271 -0.267 0.228 0.239 -0.656 0.0921 1.00 2052.
#> 5 beta[3] 0.677 0.668 0.264 0.264 0.261 1.13 1.00 2070.
#> 6 log_lik[1] -0.515 -0.510 0.0982 0.0971 -0.685 -0.366 1.00 1843.
#> 7 log_lik[2] -0.402 -0.381 0.147 0.141 -0.670 -0.196 1.00 1893.
#> 8 log_lik[3] -0.496 -0.456 0.221 0.205 -0.905 -0.212 1.00 1969.
#> 9 log_lik[4] -0.449 -0.434 0.148 0.153 -0.721 -0.233 1.00 1958.
#> 10 log_lik[5] -1.18 -1.16 0.273 0.267 -1.65 -0.760 1.00 1991.
#> # ℹ 95 more rows
#> # ℹ 1 more variable: ess_tail <dbl>
fit_logistic_optim <- cmdstanr_example("logistic", method = "optimize")
#> Model executable is up to date!
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.648
#> 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")
#> Model executable is up to date!
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.0 -65.6 1.51 1.24 -69.1 -64.3
#> 2 lp_approx__ -2.01 -1.67 1.46 1.19 -4.72 -0.370
#> 3 alpha 0.343 0.336 0.198 0.203 0.0276 0.671
#> 4 beta[1] -0.664 -0.666 0.268 0.258 -1.08 -0.215
#> 5 beta[2] -0.285 -0.281 0.193 0.194 -0.607 0.0295
#> 6 beta[3] 0.720 0.706 0.285 0.282 0.277 1.23
#> 7 log_lik[1] -0.534 -0.529 0.0945 0.0944 -0.703 -0.391
#> 8 log_lik[2] -0.378 -0.365 0.147 0.143 -0.632 -0.172
#> 9 log_lik[3] -0.508 -0.477 0.200 0.201 -0.879 -0.237
#> 10 log_lik[4] -0.418 -0.405 0.131 0.132 -0.652 -0.232
#> # ℹ 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")
#> Model executable is up to date!
#> Warning: 211 of 4000 (5.0%) transitions ended with a divergence.
#> 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__ -57.8 -58.4 5.90 5.92 -66.5 -46.9 1.03 134. 91.1
#> 2 mu 6.59 6.58 4.19 4.02 -0.239 13.4 1.01 605. 759.
#> 3 tau 5.32 4.54 3.72 3.56 0.991 12.7 1.02 129. 68.3
#> 4 theta[1] 9.40 8.66 6.82 5.74 -0.443 21.5 1.01 543. 1393.
#> 5 theta[2] 6.95 6.63 5.60 5.01 -2.06 15.9 1.01 912. 1777.
#> 6 theta[3] 5.44 5.77 6.76 5.78 -5.92 15.4 1.01 1075. 2014.
#> 7 theta[4] 6.74 6.67 5.84 5.21 -3.08 16.3 1.01 1107. 1798.
#> 8 theta[5] 4.67 4.90 5.73 5.26 -5.35 13.2 1.02 853. 1100.
#> 9 theta[6] 5.53 5.51 6.11 5.41 -5.04 15.0 1.01 1068. 1575.
#> 10 theta[7] 9.19 8.53 6.04 5.70 0.489 20.1 1.01 474. 1994.
#> 11 theta[8] 7.09 7.01 6.55 5.52 -3.34 17.8 1.02 820. 1987.
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")
#> Model executable is up to date!
fit_schools_ncp_mcmc$summary()
#> # A tibble: 19 × 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__ -46.9 -4.66e+1 2.45 2.38 -51.4 -43.4 1.00 1684. 2222.
#> 2 mu 6.53 6.60e+0 4.23 4.29 -0.526 13.3 1.00 2821. 2399.
#> 3 tau 4.78 4.00e+0 3.65 3.45 0.323 12.0 1.00 2114. 2038.
#> 4 theta_r… 0.379 4.09e-1 0.987 0.963 -1.27 1.95 1.00 3181. 2648.
#> 5 theta_r… 0.0512 7.13e-2 0.909 0.893 -1.45 1.56 1.00 3571. 2470.
#> 6 theta_r… -0.155 -1.61e-1 0.953 0.950 -1.71 1.40 1.00 3651. 2384.
#> 7 theta_r… 0.0143 9.01e-3 0.888 0.872 -1.39 1.49 1.00 3590. 2857.
#> 8 theta_r… -0.266 -2.77e-1 0.898 0.888 -1.71 1.23 1.00 3506. 2698.
#> 9 theta_r… -0.144 -1.44e-1 0.924 0.895 -1.66 1.36 1.00 3308. 2961.
#> 10 theta_r… 0.361 3.56e-1 0.954 0.946 -1.25 1.89 1.00 3656. 2540.
#> 11 theta_r… 0.0480 3.80e-2 0.959 0.953 -1.52 1.68 1.00 4024. 2776.
#> 12 theta[1] 9.07 8.48e+0 6.90 5.95 -0.696 21.7 1.00 3565. 2983.
#> 13 theta[2] 6.86 6.81e+0 5.56 5.13 -2.16 15.9 1.00 4325. 2944.
#> 14 theta[3] 5.45 5.83e+0 6.39 5.75 -5.69 15.0 1.00 3605. 3177.
#> 15 theta[4] 6.67 6.67e+0 5.72 5.16 -2.73 16.2 0.999 4154. 3160.
#> 16 theta[5] 4.93 5.18e+0 5.62 5.36 -4.69 13.4 1.00 3410. 2956.
#> 17 theta[6] 5.62 5.85e+0 5.82 5.33 -4.24 14.9 1.00 3805. 3138.
#> 18 theta[7] 8.96 8.44e+0 6.20 5.60 -0.266 20.2 1.00 3687. 3063.
#> 19 theta[8] 6.92 6.75e+0 6.59 5.75 -3.69 17.7 1.00 3285. 3072.
# optimization fails for hierarchical model
cmdstanr_example("schools", "optimize", quiet = FALSE)
#> Model executable is up to date!
#> Initial log joint probability = -74.0754
#> Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes
#> 99 114.137 0.279807 1.66573e+09 0.5386 0.1135 171
#> Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes
#> 189 249.295 0.0236622 1.57992e+16 1e-12 0.001 399 LS failed, Hessian reset
#> Optimization terminated with error:
#> Line search failed to achieve a sufficient decrease, no more progress can be made
#> Finished in 0.1 seconds.
#> variable estimate
#> lp__ 249.29
#> mu 0.95
#> tau 0.00
#> theta[1] 0.95
#> theta[2] 0.95
#> theta[3] 0.95
#> theta[4] 0.95
#> theta[5] 0.95
#> theta[6] 0.95
#> theta[7] 0.95
#>
#> # showing 10 of 11 rows (change via 'max_rows' argument or 'cmdstanr_max_rows' option)
# }