# Re\_formula argument in fitted and predict fails for multimembership models with multimembership covariates

**URL:** <https://discourse.mc-stan.org/t/re-formula-argument-in-fitted-and-predict-fails-for-multimembership-models-with-multimembership-covariates/9719>\
**Category:** brms\
**Created:** [July 15, 2019, 8:15am UTC](https://discourse.mc-stan.org/t/re-formula-argument-in-fitted-and-predict-fails-for-multimembership-models-with-multimembership-covariates/9719 "2019-07-15T08:15:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jackbailey](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jackbailey/32/14978_2.png) [@jackbailey](https://discourse.mc-stan.org/u/jackbailey)\
**Post date:** [July 15, 2019, 8:15am UTC](https://discourse.mc-stan.org/t/re-formula-argument-in-fitted-and-predict-fails-for-multimembership-models-with-multimembership-covariates/9719/1 "2019-07-15T08:15:46Z")

</div>

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

```

---

<div class="post-metadata">

**Author:** ![paul.buerkner](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/paul.buerkner/32/3303_2.png) [@paul.buerkner](https://discourse.mc-stan.org/u/paul.buerkner)\
**Post date:** [July 15, 2019, 10:14am UTC](https://discourse.mc-stan.org/t/re-formula-argument-in-fitted-and-predict-fails-for-multimembership-models-with-multimembership-covariates/9719/2 "2019-07-15T10:14:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jackbailey](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jackbailey/32/14978_2.png) [@jackbailey](https://discourse.mc-stan.org/u/jackbailey)\
**Post date:** [July 15, 2019, 12:45pm UTC](https://discourse.mc-stan.org/t/re-formula-argument-in-fitted-and-predict-fails-for-multimembership-models-with-multimembership-covariates/9719/3 "2019-07-15T12:45:31Z")

</div>

Thanks, Paul!
