I was working on some code assembled by a colleague and ran into some problems with the expose_functions()
function in brms. When I got the error (once I had made sure I hadn’t fat-fingered something), I started a new R session, made sure all of my packages, etc. were updated, and then tried it again - same error.
At this point, I went to the vignette (here) and ran that code - where the error repeated itself exactly.
Here are the relevant parts of the vignette:
# Data
data("cbpp", package = "lme4")
# Custom family definition
beta_binomial2 <- custom_family(
"beta_binomial2", dpars = c("mu", "phi"),
links = c("logit", "log"), lb = c(NA, 0),
type = "int", vars = "vint1[n]")
# Stan function definition
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);
}
"
# Stan variables
stanvars <- stanvar(scode = stan_funs, block = "functions")
# Model fit
fit2 <- brm(
incidence | vint(size) ~ period + (1|herd),
data = cbpp,
family = beta_binomial2,
stanvars = stanvars
)
# Expose functions
expose_functions(fit2, vectorize = TRUE)
None of this is original code - it’s taken directly from the vignette. When I run this, I generate 32 warnings and one error:
Error: $ operator is invalid for atomic vectors
This is exactly the same thing that happens when I run my other code. I’m sure it’s me and not the code, but I don’t see where I’ve gone wrong here.