-
Notifications
You must be signed in to change notification settings - Fork 59
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
Recover higher-level groups with gather_draws() and a nested model #269
Comments
This is a really good question. It does seem unsatisfying to not have a counterpart for In the absence of that easier method already existing, your approach with joins is close to the simplest thing I can think of off the top of my head. You could also try using the lookup vector created in sdata instead of doing a join, which should be faster. It would probably look something like this (not tested): draws <- fit %>%
recover_types(data) %>%
gather_draws(d_theta[Prog]) %>%
mutate(Pillar = sdata$Pillar[Prog]) Or if Pillar is a factor, you could do this to recover the level names (again not tested): draws <- fit %>%
recover_types(data) %>%
gather_draws(d_theta[Prog]) %>%
mutate(Pillar = factor(sdata$Pillar[Prog], labels = levels(data$Pillar)) I think that should work but let me know if it doesn't. The future...As to the question of some kind of general solution to this problem... I usually try to imagine what the syntax should look like first, then sort out the details later :). In that spirit, one option might be to think about allowing arbitrary expressions to determine the index value to join on, so maybe allowing syntax like this: draws <- fit %>%
recover_types(data) %>%
gather_draws(d_theta[Prog], d_theta_mu[Pillar = sdata$Pillar[Prog]]) However, that might be hard to figure out automatically. One option would be to turn the derived index ( Maybe a better option is to explicitly allow the construction of such a "derived" index that the join could then be made on, so something like this: draws <- fit %>%
recover_types(data) %>%
gather_draws(d_theta[Prog, Pillar = sdata$Pillar[Prog]], d_theta_mu[Pillar]) Where a named index is simply an additional column added to the output evaluated as an expression of columns in the tidied table and the user's environment (thus allowing you to access both Anyway, I think something like this could solve your problem if implemented, but is also verging a bit on "too clever by half" territory, so I'd want to think a bit before committing to implementing it. Would be curious your thoughts. |
Thanks for the response. If thought is going to go into this, I think it is worthwhile to have a working example.
|
Based on your suggestion, the solution to my immediate problem is (noting that the groups "get in the way"):
|
I have been thinking about the syntax. Please note, the simplified example has the parameters I originally thought about I find you idea good:
It is clear (to me) that Pillar is being joined/added to d_theta[Prog]. I wonder if a separate function would be more in line with the tidyverse philosophy. The previous looks a bit more like datatable syntax. I have yet to come up with a decent idea for the name of the function, or even the function definition. Could the name involve join? index? Or is it just mutate? Could x_at_y() be reused here (not really, I guess)? Perhaps a decent idea will come to me. I guess I imagine something similar to the current solution, but simpler:
|
Thanks, this is really helpful! I agree re: the potential crypticness / non-tidyverse style of this. On the other hand, the indexing syntax is already a little different from tidyverse style. I think the idea of an additional function is a good one; one thing I've been thinking about is adding a kind of alternative tidyverse-like syntax that can be used to create specs inside of spread/gather_draws, in the way that |
Hi! I learned about tidybayes during StanCon and I am very pleased with what I am finding.
I am wondering if there is a tidier way to use gather_draws() with a nested model akin to x_at_y(). I think an example would be most clear.
In my model, each Prog is one of three Pillars. I am very pleased how easily I can get my data list for stan especially with x_at_y().
I am then also happy to get the draws out of the stan model using gather_draws.
The only thing that isn't so "tidy" is the inner_join() and what I did within the inner join. gather_draws() and recover_types(data) gives me Prog, but I also want the higher level Pillar, hence the join.
It gets a bit ugly if I try to also include the top-level hyperparameters in a single call to gather_draws():
gather_draws(d_theta_mu[Pillar]) produces a Pillar column, but it is empty for gather_draws(d_theta[Prog]), so I do the ugly join and then coalesce.
May I kindly as: is there a tidier way to do this?
The text was updated successfully, but these errors were encountered: