Skip to content
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

Mangled variable names from rstan::As.mcmc.list interferes with tidybayes #248

Open
psmits opened this issue Apr 20, 2020 · 2 comments
Open
Milestone

Comments

@psmits
Copy link

psmits commented Apr 20, 2020

I've already posted this on the stan discourse, but I figured this repo would be a better place to take this.

I'm working with the posterior samples from a model fit using RStan. I started my normal routine of using tidybayes to extract posterior samples from the stanfit object and start plotting. However, I ran into an issue with the variable names that come out of tidy_draws() where instead of the expected bracket notation e.g. x[1], I get the results in an unexpected dot notation form e.g. x.1. This mangling of the names does happen when i use dimnames() on the stanfit object.

I hunted for a long time to find the source of the problem, reading the github repo for tidybayes and rstan and I think I found the source of the problem. rstan::As.mcmc.list appears to be the source of the unexpected renaming.

You can compare the outputs from the following

library(rstan)
library(tidybayes)

csvfiles <- dir(system.file('misc', package = 'rstan'),
                pattern = 'rstan_doc_ex_[0-9].csv', full.names = TRUE)
fit <- read_stan_csv(csvfiles)
dimnames(fit)
As.mcmc.list(fit)
tidy_draws(fit)

I'm not sure how to resolve this issue, because it appears to break a lot functionality with tidybayes. For example, this now longer works because the intermediate mcmc.list created by rstan::As.mcmc.list doesn't have the expected variable names.

gather_draws(fit, z[i, j])
@mjskay
Copy link
Owner

mjskay commented Apr 22, 2020

Hmm this looks related to #132? You might find the workaround there helpful in the short term.

Long term I plan to look at this bug in detail and fix it, though given my current constraints this may not happen until this summer.

@psmits
Copy link
Author

psmits commented Apr 22, 2020

thanks for pointing out that work around. I was able to use the .repair_names function jburos wrote as a workaround.

Ben Goodrich replied to my post on the Stan forums about how there might be changes to rstan::As.mcmc.list down the line to address this incompatibility. I replied with a link to #132.

@mjskay mjskay added this to the Summer milestone May 10, 2020
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

No branches or pull requests

2 participants