# Matrix algebra: parameter with two indices

**URL:** <https://discourse.mc-stan.org/t/matrix-algebra-parameter-with-two-indices/5095>\
**Category:** Modeling\
**Created:** [August 6, 2018, 8:17pm UTC](https://discourse.mc-stan.org/t/matrix-algebra-parameter-with-two-indices/5095 "2018-08-06T20:17:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jgellar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jgellar/32/2917_2.png) [@jgellar](https://discourse.mc-stan.org/u/jgellar)\
**Post date:** [August 6, 2018, 8:17pm UTC](https://discourse.mc-stan.org/t/matrix-algebra-parameter-with-two-indices/5095/1 "2018-08-06T20:17:40Z")

</div>

I have a model with a coefficient (`theta`) that I want to vary by two other categorical variables, `group` and `time`. Suppose there are `G=20` groups and `T=10` time points, then I can declare the `theta` parameter as

```stan
  matrix[T, G] theta ; 

```

The `group` and `time` indices are passed in as data, as integer vectors of length `N` (number of observations). In the model step, the easy way to index the particular `theta` element that corresponds to each observation would be with a `for` loop in the `model` step, e.g.

```stan
real theta_use[N] ;
for(n in 1:N){
   theta_use[n] = theta[time[n], group[n]];
}

```

It seems this could be done more efficiently with matrix algebra, but I can’t figure out the best way. Any suggestions? I was thinking that I could pass in (from R) two “map” matrices of dummy variables, `time_map` and `group_map`, that are N\times T and N\times G respectively (no intercept). I’m just not sure what to do with these in Stan.

FWIW, this is just a small part of a much larger program, but I eliminated the rest of the details as they are not necessary for understanding this issue.

Thanks,  
Jon

---

<div class="post-metadata">

**Author:** ![bbbales2](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bbbales2/32/77_2.png) [@bbbales2](https://discourse.mc-stan.org/u/bbbales2)\
**Post date:** [August 7, 2018, 8:17am UTC](https://discourse.mc-stan.org/t/matrix-algebra-parameter-with-two-indices/5095/2 "2018-08-07T08:17:19Z")

</div>

I think for what you describe, the code you wrote is better. Doing row/element selections with linear algebra operations isn’t gonna be efficient. You’d be multiplying by vectors of mostly zeros.

---

<div class="post-metadata">

**Author:** ![jgellar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jgellar/32/2917_2.png) [@jgellar](https://discourse.mc-stan.org/u/jgellar)\
**Post date:** [August 7, 2018, 1:42pm UTC](https://discourse.mc-stan.org/t/matrix-algebra-parameter-with-two-indices/5095/3 "2018-08-07T13:42:53Z")

</div>

I did some tests in Stan and it looks like you’re right. In R the opposite is true - the linear algebra is much more efficient than the for loop. But I think that’s because for loops are particularly slow in R.

Oh well.

---

<div class="post-metadata">

**Author:** ![Bob\_Carpenter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bob_carpenter/32/9230_2.png) [@Bob\_Carpenter](https://discourse.mc-stan.org/u/Bob_Carpenter)\
**Post date:** [August 15, 2018, 12:28am UTC](https://discourse.mc-stan.org/t/matrix-algebra-parameter-with-two-indices/5095/4 "2018-08-15T00:28:20Z")

</div>

> [@jgellar](#):
>
> But I think that’s because for loops are particularly slow in R.

Yes, loops are slow in R. Stan just compiles straight to C++. There’s a chapter in the manual on efficiency that explains why — the autodiff overhead of the multiply-by-zero in memory and time is high. But looping is fast.
