Error with beta-binomial vignette

I recently got a new computer and the vignette on beta-binomial models using brms now doesn’t work. The beta-binomial models run fine, but in post-processing them, it seems like the stan functions are unrecognizable.

# load data
data("cbpp", package = "lme4")
head(cbpp)

# define custom family
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")

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

# post-processing
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)
}

loo(fit2)

The last line produces this error:

> loo(fit2)
Error in beta_binomial2_lpmf(y, mu, phi, trials) : 
  could not find function "beta_binomial2_lpmf"

Here are my specs:
R version 4.1.1 (2021-08-10)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Big Sur 11.3.1

rstan_2.26.4
StanHeaders_2.26.4
brms_2.16.2

It looks like the 8 schools example doesn’t work with Rstan either. I would’ve sworn I downloaded Xcode and rtools, but apparently not? I haven’t been using this computer much, so it’s been a bit neglected. Hopefully once Xcode installs from the app store (it’s been 1+ hrs with 25% progress), I can work this out.

EDIT: After a few hours, I was able to correctly install Xcode and rtools. The GUI for rtools (GitHub - rmacoslib/r-macos-rtools: Scripts to build an **unofficial** Rtools-esq installer for the macOS R toolchain) doesn’t work for OS Big Sur, so I followed the steps as documented here: R Compiler Tools for Rcpp on macOS | The Coatless Professor . I haven’t had to install rstan in a few yrs, so I forgot about the installation process.

2 Likes