Fit models for use in examples
cmdstanr_example( example = c("logistic", "schools", "schools_ncp"), method = c("sample", "optimize", "variational", "diagnose"), ..., quiet = TRUE ) print_example_program(example = c("logistic", "schools", "schools_ncp"))
example | (string) The name of the example. The currently available examples are
To print the Stan code for a given |
---|---|
method | (string) Which fitting method should be used? The default is
the |
... | Arguments passed to the chosen |
quiet | (logical) If |
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) 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.45 1.27 -68.8 -64.3 1.00 1146. #> 2 alpha 0.384 0.382 0.217 0.207 0.0306 0.749 1.00 2115. #> 3 beta[1] -0.662 -0.658 0.253 0.249 -1.08 -0.256 0.999 2154. #> 4 beta[2] -0.273 -0.272 0.225 0.221 -0.651 0.0966 1.00 1882. #> 5 beta[3] 0.679 0.676 0.272 0.269 0.241 1.14 1.00 2000. #> 6 log_lik[1] -0.514 -0.509 0.0959 0.0951 -0.682 -0.369 1.00 2063. #> 7 log_lik[2] -0.407 -0.386 0.151 0.150 -0.695 -0.193 1.00 2242. #> 8 log_lik[3] -0.494 -0.464 0.212 0.194 -0.888 -0.209 1.00 2079. #> 9 log_lik[4] -0.453 -0.434 0.154 0.152 -0.732 -0.236 1.00 1963. #> 10 log_lik[5] -1.19 -1.17 0.276 0.276 -1.66 -0.768 1.00 2212. #> # … with 95 more rows, and 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.365 #> 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 #> # … with 95 more rowsfit_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__ -65.9 -65.6 1.42 1.24 -68.7 -64.2 #> 2 lp_approx__ -1.99 -1.68 1.40 1.24 -4.76 -0.361 #> 3 alpha 0.402 0.398 0.234 0.243 0.0390 0.785 #> 4 beta[1] -0.598 -0.605 0.233 0.249 -0.971 -0.230 #> 5 beta[2] -0.252 -0.256 0.199 0.192 -0.585 0.0849 #> 6 beta[3] 0.779 0.773 0.211 0.215 0.450 1.13 #> 7 log_lik[1] -0.520 -0.514 0.104 0.101 -0.711 -0.364 #> 8 log_lik[2] -0.370 -0.354 0.122 0.115 -0.604 -0.202 #> 9 log_lik[3] -0.470 -0.439 0.198 0.184 -0.841 -0.206 #> 10 log_lik[4] -0.440 -0.430 0.132 0.123 -0.699 -0.251 #> # … with 96 more rowsprint_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")#>#>#>#>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.9 -57.1 7.17 7.48 -66.2 -43.1 1.18 15.5 NA #> 2 mu 6.44 6.31 4.01 3.68 -0.00706 13.7 1.15 592. 632. #> 3 tau 4.63 3.66 3.77 3.63 0.760 11.9 1.18 15.5 8.46 #> 4 theta[1] 8.81 7.10 6.76 4.99 -0.300 21.0 1.06 399. 1279. #> 5 theta[2] 6.86 6.89 5.35 4.29 -1.81 16.0 1.15 969. 1298. #> 6 theta[3] 5.67 6.13 6.26 4.75 -4.95 16.1 1.15 901. 1189. #> 7 theta[4] 6.38 5.90 5.49 4.55 -2.34 16.0 1.15 750. 1636. #> 8 theta[5] 4.78 5.75 5.41 4.27 -4.90 13.5 1.12 673. 863. #> 9 theta[6] 5.42 5.79 5.57 4.58 -4.12 14.6 1.15 765. 878. #> 10 theta[7] 8.53 7.16 5.81 4.96 0.719 19.3 1.05 244. 1167. #> 11 theta[8] 6.87 5.78 6.41 4.86 -3.12 18.3 1.15 999. 1605.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 ess_tail #> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> #> 1 lp__ -46.9 -46.6 2.45 2.39 -51.3 -43.4 1.00 1434. 2266. #> 2 mu 6.39 6.33 4.36 4.25 -0.764 13.5 1.00 2726. 2142. #> 3 tau 4.77 3.95 3.75 3.45 0.374 11.7 1.00 1988. 1457. #> 4 theta_r… 0.376 0.379 0.949 0.970 -1.17 1.93 1.00 4065. 2603. #> 5 theta_r… 0.0728 0.0765 0.897 0.895 -1.42 1.55 1.00 3969. 2465. #> 6 theta_r… -0.135 -0.129 0.965 0.981 -1.70 1.45 1.00 4040. 2528. #> 7 theta_r… 0.0355 0.0394 0.913 0.905 -1.46 1.54 1.00 4126. 2685. #> 8 theta_r… -0.272 -0.269 0.917 0.888 -1.76 1.25 1.00 4273. 3098. #> 9 theta_r… -0.167 -0.168 0.922 0.910 -1.69 1.32 1.00 3993. 2775. #> 10 theta_r… 0.386 0.410 0.946 0.910 -1.22 1.90 1.00 4227. 2942. #> 11 theta_r… 0.0653 0.0824 0.938 0.952 -1.48 1.58 1.00 4858. 2868. #> 12 theta[1] 8.83 8.22 6.62 5.73 -0.909 20.7 1.00 3668. 3136. #> 13 theta[2] 6.81 6.70 5.51 5.09 -2.08 15.6 1.00 4198. 3392. #> 14 theta[3] 5.49 5.74 6.46 5.69 -5.57 15.5 1.00 3698. 2838. #> 15 theta[4] 6.68 6.59 5.85 5.35 -2.69 16.1 1.00 4391. 3338. #> 16 theta[5] 4.76 5.04 5.74 5.33 -5.40 13.5 1.00 4192. 3211. #> 17 theta[6] 5.40 5.60 5.92 5.32 -4.58 14.4 1.00 4953. 3365. #> 18 theta[7] 8.85 8.39 6.04 5.70 -0.292 19.4 0.999 3832. 3162. #> 19 theta[8] 6.92 6.72 6.45 5.79 -3.11 17.5 1.00 4113. 3205.# optimization fails for hierarchical model cmdstanr_example("schools", "optimize", quiet = FALSE)#> Initial log joint probability = -373.952 #> Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes #> 99 114.115 0.330131 2.59089e+09 0.3881 1 169 #> Iter log prob ||dx|| ||grad|| alpha alpha0 # evals Notes #> 179 242.979 0.047195 2.42406e+16 1e-12 0.001 373 LS failed, Hessian reset#>#>#> Finished in 0.1 seconds.#> variable estimate #> lp__ 242.98 #> mu 1.05 #> tau 0.00 #> theta[1] 1.05 #> theta[2] 1.05 #> theta[3] 1.05 #> theta[4] 1.05 #> theta[5] 1.05 #> theta[6] 1.05 #> theta[7] 1.05 #> #> # showing 10 of 11 rows (change via 'max_rows' argument or 'cmdstanr_max_rows' option)# }