mjskay / tidybayes

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

Predicted values for multinomial family #176

Closed aammd closed 4 years ago

aammd commented 5 years ago

I just made a very small adjustment to fitted_predicted_draws_brmsfit_() in order to allow it to make predictions for members of the multinomial family

mjskay commented 5 years ago

Sweet, thanks!

Can you send me a simple test case model and the expected output? I'd like to make a test case for this (you're also more than welcome to try your hand at a test case for this if you want, but it will require mucking with build_test_models.R and I haven't really documented how that whole jankiness works, so I wouldn't want to inflict it upon you...)

aammd commented 5 years ago

Of course! I'm not sure though what the best way would be to provide the model? Here is some code to make fake data and fit a multinomial model, following Paul Buerkner's code here

library(brms)
#> Loading required package: Rcpp
#> Warning: package 'Rcpp' was built under R version 3.5.2
#> Loading 'brms' package (version 2.9.0). Useful instructions
#> can be found by typing help('brms'). A more detailed introduction
#> to the package is available through vignette('brms_overview').
library(tidybayes)

N <- 15
dat <- data.frame(
  y1 = rbinom(N, 10, 0.3), y2 = rbinom(N, 10, 0.5), 
  y3 = rbinom(N, 10, 0.7), x = rnorm(N)
)
dat$size <- with(dat, y1 + y2 + y3)
dat$y <- with(dat, cbind(y1, y2, y3))

prior <- prior(normal(0, 10), b, dpar = muy2) +
  prior(cauchy(0, 1), Intercept) +
  prior(normal(0, 2), Intercept, dpar = muy3)

fit <- brm(bf(y | trials(size)  ~ 1), data = dat, 
           family = multinomial(), prior = prior(normal(0,3), Intercept), 
           file = "multinomial_example")
#> Compiling the C++ model
#> Start sampling
#> 
#> SAMPLING FOR MODEL 'b5fd64142844f76565ed307fc3c57731' NOW (CHAIN 1).
#> Chain 1: 
# (etc)

# source("new_fitted_predicted_draw_.R")
some_draws <- dat %>% 
  select(x, size) %>% 
  add_predicted_draws(fit, n = 10)
#> Error in dat %>% select(x, size) %>% add_predicted_draws(fit, n = 10): could not find function "%>%"

Created on 2019-06-17 by the reprex package (v0.2.0).

mjskay commented 5 years ago

Thanks --- the ideal test case would be something that gives the incorrect output on the current version of tidybayes and the correct output on the modified version. I try to follow the practice of writing a test case that fails and then writing code that fixes it: this helps prevent reversions back to the incorrect version in the future (the software engineering version of whack-a-mole).

Unless I've screwed something up in my testing, the example you've given gives me the same output in both versions. Do you have an example of something that gives incorrect output in the current version and correct output in the modified version?

(also sidebar: Could you file this PR against the dev branch instead of master? I prefer everything to filter through dev so I can ensure tests pass before shuffling to master; this makes it more likely that users typing devtools::install_github("mjskay/tidybayes") doesn't give broken versions during development). Thanks!

mjskay commented 4 years ago

Sorry for not circling back around to this sooner. This should be fixed on master now.