permuted=FALSE/TRUE question


#1

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


#2

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__"]))

#3

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


#4

@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

#5

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.


#6

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.


#7

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…


#8

Thanks for all the tips!