Skip to content

Allow tidy_draws to work with rstanarm models with a single variable #333

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

damonbayer
Copy link

@damonbayer damonbayer commented Mar 20, 2025

Fixes #331

Proof of the fix:

library(rstanarm)
#> Loading required package: Rcpp
#> This is rstanarm version 2.32.1
#> - See https://mc-stan.org/rstanarm/articles/priors for changes to default priors!
#> - Default priors may change, so it's safest to specify priors, even if equivalent to the defaults.
#> - For execution on a local, multicore CPU with excess RAM we recommend calling
#>   options(mc.cores = parallel::detectCores())
library(tidybayes)

# Simulate some data
true_prob <- 0.25
n <- sample(10:20, 20, replace = TRUE)
y <- rbinom(20, n, true_prob)
dat <- data.frame(hits = y, misses = n - y)

# Fit an intercept-only model
m <- stan_glm(cbind(hits, misses) ~ 1, data = dat, family = binomial())

post <- tidy_draws(m)
post
#> # A tibble: 4,000 × 10
#>    .chain .iteration .draw `(Intercept)` accept_stat__ stepsize__ treedepth__
#>     <int>      <int> <int>         <dbl>         <dbl>      <dbl>       <dbl>
#>  1      1          1     1         -1.22         0.891      0.814           2
#>  2      1          2     2         -1.26         1          0.814           1
#>  3      1          3     3         -1.48         0.905      0.814           1
#>  4      1          4     4         -1.27         0.974      0.814           2
#>  5      1          5     5         -1.29         1          0.814           1
#>  6      1          6     6         -1.27         0.980      0.814           1
#>  7      1          7     7         -1.31         1          0.814           2
#>  8      1          8     8         -1.41         0.969      0.814           2
#>  9      1          9     9         -1.26         0.906      0.814           1
#> 10      1         10    10         -1.57         0.993      0.814           2
#> # ℹ 3,990 more rows
#> # ℹ 3 more variables: n_leapfrog__ <dbl>, divergent__ <dbl>, energy__ <dbl>

Created on 2025-03-19 with reprex v2.1.1

@mbedward
Copy link

@damonbayer Just tested this and it is working perfectly for me. Thanks!

@damonbayer damonbayer changed the title Allow tidy_draws.mcmc.list to work with single variables Allow tidy_draws to work with rstanarm models with a single variable Apr 2, 2025
@mjskay
Copy link
Owner

mjskay commented Apr 2, 2025

Thanks for this! Is there a fix that doesn't introduce a direct dependency on abind? Might be able to use NROW instead of nrow for the first bit.

@damonbayer
Copy link
Author

Thanks @mjskay. I removed the abind dependency in 26db78a (#333).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Error calling tidy_draws on an intercept-only binomial model fitted with rstanarm
3 participants