Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

name of second additional variable in custom_family is duplicated in call to density function #1110

Closed
singmann opened this issue Feb 25, 2021 · 2 comments
Labels
Milestone

Comments

@singmann
Copy link
Contributor

singmann commented Feb 25, 2021

Something seems broken when having custom families with more than one additional variables. From the second onward, the variable name is copied in the call to the density function in the model block.

A short minimal reprex based on the example in ?custom_family to which we add the second variable foo which becomes foofoo in the code.

library("brms")
## Not run: 
## demonstrate how to fit a beta-binomial model
## generate some fake data
phi <- 0.7
n <- 300
z <- rnorm(n, sd = 0.2)
ntrials <- sample(1:10, n, replace = TRUE)
eta <- 1 + z
mu <- exp(eta) / (1 + exp(eta))
a <- mu * phi
b <- (1 - mu) * phi
p <- rbeta(n, a, b)
y <- rbinom(n, ntrials, p)
dat <- data.frame(y, z, ntrials)

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

# define the corresponding Stan density function
stan_funs <- "
  real beta_binomial2_lpmf(int y, real mu, real phi, int N, real foo) {
    return beta_binomial_lpmf(y | N, mu * phi, (1 - mu) * phi);
  }
"
svars <- stanvar(scode = stan_funs, block = "functions")

make_stancode(y | trials(ntrials) ~ z, data = dat, 
           family = beta_binomial2, stanvars =  svars)

This produces (only relevant part):

    for (n in 1:N) {
      target += beta_binomial2_lpmf(Y[n] | mu[n], phi, trials[n], foofoo);
    }

As can be seen, instead of foo we get foofoo.

@paul-buerkner paul-buerkner added this to the brms 2.14.0++ milestone Feb 25, 2021
paul-buerkner added a commit that referenced this issue Feb 25, 2021
@paul-buerkner
Copy link
Owner

Thanks. It should now be fixed. The problem was that foo did not contain an index at the end, a case that should now be handled correctly.

@singmann
Copy link
Contributor Author

awesome, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants