Error when trying to fit a mixed model with a custom beta-binomial family


First, thank you for this amazing package. I would also like to say that I am not used to posting issues so I will try to make my post and questions as reproducible as possible.

I have successfully fitted a mixed model with the custom beta-binomial family on a subset of my data following the example provided in this vignette : Define Custom Response Distributions with brms
The model was run on a laptop under Windows 10 OS.

However, my dataset is very large, and so, I have to run the model on a remote server with multiple cores and within-chain parallelization. The remote server runs on Cent OS Linux 7 and I used brms version 2.14.4. Unfortunately, the data is private so I cannot share it here.

Here is the structure of my model :

beta_binomial2 <- custom_family(
  "beta_binomial2", dpars = c("mu", "phi"),
  links = c("logit", "log"), lb = c(NA, 0),
  type = "int", vars = "vint1[n]"

stan_funs <- "
  real beta_binomial2_lpmf(int y, real mu, real phi, int T) {
    return beta_binomial_lpmf(y | T, mu * phi, (1 - mu) * phi);
  int beta_binomial2_rng(real mu, real phi, int T) {
    return beta_binomial_rng(T, mu * phi, (1 - mu) * phi);
stanvars <- stanvar(scode = stan_funs, block = "functions")

priors <- set_prior("normal(0, 5)", class = "b")

model_formula <- brmsformula(hunting_success | vint(4) ~
                                        Zspeed +
                                        Zspace_covered_rate +
                                        Zprox_mid_guard +
                                        Zhook_start_time +
                                        Zsurv_speed +
                                        Zsurv_space_covered_rate +
                                        (1 | map_name) +
                                        (1 | mirrors_id))

system.time(beta_binom_mod <- brm(formula = model_formula,
                              family = beta_binomial2,
                              warmup =3000, 
                              iter = 153000,
                              thin = 100,
                              chains = 4, 
                              inits = "0", 
                              threads = threading(10),
                              backend = "cmdstanr",
                              seed = 20210414,
                              stanvars = stanvars,
                              prior = priors,
                              control = list(adapt_delta = 0.95),
                              data = data))

For an unknown reason, I get the following error :

Compiling Stan program...

Semantic error in
'/localscratch/maxime11.18202117.0/Rtmpzdtb72/model-eede2bbff12.stan', line 34,
column 17 to column 66:
32: for (n in 1:N) {
33: int nn = n + start - 1;
34: ptarget += beta_binomial2_lpmf(Y[nn] | mu[n], phi, vint1[n]);
35: }
36: return ptarget;

A returning function was expected but an undeclared identifier
'beta_binomial2_lpmf' was supplied.

make: *** [make/program:53:
/localscratch/maxime11.18202117.0/Rtmpzdtb72/model-eede2bbff12.hpp] Error 1
Error: An error occured during compilation! See the message above for more
Timing stopped at: 0.831 1.085 73.23
Execution halted

When I run the same model with an observation-level random effect using the native binomial family on the same remote server, I don’t get an error.

I am sorry if this is a simple error, but I am quite new to using Bayesian models with STAN on remote servers, so I am still learning.

Thank you very much for your help.

EDIT** : I realized that without cmdstanr running as a backend, the model runs perfectly. So the error seems to stem from there. However, I need to use within-chain parallelization for the model to run fast.

Hi Maxime,

I believe this is due to a bug when using stanvars with within-chain parallelisation. The bug was fixed in brms 2.15.0, can you try updating the package and running the model again?


Thank you for your reply. In the end, I used a different computer cluster which uses the same R and package versions. I downloaded everything from scratch (cmdstanr, rstan, etc) and now everything works fine.

Thank you again!