Skip to content

tidy/gather/spread_draws no longer working with cmdstan models when fixed_param = TRUE #339

@DanOvando

Description

@DanOvando

ran into a new issue using tidy_draws with cmdstan models when fixed_param = TRUE. It seems like the _draws functions now add in sampler diagnostics by default. These diagnostics are not produced by cmdstan when fixed_param = TRUE, and so the tidybayes::XX_draws no longer work. See reprex below. Possible simple solution to add an option to include sampler diagnostics or not?

Seems like this is caused by this line here in tidy_draws

library(cmdstanr)
#> This is cmdstanr version 0.9.0
#> - CmdStanR documentation and vignettes: mc-stan.org/cmdstanr
#> - CmdStan path: /Users/danovando/.cmdstan/cmdstan-2.36.0
#> - CmdStan version: 2.36.0
cmdstanr::cmdstan_version()
#> [1] "2.36.0"
library(tidybayes)
packageVersion("tidybayes")
#> [1] '3.0.7'


file <- file.path(cmdstan_path(), "examples", "bernoulli", "bernoulli.stan")
mod <- cmdstan_model(file)

# names correspond to the data block in the Stan program
data_list <- list(N = 10, y = c(0,1,0,0,0,0,0,0,0,1))

fit <- mod$sample(
  data = data_list,
  seed = 123,
  chains = 4,
  parallel_chains = 1,
  refresh = 0 # print update every 500 iters
)
#> Running MCMC with 4 sequential chains...
#> 
#> Chain 1 finished in 0.0 seconds.
#> Chain 2 finished in 0.0 seconds.
#> Chain 3 finished in 0.0 seconds.
#> Chain 4 finished in 0.0 seconds.
#> 
#> All 4 chains finished successfully.
#> Mean chain execution time: 0.0 seconds.
#> Total execution time: 0.5 seconds.

tidy_draws(fit)
#> # A tibble: 4,000 × 11
#>     lp__ theta treedepth__ divergent__ energy__ accept_stat__ stepsize__
#>    <dbl> <dbl>       <dbl>       <dbl>    <dbl>         <dbl>      <dbl>
#>  1 -7.00 0.169           2           0     8.95         0.690      0.905
#>  2 -7.90 0.462           1           0     8.77         0.809      0.905
#>  3 -7.42 0.409           2           0     7.88         1          0.905
#>  4 -6.75 0.249           2           0     7.64         0.966      0.905
#>  5 -6.90 0.185           2           0     6.93         0.976      0.905
#>  6 -6.93 0.330           1           0     7.29         0.957      0.905
#>  7 -7.16 0.149           2           0     7.92         0.846      0.905
#>  8 -6.80 0.294           1           0     7.43         0.961      0.905
#>  9 -6.75 0.235           2           0     6.79         1          0.905
#> 10 -6.75 0.235           1           0     8.22         0.727      0.905
#> # ℹ 3,990 more rows
#> # ℹ 4 more variables: n_leapfrog__ <dbl>, .chain <int>, .iteration <int>,
#> #   .draw <int>

fixed_param_fix <- mod$sample(
  data = data_list,
  seed = 123,
  chains = 4,
  parallel_chains = 1,
  refresh = 0, # print update every 500 iters
  fixed_param = TRUE
)
#> Running MCMC with 4 sequential chains...
#> 
#> Chain 1 finished in 0.0 seconds.
#> Chain 2 finished in 0.0 seconds.
#> Chain 3 finished in 0.0 seconds.
#> Chain 4 finished in 0.0 seconds.
#> 
#> All 4 chains finished successfully.
#> Mean chain execution time: 0.0 seconds.
#> Total execution time: 0.5 seconds.

tidy_draws(fixed_param_fix)
#> Error: There are no sampler diagnostics when fixed_param = TRUE.

Created on 2025-07-29 with reprex v2.1.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions