Custom family - Beta-binomial in BRMS (R)

Hi
I’m trying to make a beta-binomial model using the vignette provided here: https://cran.r-project.org/web/packages/brms/vignettes/brms_customfamilies.html

However, I get the following error:

SYNTAX ERROR, MESSAGE(S) FROM PARSER:
No matches for: 
  beta_binomial2_lpmf(int, real, real, int)
Function beta_binomial2_lpmf not found.
 error in 'model445074614f85_file44502d6f3' at line 71, column 65
  -------------------------------------------------
    69:   if (!prior_only) {
    70:     for (n in 1:N) {
    71:       target += beta_binomial2_lpmf(Y[n] | mu[n], phi, trials[n]);
                                                                        ^
    72:     }
  -------------------------------------------------
Error in stanc(model_code = paste(program, collapse = "\n"), model_name = model_cppname,  : 
  failed to parse Stan model 'file44502d6f3' due to the above error.
SYNTAX ERROR, MESSAGE(S) FROM PARSER:
No matches for: 
  beta_binomial2_lpmf(int, real, real, int)
Function beta_binomial2_lpmf not found.
 error in 'model44506c6352ff_file44502d6f3' at line 71, column 65
  -------------------------------------------------
    69:   if (!prior_only) {
    70:     for (n in 1:N) {
    71:       target += beta_binomial2_lpmf(Y[n] | mu[n], phi, trials[n]);
                                                                        ^
    72:     }
  -------------------------------------------------
Error in stanc(model_code = paste(program, collapse = "\n"), model_name = model_cppname,  : 
  failed to parse Stan model 'file44502d6f3' due to the above error.

My code is as follows:

log_lik_beta_binomial2 <- function(i, draws) {
  mu <- draws$dpars$mu[, i]
  phi <- draws$dpars$phi
  N <- draws$data$trials[i]
  y <- draws$data$Y[i]
  beta_binomial2_lpmf(y, mu, phi, N)
}

predict_beta_binomial2 <- function(i, draws, ...) {
  mu <- draws$dpars$mu[, i]
  phi <- draws$dpars$phi
  N <- draws$data$trials[i]
  beta_binomial2_rng(mu, phi, N)
}

fitted_beta_binomial2 <- function(draws) {
  mu <- draws$dpars$mu
  trials <- draws$data$trials
  trials <- matrix(trials, nrow = nrow(mu), ncol = ncol(mu), byrow = TRUE)
  mu * trials
}

# definition of the custom family
beta_binomial2 <- custom_family(
  "beta_binomial2", 
  dpars = c("mu", "phi"),
  links = c("logit", "log"), 
  lb = c(0, 0), 
  ub = c(1, NA),
  type = "int", vars = "trials[n]",
  log_lik = log_lik_beta_binomial2,
  predict = predict_beta_binomial2,
  fitted = fitted_beta_binomial2
)
additionally required Stan code
stan_beta_binomial2 <- "
  real beta_binomial2_lpmf(int y, real mu, real phi, int T) {
    return beta_binomial2_lpmf(y | T, mu * phi, (1 - mu) * phi);
  }
  int beta_binomial2_rng(real mu, real phi, int T) {
    return beta_binomial2_rng(T, mu * phi, (1 - mu) * phi);
  }
"
fit1ab=brm(Disease~Ants+Treatment+Plant_sort+Pests+(Ants*Treatment*Plant_sort*Pests)+(1|Block/Plant_ID),data=mydata,family = beta_binomial2)

I’ve attached an example of my data. I’m very new to coding, and not that experienced in problem solving yet. Can anyone help me with how to solve the error?
Example_data.csv (4.9 KB)

Best regards,

I found an answer to my problem in the brms section.

2 Likes