mjskay / tidybayes

Bayesian analysis + tidy data + geoms (R package)
http://mjskay.github.io/tidybayes
GNU General Public License v3.0
725 stars 59 forks source link

Confusion on generating fit curves with add_predicted_draws #306

Closed Erin-Fedewa-NOAA closed 1 year ago

Erin-Fedewa-NOAA commented 1 year ago

In the simple mtcars example for generating fit curves, the model is fit with one parameter, making your script below straightforward. However, my brms model is fit with several predictors and when trying to use a similar approach to add the posterior distribution, I'm getting a: Error in get(sgroup[1], data) : object 'x' not found....object x being any one of my additional covariates. Is this approach not possible for a more complex model, or am I just misinterpreting the function?

mtcars %>% data_grid(hp = seq_range(hp, n = 101)) %>% add_predicted_draws(m_mpg) %>% ggplot(aes(x = hp, y = mpg)) + stat_lineribbon(aes(y = .prediction), .width = c(.99, .95, .8, .5), color = "#08519C") + geom_point(data = mtcars, size = 2) + scale_fill_brewer()

mjskay commented 1 year ago

Yes, in general you need all predictors to be in the input - for some examples with more than one predictor see: https://mjskay.github.io/tidybayes/articles/tidy-brms.html#fitprediction-curves

The only exception is that random effects may be omitted if you zero them out using the re_formula argument.

mjskay commented 1 year ago

closing this for now --- let me know if you still have issues