# Disable non-centered parameterization in brms

**URL:** <https://discourse.mc-stan.org/t/disable-non-centered-parameterization-in-brms/7184>\
**Category:** brms\
**Created:** [January 7, 2019, 4:05pm UTC](https://discourse.mc-stan.org/t/disable-non-centered-parameterization-in-brms/7184 "2019-01-07T16:05:58Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Henrik\_Singmann](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/henrik_singmann/32/119_2.png) [@Henrik\_Singmann](https://discourse.mc-stan.org/u/Henrik_Singmann)\
**Post date:** [January 7, 2019, 4:05pm UTC](https://discourse.mc-stan.org/t/disable-non-centered-parameterization-in-brms/7184/1 "2019-01-07T16:05:58Z")

</div>

We are currently working on a new `custom_family` for `brms`. One important part of this family is the ability to specify specific priors for the regression parameters. For this, we need to be able to use the design matrix in exactly the way we pass it without centering. Take a look at this simple example:

```
data("Machines", package = "MEMSS")
library("brms")
form1 <- bf(score ~ Machine + (1|Worker))
make_stancode(form1, Machines)

```

From this we get the following in the transformed data block that we do not want:

```
transformed data { 
  int Kc = K - 1; 
  matrix[N, K - 1] Xc; // centered version of X 
  vector[K - 1] means_X; // column means of X before centering 
  for (i in 2:K) { 
    means_X[i - 1] = mean(X[, i]); 
    Xc[, i - 1] = X[, i] - means_X[i - 1]; 
  } 
} 

```

We have figured out that without the random part, this could work with `cmc = FALSE`. However, this fails with a random part.

Works:

```
form2 <- bf(score ~ 0 + Machine, cmc = FALSE)
make_stancode(form2, Machines)

```

Fails:

```
form3 <- bf(score ~ 0 + Machine + (1|Worker), cmc = FALSE)
make_stancode(form3, Machines)
# Error in data.frame(id = re$id[[i]], group = re$group[[i]], gn = re$gn[[i]], : 
# arguments imply differing number of rows: 1, 0

```

For us it seems that the best possibility would be a direct option to disable the automatic centering of the predictors (our design matrix is essentially centered). But any work around to get rid of the part in the transformed parameters block would probably work as well.

---

<div class="post-metadata">

**Author:** ![Henrik\_Singmann](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/henrik_singmann/32/119_2.png) [@Henrik\_Singmann](https://discourse.mc-stan.org/u/Henrik_Singmann)\
**Post date:** [January 8, 2019, 11:09am UTC](https://discourse.mc-stan.org/t/disable-non-centered-parameterization-in-brms/7184/2 "2019-01-08T11:09:29Z")

</div>

@paul.buerkner It seems we have found a fix for this issue by always setting `cmc` to `TRUE` for the random effects. See [here for the actual fix in the code](https://github.com/singmann/brms/commit/57c9c13c1fb257e6877d21c12a74ea1c2d89cdf2#diff-788bf47ae804d6ccd3f846f024a7ec5f).

I am happy to submit a pull request with this fix. However, there of course might be situations where `cmc = TRUE` in the random effects. But at least not in the current situation.

---

<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:** [January 8, 2019, 11:40am UTC](https://discourse.mc-stan.org/t/disable-non-centered-parameterization-in-brms/7184/3 "2019-01-08T11:40:35Z")

</div>

thanks! I am still on vacation and will take a look in a few days.

---

<div class="post-metadata">

**Author:** ![Henrik\_Singmann](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/henrik_singmann/32/119_2.png) [@Henrik\_Singmann](https://discourse.mc-stan.org/u/Henrik_Singmann)\
**Post date:** [January 8, 2019, 2:37pm UTC](https://discourse.mc-stan.org/t/disable-non-centered-parameterization-in-brms/7184/4 "2019-01-08T14:37:29Z")

</div>

I might have spoked too early. While this fix solves the problem for our case, it leads to some failed unit tests: `tests.make_standata.R:613` and `tests.make_standata.R:614`

As I am not the expert on the internals, I am not sure I feel confident to provide a fix that considers all aspects of what `cmc` is actually useful for. One possibility would be to allow the user to set `cmc` independently for both fixed and random effects.

---

<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:** [January 9, 2019, 1:05pm UTC](https://discourse.mc-stan.org/t/disable-non-centered-parameterization-in-brms/7184/5 "2019-01-09T13:05:42Z")

</div>

I just fixed this issue in the github version of brms.

---

<div class="post-metadata">

**Author:** ![Dillon\_Fitch](https://avatars.discourse-cdn.com/v4/letter/d/858c86/32.png) [@Dillon\_Fitch](https://discourse.mc-stan.org/u/Dillon_Fitch)\
**Post date:** [February 5, 2019, 6:21pm UTC](https://discourse.mc-stan.org/t/disable-non-centered-parameterization-in-brms/7184/6 "2019-02-05T18:21:04Z")

</div>

Related question. Is there any way to get brms to use a “centered” parameterization? What I mean is no:

transformed parameters {  
// group-level effects  
vector[N\_1] r\_1\_1 = sd\_1[1] \* (z\_1[1]);  
}

just declaring r\_1\_1 in parameters and using directly without scaling by sd\_1. Stan manual suggests this is more efficient in large data settings.

---

<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:** [February 5, 2019, 6:26pm UTC](https://discourse.mc-stan.org/t/disable-non-centered-parameterization-in-brms/7184/7 "2019-02-05T18:26:06Z")

</div>

brms does not support the centered parameterization for random effects as, for the majority of cases, the non-centered parameterization leads to better convergence and also generalizes to non-hierarchical random effects structures.

---

<div class="post-metadata">

**Author:** ![Dillon\_Fitch](https://avatars.discourse-cdn.com/v4/letter/d/858c86/32.png) [@Dillon\_Fitch](https://discourse.mc-stan.org/u/Dillon_Fitch)\
**Post date:** [February 5, 2019, 6:35pm UTC](https://discourse.mc-stan.org/t/disable-non-centered-parameterization-in-brms/7184/8 "2019-02-05T18:35:41Z")

</div>

thanks Paul. I have a case where my model seems fine with a sub sample of data, but “gets stuck” (i.e. runtime disproportionately longer than the increase in data would suggest it should be) with the full sample, so I was just curious about trying a centered parameterization. I’ll post the modeling issue on a separate thread.
