I am fitting a negative binomial model with trials, using a custom family; closely following the vignette for custom families (https://cran.r-project.org/web/packages/brms/vignettes/brms_customfamilies.html), but I am wanting to pass the number of trials in to the model formula (my reason for this is so that I can change the number of trials when I use the model to predict on new data).
The problem I am having is that the negative binomial allows for a number to be drawn that is higher than the number of trials, and so the model is failing with the error Error: Number of trials is smaller than the number of events.
(possibly at this line https://github.com/paul-buerkner/brms/blob/242843d3de39b4311c241c00cb90fff74542ff5d/R/data-predictor.R#L870)
Is there a way to disable this checking, either when defining the custom family, or through setting the check_response
flag to False ?
# Minimal example (run on BRMS 2.6.3, Ubuntu 18.04)
library(brms)
data <- data.frame(trials=rep(5, 100),
count=rnbinom(100, size=5*2, prob=0.4))
neg_binomial_2_trials <- custom_family(
"neg_binomial_2_trials",
dpars = c("mu", "phi"),
links = c("log", "identity"),
lb = c(0, 0),
type = "int",
vars = "trials[n]"
)
stan_funs <- "
real neg_binomial_2_trials_lpmf(int y, real mu, real phi, int T) {
return neg_binomial_2_lpmf(y | mu * T, phi * T);
}
"
stanvars <- stanvar(scode = stan_funs, block = "functions")
model <- brm(count | trials(trials) ~ 1,
data=data,
family=neg_binomial_2_trials,
stanvars=stanvars)