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

Give example for the use for stan_lm or stan_glm from rstanarm #215

Closed kaybenleroll closed 3 years ago

kaybenleroll commented 4 years ago

It is not clear from the documentation what the intended usage is for outputs from stan_lm or stan_lm where you do not have hierarchies to pass to gather_draws() or spread_draws().

For example, fitting a very simple model with stan_lm():

library(tidyverse)
library(rstan)
library(rstanarm)
library(tidybayes)

test_stanlm <- stan_lm(mpg ~ cyl + disp + hp + drat + wt + vs + am + gear,
                       data  = mtcars,
                       prior = R2(location = 0.7),
                       seed  = 42)

Using the standard functions in this case throws errors, as I am not sure how to pass the keys in here, should I just use tidy_draws() here? These seems like the obvious answer (and it works) but the documentation for the function does talk about how it is mainly used by the other routines, so I am curious what is the most user-friendly or intended way to proceed with a model like this in tidybayes?

Great work on the package - hugely useful!

kaybenleroll commented 4 years ago

Sorry, just to be clear about - I spent a while reading around the various vignettes and looking at the READMEs and the code, but it wasn't clear from the docs what to do here.

Assuming it is to just use tidy_draws() it might be worth putting in an example for this to help out the stupid and/or pedantic like myself. :)

mjskay commented 4 years ago

Yeah, good question. tidy_draws() is a good usage here: it was originally intended to be primarily internal (as the docs say), but enough usages crop up that it has morphed away from that.

At some point spread_draws() and gather_draws() without variables specified will also do what you expect in this case (see #200), but that does not work yet.

kaybenleroll commented 4 years ago

Would you like me to add an example like the one above?

Happy to do it, just let me know where you would like to see it and I can create a pull request.

mjskay commented 4 years ago

I would love it if you wanted to do this!

An example using m %>% tidy_draws() or m %>% tidy_draws() %>% gather_variables() might be useful, especially if piped into a ggplot with a halfeye or something like that. Unless you had a different use case?

Could fit this in the tidy-rstanarm and tidy-brms vignettes (currently they are both partial duplicates of each other --- making something to share code across them is on my list but hasn't been done yet). A title like "Coefficient plots" or something like that could work.

kaybenleroll commented 4 years ago

Happy to do this over the next week or two - I'll do up a pull request and you can give me feedback?

I'll start with the rstanarm one, but I can look at doing something similar with brms too.

mjskay commented 4 years ago

Sounds great, thanks!

mjskay commented 3 years ago

I've removed the admonition not to use tidy_draws() from the docs and added an example to the tidybayes vignette of its use with gather_variables(), so I think that's good on this for now.