# Non-centered parameterization with group-specific random effects?

**URL:** <https://discourse.mc-stan.org/t/non-centered-parameterization-with-group-specific-random-effects/3409>\
**Category:** Modeling\
**Tags:** specification\
**Created:** [February 27, 2018, 4:09am UTC](https://discourse.mc-stan.org/t/non-centered-parameterization-with-group-specific-random-effects/3409 "2018-02-27T04:09:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shauna\_Sweet](https://avatars.discourse-cdn.com/v4/letter/s/cdc98d/32.png) [@Shauna\_Sweet](https://discourse.mc-stan.org/u/Shauna_Sweet)\
**Post date:** [February 27, 2018, 4:09am UTC](https://discourse.mc-stan.org/t/non-centered-parameterization-with-group-specific-random-effects/3409/1 "2018-02-27T04:09:05Z")

</div>

Good evening,

This post considers the same program as found in my previous [post](http://discourse.mc-stan.org/t/help-vectorizing-a-cross-classified-model/3296/11), but looks to build on the vectorized code and non-centered parameterizations.

In that code, the model is specified such that the variance across elements within each group (here, items within family) is the same across all groups:

```
parameters {
...
  real<lower=0> sigma_item;
  vector[K] betai_offset;
...
transformed parameters{
...
  vector[K] betai;
  betai = family_mu[parent] + betai_offset*sigma_item; 
...
model { 
  betai_offset ~ normal(0,1);
...
```

I would like to specify the model so that variances are group-specific, e.g., ` vector [P] sigma_item `. I have not seen in any of the posts here how to specify NCP when the variances are group-specific, and I am wondering how to do this.

Would an appropriate approach look something like this, where ` matrix[K,P] betai_offset ` is a diagonal matrix? …

```
parameters {
...
  vector [P]<lower=0> sigma_item;
  matrix[K,P] betai_offset;
...
transformed parameters{
...
  vector[K] betai;
  betai = family_mu[parent] + sigma_item*betai_offset; 
...
model { 
  betai_offset ~ normal(0,1);
...
```

if ` betai_offset ` needs to be a diagonal matrix, is there a clean way to specify this in the code?

Thank you for any suggestions.

---

<div class="post-metadata">

**Author:** ![martinmodrak](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/martinmodrak/32/133_2.png) [@martinmodrak](https://discourse.mc-stan.org/u/martinmodrak)\
**Post date:** [February 28, 2018, 5:11pm UTC](https://discourse.mc-stan.org/t/non-centered-parameterization-with-group-specific-random-effects/3409/2 "2018-02-28T17:11:19Z")

</div>

I don’t really understand your model, but it seems that you just need to multiply each `betaai_offset` with the correct element of `sigma_item`, so you might need something like:

```
parameters {
...
  vector [P]<lower=0> sigma_item;
  vector[K] betai_offset;
...
transformed parameters{
...
  vector[K] betai;
  betai = family_mu[parent] + betai_offset .* sigma_item[parent]; 
...
model { 
  betai_offset ~ normal(0,1);
...

```

Note the `.*` operator which indicates element-wise multiplication (i-th element of result is simply the i-th element of `betai_offset` multipled by i-th element of `sigma_item[parent]`).  
That is in contrast to `*` which defaults to matrix multiplication (or dot product for vectors). This might be confusing, becaus it is the exact opposite of R, where `*` defaults to element-wise multiplication and a special operator `%*%` is reserved for matrix multiplication.

---

<div class="post-metadata">

**Author:** ![Shauna\_Sweet](https://avatars.discourse-cdn.com/v4/letter/s/cdc98d/32.png) [@Shauna\_Sweet](https://discourse.mc-stan.org/u/Shauna_Sweet)\
**Post date:** [February 28, 2018, 8:23pm UTC](https://discourse.mc-stan.org/t/non-centered-parameterization-with-group-specific-random-effects/3409/3 "2018-02-28T20:23:29Z")

</div>

Hello @martinmodrak,

I appreciate your comment - I think I was definitely overthinking and overcomplicating the proposed model. I will work through your suggestion with some simulated data, and once it is debugged post the code in full here. Perhaps it will provide more context, and also it may be helpful to folks in the future as well - in case anyone has a similar specification.

Thank you!  
S
