Skip to contents

Takes as input a array object from call to sampler and extracts rhat and neff values.

Usage

get_rhat(object, ...)

plot_rhat(object, labels, ...)

get_neff(object, ...)

plot_neff(object, labels, ...)

Arguments

object

output from call to sampler.

...

(not used)

labels

vector of parameter labels

Examples

library(cmdstanr)
#> This is cmdstanr version 0.9.0.9000
#> - CmdStanR documentation and vignettes: mc-stan.org/cmdstanr
#> - CmdStan path: C:/Users/ctted/.cmdstan/cmdstan-2.37.0
#> - CmdStan version: 2.37.0

CODE <- "data{ int n; vector[n] x; }  
   parameters{ real mu; }  
   model{ x ~ normal(mu, 1.0);} 
   generated quantities{ vector[n] x_sim; real x_sim_sum; 
   for (i in 1:n) x_sim[i] = normal_rng(mu, 1.0); x_sim_sum = sum(x_sim);}\n"
   
ff  <- write_stan_file(CODE)  
mdl <- cmdstan_model(stan_file = ff); rm(ff)
#> In file included from stan/src/stan/model/model_header.hpp:5:
#> stan/src/stan/model/model_base_crtp.hpp:159:20: warning: 'stan::math::var stan::model::model_base_crtp<M>::log_prob(std::vector<stan::math::var_value<double>, std::allocator<stan::math::var_value<double> > >&, std::vector<int>&, std::ostream*) const [with M = model_d1847a8552086ba1718cf52e6e9d2499_model_namespace::model_d1847a8552086ba1718cf52e6e9d2499_model; stan::math::var = stan::math::var_value<double>; std::ostream = std::basic_ostream<char>]' was hidden [-Woverloaded-virtual=]
#>   159 |   inline math::var log_prob(std::vector<math::var>& theta,
#>       |                    ^~~~~~~~
#> C:/Users/ctted/AppData/Local/Temp/RtmpsVlYQl/model-2d102d23101f.hpp:374: note:   by 'model_d1847a8552086ba1718cf52e6e9d2499_model_namespace::model_d1847a8552086ba1718cf52e6e9d2499_model::log_prob'
#>   374 |   log_prob(std::vector<T_>& params_r, std::vector<int>& params_i,
#> stan/src/stan/model/model_base_crtp.hpp:154:17: warning: 'double stan::model::model_base_crtp<M>::log_prob(std::vector<double>&, std::vector<int>&, std::ostream*) const [with M = model_d1847a8552086ba1718cf52e6e9d2499_model_namespace::model_d1847a8552086ba1718cf52e6e9d2499_model; std::ostream = std::basic_ostream<char>]' was hidden [-Woverloaded-virtual=]
#>   154 |   inline double log_prob(std::vector<double>& theta, std::vector<int>& theta_i,
#>       |                 ^~~~~~~~
#> C:/Users/ctted/AppData/Local/Temp/RtmpsVlYQl/model-2d102d23101f.hpp:374: note:   by 'model_d1847a8552086ba1718cf52e6e9d2499_model_namespace::model_d1847a8552086ba1718cf52e6e9d2499_model::log_prob'
#>   374 |   log_prob(std::vector<T_>& params_r, std::vector<int>& params_i,
#> stan/src/stan/model/model_base_crtp.hpp:96:20: warning: 'stan::math::var stan::model::model_base_crtp<M>::log_prob(Eigen::Matrix<stan::math::var_value<double>, -1, 1>&, std::ostream*) const [with M = model_d1847a8552086ba1718cf52e6e9d2499_model_namespace::model_d1847a8552086ba1718cf52e6e9d2499_model; stan::math::var = stan::math::var_value<double>; std::ostream = std::basic_ostream<char>]' was hidden [-Woverloaded-virtual=]
#>    96 |   inline math::var log_prob(Eigen::Matrix<math::var, -1, 1>& theta,
#>       |                    ^~~~~~~~
#> C:/Users/ctted/AppData/Local/Temp/RtmpsVlYQl/model-2d102d23101f.hpp:374: note:   by 'model_d1847a8552086ba1718cf52e6e9d2499_model_namespace::model_d1847a8552086ba1718cf52e6e9d2499_model::log_prob'
#>   374 |   log_prob(std::vector<T_>& params_r, std::vector<int>& params_i,
#> stan/src/stan/model/model_base_crtp.hpp:91:17: warning: 'double stan::model::model_base_crtp<M>::log_prob(Eigen::VectorXd&, std::ostream*) const [with M = model_d1847a8
#> 552086ba1718cf52e6e9d2499_model_namespace::model_d1847a8552086ba1718cf52e6e9d2499_model; Eigen::VectorXd = Eigen::Matrix<double, -1, 1>; std::ostream = std::basic_ostream<char>]' was hidden [-Woverloaded-virtual=]
#>    91 |   inline double log_prob(Eigen::VectorXd& theta,
#>       |                 ^~~~~~~~
#> C:/Users/ctted/AppData/Local/Temp/RtmpsVlYQl/model-2d102d23101f.hpp:374: note:   by 'model_d1847a8552086ba1718cf52e6e9d2499_model_namespace::model_d1847a8552086ba1718cf52e6e9d2499_model::log_prob'
#>   374 |   log_prob(std::vector<T_>& params_r, std::vector<int>& params_i,

n <- 20
x <- rnorm(n, 0, 2)

mdl_fit <- mdl$sample(data = list(n = n, x = x), init = function() list(mu = 0), chains = 3)
#> Running MCMC with 3 sequential chains...
#> 
#> Chain 1 Iteration:    1 / 2000 [  0%]  (Warmup) 
#> Chain 1 Iteration:  100 / 2000 [  5%]  (Warmup) 
#> Chain 1 Iteration:  200 / 2000 [ 10%]  (Warmup) 
#> Chain 1 Iteration:  300 / 2000 [ 15%]  (Warmup) 
#> Chain 1 Iteration:  400 / 2000 [ 20%]  (Warmup) 
#> Chain 1 Iteration:  500 / 2000 [ 25%]  (Warmup) 
#> Chain 1 Iteration:  600 / 2000 [ 30%]  (Warmup) 
#> Chain 1 Iteration:  700 / 2000 [ 35%]  (Warmup) 
#> Chain 1 Iteration:  800 / 2000 [ 40%]  (Warmup) 
#> Chain 1 Iteration:  900 / 2000 [ 45%]  (Warmup) 
#> Chain 1 Iteration: 1000 / 2000 [ 50%]  (Warmup) 
#> Chain 1 Iteration: 1001 / 2000 [ 50%]  (Sampling) 
#> Chain 1 Iteration: 1100 / 2000 [ 55%]  (Sampling) 
#> Chain 1 Iteration: 1200 / 2000 [ 60%]  (Sampling) 
#> Chain 1 Iteration: 1300 / 2000 [ 65%]  (Sampling) 
#> Chain 1 Iteration: 1400 / 2000 [ 70%]  (Sampling) 
#> Chain 1 Iteration: 1500 / 2000 [ 75%]  (Sampling) 
#> Chain 1 Iteration: 1600 / 2000 [ 80%]  (Sampling) 
#> Chain 1 Iteration: 1700 / 2000 [ 85%]  (Sampling) 
#> Chain 1 Iteration: 1800 / 2000 [ 90%]  (Sampling) 
#> Chain 1 Iteration: 1900 / 2000 [ 95%]  (Sampling) 
#> Chain 1 Iteration: 2000 / 2000 [100%]  (Sampling) 
#> Chain 1 finished in 0.0 seconds.
#> Chain 2 Iteration:    1 / 2000 [  0%]  (Warmup) 
#> Chain 2 Iteration:  100 / 2000 [  5%]  (Warmup) 
#> Chain 2 Iteration:  200 / 2000 [ 10%]  (Warmup) 
#> Chain 2 Iteration:  300 / 2000 [ 15%]  (Warmup) 
#> Chain 2 Iteration:  400 / 2000 [ 20%]  (Warmup) 
#> Chain 2 Iteration:  500 / 2000 [ 25%]  (Warmup) 
#> Chain 2 Iteration:  600 / 2000 [ 30%]  (Warmup) 
#> Chain 2 Iteration:  700 / 2000 [ 35%]  (Warmup) 
#> Chain 2 Iteration:  800 / 2000 [ 40%]  (Warmup) 
#> Chain 2 Iteration:  900 / 2000 [ 45%]  (Warmup) 
#> Chain 2 Iteration: 1000 / 2000 [ 50%]  (Warmup) 
#> Chain 2 Iteration: 1001 / 2000 [ 50%]  (Sampling) 
#> Chain 2 Iteration: 1100 / 2000 [ 55%]  (Sampling) 
#> Chain 2 Iteration: 1200 / 2000 [ 60%]  (Sampling) 
#> Chain 2 Iteration: 1300 / 2000 [ 65%]  (Sampling) 
#> Chain 2 Iteration: 1400 / 2000 [ 70%]  (Sampling) 
#> Chain 2 Iteration: 1500 / 2000 [ 75%]  (Sampling) 
#> Chain 2 Iteration: 1600 / 2000 [ 80%]  (Sampling) 
#> Chain 2 Iteration: 1700 / 2000 [ 85%]  (Sampling) 
#> Chain 2 Iteration: 1800 / 2000 [ 90%]  (Sampling) 
#> Chain 2 Iteration: 1900 / 2000 [ 95%]  (Sampling) 
#> Chain 2 Iteration: 2000 / 2000 [100%]  (Sampling) 
#> Chain 2 finished in 0.0 seconds.
#> Chain 3 Iteration:    1 / 2000 [  0%]  (Warmup) 
#> Chain 3 Iteration:  100 / 2000 [  5%]  (Warmup) 
#> Chain 3 Iteration:  200 / 2000 [ 10%]  (Warmup) 
#> Chain 3 Iteration:  300 / 2000 [ 15%]  (Warmup) 
#> Chain 3 Iteration:  400 / 2000 [ 20%]  (Warmup) 
#> Chain 3 Iteration:  500 / 2000 [ 25%]  (Warmup) 
#> Chain 3 Iteration:  600 / 2000 [ 30%]  (Warmup) 
#> Chain 3 Iteration:  700 / 2000 [ 35%]  (Warmup) 
#> Chain 3 Iteration:  800 / 2000 [ 40%]  (Warmup) 
#> Chain 3 Iteration:  900 / 2000 [ 45%]  (Warmup) 
#> Chain 3 Iteration: 1000 / 2000 [ 50%]  (Warmup) 
#> Chain 3 Iteration: 1001 / 2000 [ 50%]  (Sampling) 
#> Chain 3 Iteration: 1100 / 2000 [ 55%]  (Sampling) 
#> Chain 3 Iteration: 1200 / 2000 [ 60%]  (Sampling) 
#> Chain 3 Iteration: 1300 / 2000 [ 65%]  (Sampling) 
#> Chain 3 Iteration: 1400 / 2000 [ 70%]  (Sampling) 
#> Chain 3 Iteration: 1500 / 2000 [ 75%]  (Sampling) 
#> Chain 3 Iteration: 1600 / 2000 [ 80%]  (Sampling) 
#> Chain 3 Iteration: 1700 / 2000 [ 85%]  (Sampling) 
#> Chain 3 Iteration: 1800 / 2000 [ 90%]  (Sampling) 
#> Chain 3 Iteration: 1900 / 2000 [ 95%]  (Sampling) 
#> Chain 3 Iteration: 2000 / 2000 [100%]  (Sampling) 
#> Chain 3 finished in 0.1 seconds.
#> 
#> All 3 chains finished successfully.
#> Mean chain execution time: 0.0 seconds.
#> Total execution time: 0.6 seconds.
#> 

get_rhat(get_traces(mdl_fit, pars = "mu", dims = 1)[[1]])
#>      value parameter rating
#> 1 1.001133         1   Good
get_neff(get_traces(mdl_fit, pars = "mu", dims = 1)[[1]])
#>       neff    value parameter rating
#> 1 1154.257 1.154257         1   Good