Error in conditional_effects() in vignette example of custom response distribution

brms version 2.15.0

I get the following error when trying to follow the vignette regarding custom response distributions when I get to the conditional_effects() part.
Error: 'get_dpar' is not an exported object from 'namespace:brms'

I have pasted the code from the vignette below, for ease of use.


data("cbpp", package = "lme4")

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")

fit2 <- brm(
  incidence | vint(size) ~ period + (1|herd), data = cbpp, 
  family = beta_binomial2, stanvars = stanvars, 

expose_functions(fit2, vectorize = TRUE)
log_lik_beta_binomial2 <- function(i, prep) {
  mu <- brms::get_dpar(prep, "mu", i = i)
  phi <- brms::get_dpar(prep, "phi", i = i)
  trials <- prep$data$vint1[i]
  y <- prep$data$Y[i]
  beta_binomial2_lpmf(y, mu, phi, trials)

posterior_predict_beta_binomial2 <- function(i, prep, ...) {
  mu <- brms::get_dpar(prep, "mu", i = i)
  phi <- brms::get_dpar(prep, "phi", i = i)
  trials <- prep$data$vint1[i]
  beta_binomial2_rng(mu, phi, trials)

posterior_epred_beta_binomial2 <- function(prep) {
  mu <- brms::get_dpar(prep, "mu")
  trials <- prep$data$vint1
  trials <- matrix(trials, nrow = nrow(mu), ncol = ncol(mu), byrow = TRUE)
  mu * trials

conditional_effects(fit2, conditions = data.frame(size = 1))

Does anyone else get this error when using conditional_effects() for the beta binomial model in this vignette? Does anyone know why this might occur?

No, that example with beta-binomial likelihood does work properly — at least for me using brms 2.16.1.

You use an older version of brms. Perhaps that is part of the problem.

One tedious issue recently with this specific set of custom family functions is that you should use:

expose_functions(fit2, vectorize = TRUE)

… by itself.

Then leave a bit of time for it to process and ignore the almost endless Stanheader warnings that get dumped to screen. Maybe even resubmit:

expose_functions(fit2, vectorize = TRUE)

… so no further warnings are issued.

Why all these warnings (that have emerged in the past year) is a mystery to me. Maybe that is an issue with the CRAN version of rstan? And the code for these beta-binomial likelihood functions has changed many times over the past few years.

Anyway, then … submit the 3 post-processing functions.

Then submit the conditional_effects() line.

At least that is how it works for me on the current macOS 11.5.2 (Big Sur) and the current CRAN version of rstan and brms 2.16.1.

Bummer. It is a pain to update these work computers. Any work around for the Error that I mention?

I don’t have this problem. I don’t get any warnings at all with this line of code. Also, I can do pp_checks just fine, so I think it is the last chunk of code for posterior_epred that is giving me problems…

That is what I tried, and I get that ‘get_dpar’ Error.

The work around should be to upgrade to the current CRAN version of brms, which is 2.16.1. The current custom beta-binomial likelihood functions are quite recent so the first step is upgrade.