mjskay / tidybayes

Bayesian analysis + tidy data + geoms (R package)
http://mjskay.github.io/tidybayes
GNU General Public License v3.0
712 stars 59 forks source link

Extracting pre-warmup samples from Stan model #236

Closed mark-andrews closed 4 months ago

mark-andrews commented 4 years ago

Using rstan::extract with permuted=F and inc_warmup=T, I can extract all samples including pre-warmup samples into an array. It appears to me that tidybayes::tidy_draws, tidybayes::spread_draws, tidybayes::gather_draws, etc., all provide only post-warmup samples. I understand that is what would be desired in almost all cases. However, am I right in thinking that it is not possible to get the pre-warmup samples using these functions?

mjskay commented 4 years ago

Yes, currently it is not possible. However, having an option for this wouldn't be a bad idea, let me see what I can do (probably not until this summer)

jfsalzmann commented 4 months ago

Has there been any progress on this, or are there workarounds available? @mjskay

mjskay commented 4 months ago

Uh yes actually! Because the draws formats from {posterior} have been integrated into the stan packages and are supported in {tidybayes}, this is a lot easier now.

If you're using {rstan} something like this should work:

m |>
  extract(permuted = FALSE, inc_warmup = TRUE) |> 
  posterior::as_draws_df() |>
  spread_draws(...)  # etc

and if you're using {cmdstanr}, something like this:

m$draws(format = "df", inc_warmup = TRUE) |>
  spread_draws(...)  # etc

Lemme know if that helps!

jfsalzmann commented 4 months ago

amazing, thanks!