permuted=FALSE/TRUE question

as.data.frame(extract(fit_cp80, permuted=TRUE))
gives me a nice dataframe
as.data.frame(extract(fit_cp80, permuted=FALSE))
gives me unpermuted draws

How do I get a nice dataframe with non-permuted draws with different chains stacked together?
And how do I get divergent info for those as extra column?

Mike’s case study is using only one chain, and I still have difficulties understanding how to manipulate R datatypes…

Aki

You can just do as.data.frame(fit_cp80) if you want a data.frame of unpermuted post-warmup draws for all (saved) unknowns in the Stan program. That does not include the stuff about divergences. To get the latter, you currently have to do

df <- as.data.frame(fit_cp80)
sp <- get_sampler_params(fit_cp80, inc_warmup = FALSE)
df$divergent <- c(sapply(sp, FUN = function(x) x[ ,"divergent__"]))
1 Like

Check out the implementation of partition_div in https://github.com/betanalpha/knitr_case_studies/blob/master/rstan_workflow/stan_utility.R.

1 Like

@avehtari The quickest way might be to use the convenience function nuts_params in bayesplot, which formats the hmc/nuts sampler parameters as a data frame.

df <- as.data.frame(fit) 
div_info <- nuts_params(fit, pars = "divergent__") 
df$divs <- div_info$Value
2 Likes

For future UX planning it would be nice to always return the same type from a given function unless there was a really good reason for it.

1 Like

I assume you’re referring to rstan::extract? If so then yeah I think the returning a list sometimes and array other times is not something to keep in the future.

2 Likes

I kinda got used to this approach (works well with a tidyverse-workflow):

rstan::extract(s, permute = FALSE) %>% reshape2::melt()

This gives an iterations, chains, parameters, and value column, which then works well (kind of elegantly) with anything in dyplr, tidyr, and specifically ggplot2…

3 Likes

Thanks for all the tips!