Re_formula argument in fitted and predict fails for multimembership models with multimembership covariates

Please also provide the following information in addition to your question:

  • Operating System: macOS 10.14.5
  • brms Version: 2.9.2

I wanted to flag this here before opening an issue on Github in case I’m making a mistake. It appears that the re_formula argument in the fitted and predict functions fail where we specify only a multimembership grouping term with multimembership covariates. In particular, they give the following error:

Error: Duplicated group-level effects are not allowed.
Occured for effect 'Intercept' of group 'mmg1g2'.

It is possible that I am making a mistake here. But, if not, I suspect that an error to stop people adding, for example, ~ (1 + a | group) + (1 + b | group), is incorrectly flagging multimembership groups (which have more than one intercept by definition) as such cases.

Reproducible example below:

library(brms)

# simulate some data
dat <- data.frame(
  y = rnorm(100),
  x1 = rnorm(100),
  x2 = rnorm(100),
  g1 = sample(1:10, 100, TRUE),
  g2 = sample(1:10, 100, TRUE),
  id = sample(1:3, 100, TRUE)
)

# multi-membership model with level specific covariate values
dat$xc <- (dat$x1 + dat$x2) / 2 
fit <- brm(y ~ xc + (1 + mmc(x1, x2) | mm(g1, g2)) + (1 | id),
           data = dat,
           chains = 2,
           cores = 2,
           control = list(adapt_delta = .99))
summary(fit)

# simulate new data where cases aren't clustered by id
new_dat <-
  data.frame(
    y = rnorm(100),
    x1 = rnorm(100),
    x2 = rnorm(100),
    g1 = sample(1:10, 100, TRUE),
    g2 = sample(1:10, 100, TRUE)
  )
new_dat$xc <- (new_dat$x1 + new_dat$x2) / 2 

# compute fitted and predicted values for new data
fitted(fit,
       re_formula = ~ (1 + mmc(x1, x2) | mm(g1, g2)),
       newdata = new_dat) # Error

predict(fit,
        re_formula = ~ (1 + mmc(x1, x2) | mm(g1, g2)),
        newdata = new_dat) # Error

One of the reasons I think I might be doing something wrong is that this is not an issue where there are no multimembership covariates. For example, if we run the exact same code, minus the MMCs, fitted and predict produce no error message.

# multi-membership model with level specific covariate values
fit <- brm(y ~ 1 + (1 | mm(g1, g2)) + (1 | id),
           data = dat,
           chains = 2,
           cores = 2,
           control = list(adapt_delta = .99))
summary(fit)

# compute fitted and predicted values for new data
fitted(fit,
       re_formula = ~ (1 | mm(g1, g2)),
       newdata = new_dat) # No error

predict(fit,
        re_formula = ~ (1 | mm(g1, g2)),
        newdata = new_dat) # No error

Thanks for providing a reproducible example! It should now be fixed in the github version of brms.

1 Like

Thanks, Paul!