rethinking::dashboard() doesn't support CmdStanMCMC_CSV

Open
#461 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
25/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
r

Research direction

Start with the reprex: fit a small model with cmdstanr, then load the output CSVs with cmdstanr::as_cmdstan_fit() and call rethinking::divergent(fit) and rethinking::dashboard(fit). The two errors come from dashboard() and trankplot() in the rethinking package, which expect a stanfit or ulam object, and from the attr(fit, "cstanfit")$sampler_diagnostics call. Done means divergent() counts divergent transitions correctly and dashboard() runs on a CmdStanMCMC_CSV object without error.

Written by the indexing model from the issue text.

Description

rethinking::dashboard() doesn't seem to support CmdStanMCMC_CSV objects read into the environment via cmdstanr::as_cmdstan_fit(). I've really enjoyed using the dashboard function (thanks for sharing this!) on my own models. I recognize this function is not documented or actively supported for all object types. Regardless, below I provide a reprex and my own local work around (see next comment) for consideration. This is a common aspect of my work flow where I run threaded models on a server and analyze the samples locally.

I hope that this at least helps someone else who uses CmdStanR output CSVs! Thank you for your hard work.

Reprex


library(rethinking)
library(cmdstanr)

# fit a simple model with cmdstanr
code <- "
data {
  int<lower=0> N;
  vector[N] y;
}
parameters {
  real mu;
  real<lower=0> sigma;
}
model {
  mu ~ normal(0, 10);
  sigma ~ exponential(1);
  y ~ normal(mu, sigma);
}
"

stan_data <- list(
  N = 100,
  y = rnorm(100, mean = 5, sd = 2)
)

model <- cmdstan_model(write_stan_file(code))
model$sample(
  data = stan_data,
  chains = 4,
  output_dir = getwd()
) # not saving to env to demonstrate the following:

fit <- cmdstanr::as_cmdstan_fit(list.files(pattern = "-\\w{6}.csv"))
fit$diagnostic_summary()

# This works fine, i think
rethinking::precis(fit)

# These fail with CmdStanMCMC_CSV objects

rethinking::divergent(fit)
# This is summing the stepsize__ variable from the cmdstanMCMC_CSV object, not
# divergent transitions (in the sum(x[,,2]) call)

rethinking::dashboard(fit)
# This one has returned two different errors to me at different times:

# Error in attr(fit, "cstanfit")$sampler_diagnostics(inc_warmup = warmup) : 
#   $ operator is invalid for atomic vectors

# OR

# Error in trankplot(fit, pars = "lp__", lp = TRUE, add = TRUE) : 
#   requires map2stan, ulam or stanfit/cmdstan object

I have some modifications I've made locally to dashboard and trankplot that seem to prevent all of the above errors. I'll add those changes as a comment below. Fair warning, I am not really sure what the Error in attr(fit, "cstanfit")$sampler_diagnostics(inc_warmup = warmup) : $ operator is invalid for atomic vectors error is about... it seems to only occur for some objects (and I'm certain they are all CmdStanMCMC_CSV class). Therefore my work around might be nothing more than duct tape here...

I am happy to make a fork and submit as a PR if you'd like.

Cheers!

Session Info

> packageVersion("cmdstanr")
[1] ‘0.6.1.9000’
> cmdstanr::cmdstan_version()
[1] "2.37.0"


> sessionInfo()
R version 4.5.2 (2025-10-31)
Platform: x86_64-pc-linux-gnu
Running under: Ubuntu 22.04.5 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.10.0 
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0  LAPACK version 3.10.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

time zone: Etc/UTC
tzcode source: system (glibc)

attached base packages:
[1] parallel  stats     graphics  grDevices datasets  utils     methods   base     

other attached packages:
 [1] rstan_2.32.6        StanHeaders_2.32.10 jsonlite_1.9.1      lubridate_1.9.4     forcats_1.0.0       stringr_1.5.0      
 [7] dplyr_1.1.4         purrr_1.0.4         readr_2.1.5         tidyr_1.3.1         tibble_3.3.0        tidyverse_2.0.0    
[13] ggplot2_3.5.1       rethinking_2.42     bayesplot_1.11.1    posterior_1.6.1     cmdstanr_0.6.1.9000

loaded via a namespace (and not attached):
 [1] gtable_0.3.6         shape_1.4.6.1        tensorA_0.36.2.1     xfun_0.51            QuickJSR_1.6.0      
 [6] processx_3.8.6       inline_0.3.21        lattice_0.22-6       bspm_0.5.7           tzdb_0.4.0          
[11] vctrs_0.6.5          tools_4.5.2          ps_1.9.0             generics_0.1.4       stats4_4.5.2        
[16] curl_6.2.1           pkgconfig_2.0.3      data.table_1.17.0    checkmate_2.3.3      RColorBrewer_1.1-3  
[21] distributional_0.5.0 RcppParallel_5.1.10  lifecycle_1.0.3      compiler_4.5.2       farver_2.1.2        
[26] codetools_0.2-20     pillar_1.11.1        MASS_7.3-65          abind_1.4-8          tidyselect_1.2.1    
[31] mvtnorm_1.3-3        stringi_1.8.4        reshape2_1.4.4       labeling_0.4.3       grid_4.5.2          
[36] colorspace_2.1-1     cli_3.6.5            magrittr_2.0.4       loo_2.8.0            pkgbuild_1.4.6      
[41] withr_3.0.2          scales_1.4.0         backports_1.5.0      timechange_0.3.0     matrixStats_1.5.0   
[46] gridExtra_2.3        hms_1.1.3            coda_0.19-4.1        evaluate_1.0.3       knitr_1.49          
[51] V8_6.0.1             viridisLite_0.4.1    rlang_1.1.6          Rcpp_1.0.14          glue_1.8.0          
[56] pkgload_1.4.0        rstudioapi_0.17.1    R6_2.6.1             plyr_1.8.8          
Dominant language
R
Stars
2.4k
Forks
632
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from rmcelreath/rethinking

All issues in rmcelreath/rethinking

Similar issues

More R issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.