mjskay / tidybayes

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

add capability to generate add_fitted_draws values based on n fixed at 1 in a binomial model #155

Closed realkrantz closed 5 years ago

realkrantz commented 5 years ago

I propose that a function allowing the number of trials n to be set at an arbitrary value such as 1 or 100 be added in add_fiited_draws for a binomial regression model in order to make it possible or at least straightfoward getting add_fitted_draws values in probability or percentage.

For example, when I do this based on the data below:

m <- brm(r | trials(n) + weights(weights) ~ treat*c2+(1|group), 
               data=data, family=binomial(link=logit))
DT = data %>% add_fitted_draws(m, value = "E")

I would like E to be probabilities based on number of trials n fixed at 1. Instead, I get some values of E greater than 100, which is not appropriate because I was expecting these to be proportions or percentages.

data= read.table(header = TRUE, text = "
n r r/n group treat c2 c1 weights
62 3 0.048387097 1 0 0.1438 1.941115288 1.941115288
96 1 0.010416667 1 0 0.237 1.186583128 1.186583128
17 0 0 0 0 0.2774 1.159882668 3.159882668
41 2 0.048780488 1 0 0.2774 1.159882668 3.159882668
212 170 0.801886792 0 0 0.2093 1.133397521 1.133397521
143 21 0.146853147 1 1 0.1206 1.128993008 1.128993008
143 0 0 1 1 0.1707 1.128993008 2.128993008
143 33 0.230769231 0 1 0.0699 1.128993008 1.128993008
73 62 1.260273973 0 1 0.1351 1.121927228 1.121927228
73 17 0.232876712 0 1 0.1206 1.121927228 1.121927228")

Thanks in advance for any help.

mjskay commented 5 years ago

That should be determined by the column n in the data frame you pass to add_fitted_draws. If you change it to 1 I think you should get what you're looking for.

realkrantz commented 5 years ago

Great, @mjskay. It works. Just tested. Thank you very much.