# AR(1) in the error matrix rather than in the linear model

**URL:** <https://discourse.mc-stan.org/t/ar-1-in-the-error-matrix-rather-than-in-the-linear-model/21813>\
**Category:** brms\
**Created:** [April 10, 2021, 2:30pm UTC](https://discourse.mc-stan.org/t/ar-1-in-the-error-matrix-rather-than-in-the-linear-model/21813 "2021-04-10T14:30:06Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Solomon](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/solomon/32/2495_2.png) [@Solomon](https://discourse.mc-stan.org/u/Solomon)\
**Post date:** [April 10, 2021, 2:30pm UTC](https://discourse.mc-stan.org/t/ar-1-in-the-error-matrix-rather-than-in-the-linear-model/21813/1 "2021-04-10T14:30:06Z")

</div>

- Operating System: MacOS
- brms Version: 2.15.0

This question is related to the recent thread, [Unstructured error covariance matrix in a multilevel growth model](https://discourse.mc-stan.org/t/unstructured-error-covariance-matrix-in-a-multilevel-growth-model/21792).

The `brms::ar()` function appears to set up autoregressive models following the basic format:

y\_t \sim \operatorname{Normal}(\alpha + \beta y\_{t - 1}, \sigma),

where t indexes _time_ and the autoregressive element is achieved through adding a predictor to the linear model. Is is currently possible to account for autoregression by adding structured correlations to the error matrix rather than adding predictors?

## Use case

As in the previous thread, my use case comes from Singer and Willet (2003, Ch 7). This time, I’m looking to fit the model

\begin{align\*} \text{opp}\_{ij} & \sim \operatorname{Normal}(\mu\_{ij}, \mathbf r) \\ \mu\_{ij} & = \gamma\_{00} + \gamma\_{10} \text{time}\_{ij} + \gamma\_{01} (\text{cog}\_i - \overline{\text{cog}}) + \gamma\_{11} (\text{cog}\_i - \overline{\text{cog}}) \times \text{time}\_{ij} \\ \mathbf r & = \begin{bmatrix} \mathbf{\Sigma\_r} & \mathbf 0 & \mathbf 0 & \dots & \mathbf 0 \\ \mathbf 0 & \mathbf{\Sigma\_r} & \mathbf 0 & \dots & \mathbf 0 \\ \mathbf 0 & \mathbf 0 & \mathbf{\Sigma\_r} & \dots & \mathbf 0 \\ \vdots & \vdots & \vdots & \ddots & \mathbf 0 \\ \mathbf 0 & \mathbf 0 & \mathbf 0 & \mathbf 0 & \mathbf{\Sigma\_r} \end{bmatrix}, \text{where} \\ \mathbf{\Sigma\_r} & = \begin{bmatrix} \sigma^2 & \sigma^2 \rho & \sigma^2 \rho^2 & \sigma^2 \rho^3 \\ \sigma^2 \rho & \sigma^2 & \sigma^2 \rho & \sigma^2 \rho^2 \\ \sigma^2 \rho^2 & \sigma^2 \rho & \sigma^2 & \sigma^2 \rho \\ \sigma^2 \rho^3 & \sigma^2 \rho^2 & \sigma^2 \rho & \sigma^2 \end{bmatrix}. \end{align\*}

Is this currently possible with either `brms::ar()` or some other stragety?

---

<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:** [April 10, 2021, 7:31pm UTC](https://discourse.mc-stan.org/t/ar-1-in-the-error-matrix-rather-than-in-the-linear-model/21813/2 "2021-04-10T19:31:56Z")

</div>

Have you tried ar(cov = TRUE)?

---

<div class="post-metadata">

**Author:** ![Solomon](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/solomon/32/2495_2.png) [@Solomon](https://discourse.mc-stan.org/u/Solomon)\
**Post date:** [April 10, 2021, 7:52pm UTC](https://discourse.mc-stan.org/t/ar-1-in-the-error-matrix-rather-than-in-the-linear-model/21813/3 "2021-04-10T19:52:17Z")

</div>

Hey @paul.buerkner, I have. Here’s a focused summary of the results:

 ![Screen Shot 2021-04-10 at 2.49.11 PM](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/6/647431211eb08148076d41b9a16bc8203d76ddef.png)

The values for Singer and Willett are their point estimates. Those for the two variants fit with **brms** are the posterior medians. I’m not going to pretend I fully understand what **brms** is doing, here, but these methods are clearly eating into the residual variance in a way that the REML method is not.

---

<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:** [April 10, 2021, 7:55pm UTC](https://discourse.mc-stan.org/t/ar-1-in-the-error-matrix-rather-than-in-the-linear-model/21813/4 "2021-04-10T19:55:14Z")

</div>

which package are you using for reml?

---

<div class="post-metadata">

**Author:** ![Solomon](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/solomon/32/2495_2.png) [@Solomon](https://discourse.mc-stan.org/u/Solomon)\
**Post date:** [April 10, 2021, 7:59pm UTC](https://discourse.mc-stan.org/t/ar-1-in-the-error-matrix-rather-than-in-the-linear-model/21813/5 "2021-04-10T19:59:13Z")

</div>

I’m reporting the REML values straight from the text. However, the IDRE folks fit the models using **nlme** at [Applied Longitudinal Data Analysis, Chapter 7 | R Textbook Examples](https://stats.idre.ucla.edu/r/examples/alda/r-applied-longitudinal-data-analysis-ch-7/). Here’s what their model code looked like.

```
auto1 <- gls(data = opposites,
             opp ~ time*ccog,
             correlation = corAR1(, form = ~ 1 | id), 
             method = "REML")

```

---

<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:** [April 10, 2021, 8:07pm UTC](https://discourse.mc-stan.org/t/ar-1-in-the-error-matrix-rather-than-in-the-linear-model/21813/6 "2021-04-10T20:07:51Z")

</div>

nlme uses a slightly different paramerization which leads to sigma being no longer sigma but something like sigma / sqrt(1 - ar^2) or something like that (hence higher sigma being reported for higher ar). I am at my phone so please don’t take my word for the formula.

brms parametizes correctly so that sigma is in fact what we expect it to be in this case.

---

<div class="post-metadata">

**Author:** ![Solomon](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/solomon/32/2495_2.png) [@Solomon](https://discourse.mc-stan.org/u/Solomon)\
**Post date:** [April 10, 2021, 8:51pm UTC](https://discourse.mc-stan.org/t/ar-1-in-the-error-matrix-rather-than-in-the-linear-model/21813/7 "2021-04-10T20:51:22Z")

</div>

Could you share the link where you’re getting that formula from?

---

<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:** [April 10, 2021, 9:03pm UTC](https://discourse.mc-stan.org/t/ar-1-in-the-error-matrix-rather-than-in-the-linear-model/21813/8 "2021-04-10T21:03:25Z")

</div>

I Googled and found that note in some forum 1 or 2 years or so ago.

you can also verify via simulations of a big enough dataset to see how the implementation differ and verify the correctness of my formula on that basis as well.

---

<div class="post-metadata">

**Author:** ![Solomon](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/solomon/32/2495_2.png) [@Solomon](https://discourse.mc-stan.org/u/Solomon)\
**Post date:** [April 10, 2021, 11:11pm UTC](https://discourse.mc-stan.org/t/ar-1-in-the-error-matrix-rather-than-in-the-linear-model/21813/9 "2021-04-10T23:11:47Z")

</div>

Before I dive into a simulation study, it might be helpful if we backed up and clarified a point. When one uses `ar(cov = TRUE)`, is **brms** fitting an autoregressive model where an autoregressive regression term is added to the linear model, such as in the original formula

y\_t \sim \operatorname{Normal}(\alpha + \beta y\_{t - 1}, \sigma),

where I’d expect to see a decrease in the residual standard deviation, or is it adding a correlation parameter to the sigma matrix, such as in my use case,

\begin{align\*} \text{opp}\_{ij} & \sim \operatorname{Normal}(\mu\_{ij}, \mathbf r) \\ \mu\_{ij} & = \gamma\_{00} + \gamma\_{10} \text{time}\_{ij} + \gamma\_{01} (\text{cog}\_i - \overline{\text{cog}}) + \gamma\_{11} (\text{cog}\_i - \overline{\text{cog}}) \times \text{time}\_{ij} \\ \mathbf r & = \begin{bmatrix} \mathbf{\Sigma\_r} & \mathbf 0 & \mathbf 0 & \dots & \mathbf 0 \\ \mathbf 0 & \mathbf{\Sigma\_r} & \mathbf 0 & \dots & \mathbf 0 \\ \mathbf 0 & \mathbf 0 & \mathbf{\Sigma\_r} & \dots & \mathbf 0 \\ \vdots & \vdots & \vdots & \ddots & \mathbf 0 \\ \mathbf 0 & \mathbf 0 & \mathbf 0 & \mathbf 0 & \mathbf{\Sigma\_r} \end{bmatrix}\\ \mathbf{\Sigma\_r} & = \begin{bmatrix} \sigma^2 & \sigma^2 \rho & \sigma^2 \rho^2 & \sigma^2 \rho^3 \\ \sigma^2 \rho & \sigma^2 & \sigma^2 \rho & \sigma^2 \rho^2 \\ \sigma^2 \rho^2 & \sigma^2 \rho & \sigma^2 & \sigma^2 \rho \\ \sigma^2 \rho^3 & \sigma^2 \rho^2 & \sigma^2 \rho & \sigma^2 \end{bmatrix}, \end{align\*}

where I wouldn’t expect a decrease in the residual standard deviation because I haven’t added an autoregressive regression term?

---

<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:** [April 11, 2021, 7:35am UTC](https://discourse.mc-stan.org/t/ar-1-in-the-error-matrix-rather-than-in-the-linear-model/21813/10 "2021-04-11T07:35:56Z")

</div>

it adds a correlation parameter to the sigma matrix. please check the Stan code to verify if you like.

---

<div class="post-metadata">

**Author:** ![Solomon](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/solomon/32/2495_2.png) [@Solomon](https://discourse.mc-stan.org/u/Solomon)\
**Post date:** [April 11, 2021, 3:45pm UTC](https://discourse.mc-stan.org/t/ar-1-in-the-error-matrix-rather-than-in-the-linear-model/21813/11 "2021-04-11T15:45:49Z")

</div>

Thank you for the response. I’ve checked the Stan code, but my abilities to read it are quite limited.

It still seems like we’re talking past each other a bit, so let’s simplify the model and the data structure. After making the data, I’ll try to illustrate my basic concern with two simple model. Then I’ll extend the point to variants using `ar()` and REML.

### Data.

Here we make standard bivariate normal data,

\begin{bmatrix} y1 \\ y2 \end{bmatrix} \sim \operatorname{Normal} \left ( \begin{bmatrix} 0 \\ 0 \end{bmatrix}, \begin{bmatrix} 1 & .75 \\ .75 & 1 \end{bmatrix} \right ),

for which N = 10{,}000. We’ll save the data in both the wide (`d_wide`) and long (`d_long`) formats.

```no-highlight
# load
library(tidyverse)
library(brms)
library(nlme)

# total sample
n <- 1e4

# poplation correlation
rho <- 0.75

# the variance/covariance matrix
Sigma <- 
  matrix(c(1, rho, rho, 1),
         nrow = 2, byrow = T)

# make a wide data set where you might think of
# y1 and y2 as y values at two time points
set.seed(1)

d_wide <- 
  mvtnorm::rmvnorm(n = n, sigma = Sigma) %>% 
  data.frame() %>% 
  set_names(c("y1", "y2")) %>% 
  mutate(id = 1:n())

# make a long version of the data with a single criterion called y,
# which is measured at time == 0 and time == 1
d_long <-
  d_wide %>% 
  pivot_longer(-id, values_to = "y") %>% 
  mutate(time = ifelse(name == "y1", 0, 1))

```

The data have the sample statistics one would expect.

```no-highlight
d_wide %>% 
  summarise(mean1 = mean(y1),
            mean2 = mean(y2),
            sd1 = sd(y1),
            sd2 = sd(y2),
            p = cor(y1, y2))

```

```no-highlight
        mean1 mean2 sd1 sd2 p
1 -0.01297871 -0.001211921 1.00779 0.9971651 0.7511297

```

### Simple models.

We’ll start by fitting two simple models. `fit1` is a bivariate normal model with no predictors. Thus the \Sigma matrix is reproduced in the parameters. `fit2` is captures the correlation by letting `y1` predict `y2`. However, \sigma\_2 is now a residual correlation, and thus quite a bit lower (0.66) than it was in `fit1`.

```no-highlight
# bivariate
fit1 <-
  brm(data = d_wide,
      bf(y1 ~ 1) + bf(y2 ~ 1) + set_rescor(rescor = TRUE))

# pre predicting post
fit2 <-
  brm(data = d_wide,
      y2 ~ 1 + y1)

```

This exercize is meant to convey my understanding that when a correlation is expressed within the \Sigma matrix (`fit1`), I would not expect it to eat into the residual variance the way it would when it is expressed, instead, within the linear model (`fit2`).

Now we get more to the point.

### `ar()`.

Instead of fitting AR(1) models with homogeneous variances, as before, I’m going to switch to heterogeneous variances, which will help showcase the issue.

```no-highlight
fit3 <-
  brm(data = d_long,
      bf(y ~ time + ar(gr = id),
         sigma ~ 0 + factor(time)))

```

This does a great job capturing \rho.

```no-highlight
posterior_summary(fit3)["ar[1]", -2] %>% round(digits = 2)

```

```no-highlight
Estimate Q2.5 Q97.5 
    0.74 0.73 0.76 

```

But look at what happens with the \sigma parameters.

```no-highlight
fixef(fit3)[3:4, -2] %>% exp() %>% round(digits = 2)

```

```no-highlight
                  Estimate Q2.5 Q97.5
sigma_factortime0 1.01 0.99 1.02
sigma_factortime1 0.66 0.65 0.67

```

Whereas the first is a standard deviation, the second is a residual standard deviation.

### `ar(cov = TRUE)`.

Now we explore the results when we set `cov = TRUE` within the `ar()` function.

```no-highlight
fit4 <-
  brm(data = d_long,
      bf(y ~ time + ar(gr = id, cov = TRUE),
         sigma ~ 0 + factor(time)))

```

As with the previous model, this one does a great job capturing \rho. For the sake of space, I’ll omit the parameter summary. Of greater interest is what happens to the \sigma parameters.

```no-highlight
fixef(fit4)[3:4, -2] %>% exp() %>% round(digits = 2)

```

```no-highlight
                  Estimate Q2.5 Q97.5
sigma_factortime0 0.67 0.66 0.67
sigma_factortime1 0.66 0.65 0.67

```

Now they both appear to be in a residual variance metric. Keeping this in mind, consider what happens when we switch to **nlme**.

### nlme with `corAR1()` and `varIdent()`.

If you look back at the workflow on the IDRE site, [Applied Longitudinal Data Analysis, Chapter 7 | R Textbook Examples](https://stats.idre.ucla.edu/r/examples/alda/r-applied-longitudinal-data-analysis-ch-7/), you’ll see one can use **nlme** to fit an AR(1) model with heterogeneous variances using a combination of the `corAR1()` and `varIdent()` functions. Here’s an adaptation of their code applied to our simulated data.

```no-highlight
fit5 <- gls(data = d_long,
             y ~ time,
             correlation = corAR1(, form = ~ 1 | id), 
             weights = varIdent(form = ~1 | time),
             method = "REML")

```

Inspect the results.

```no-highlight
print(fit5)

```

```no-highlight
Generalized least squares fit by REML
  Model: y ~ time 
  Data: d_long 
  Log-restricted-likelihood: -24281.95

Coefficients:
(Intercept) time 
-0.01297871 0.01176678 

Correlation Structure: AR(1)
 Formula: ~1 | id 
 Parameter estimate(s):
      Phi 
0.7511297 
Variance function:
 Structure: Different standard deviations per stratum
 Formula: ~1 | time 
 Parameter estimates:
       0 1 
1.000000 0.989457 
Degrees of freedom: 20000 total; 19998 residual
Residual standard error: 1.00779 

```

I can’t speak to [the equation](https://discourse.mc-stan.org/t/ar-1-in-the-error-matrix-rather-than-in-the-linear-model/21813/6) the **nlme** package used to compute those \sigma parameters. But it appears, to me, that these residual variances are what I’d expect when (a) the coefficient for `time` was near zero and the \rho parameter was not eating into the residual variance. This also seems consistent with the models Singer and Willett were discussing in their text. That is, when I fit the model

\begin{align\*} y\_{ij} & \sim \operatorname{Normal} (\mu\_{ij}, \mathbf r) \\ \mu\_{ij} & = \beta\_0 + \beta\_1 \text{time}\_{ij} \\ \mathbf r & = \begin{bmatrix} \mathbf{\Sigma\_r} & \mathbf 0 & \dots & \mathbf 0 \\ \mathbf 0 & \mathbf{\Sigma\_r} & \dots & \mathbf 0 \\ \vdots & \vdots & \ddots & \mathbf 0 \\ \mathbf 0 & \mathbf 0 & \mathbf 0 & \mathbf{\Sigma\_r} \end{bmatrix} \\ \mathbf{\Sigma\_r} & = \begin{bmatrix} \sigma\_1^2 & \sigma\_1 \sigma\_2 \rho \\ \sigma\_1 \sigma\_2 \rho & \sigma\_2^2 \end{bmatrix}, \end{align\*}

where we already know that \beta\_1 = 0 because of how we simulated the data, I would expect the \sigma\_1 and \sigma\_2 parameters to be very close to 1.

So I suppose this leads me back to my original question: Can one fit this model with **brms** , whether by using the `ar()` function or by some other means?

---

<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:** [April 11, 2021, 6:44pm UTC](https://discourse.mc-stan.org/t/ar-1-in-the-error-matrix-rather-than-in-the-linear-model/21813/12 "2021-04-11T18:44:13Z")

</div>

What you want in your model is in fact an unstructured residual correlation matrix, which as discussed in the other post, is something that requires a new `unst()` correlation structure to work in general…

For your simple example with ntimes = 2, you can use ar(cov = TRUE) and get back to the metric you want by comptung `sigma_new = sigma / sqrt(1 - ar^2)` (on the level of posterior samples). You see this being discussed in more detail on slide 8 ff. in [http://bstt513.class.uic.edu/ACerrorsLS.pdf](http://bstt513.class.uic.edu/ACerrorsLS.pdf)

Does that resolve your question?

---

<div class="post-metadata">

**Author:** ![Solomon](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/solomon/32/2495_2.png) [@Solomon](https://discourse.mc-stan.org/u/Solomon)\
**Post date:** [April 12, 2021, 6:03pm UTC](https://discourse.mc-stan.org/t/ar-1-in-the-error-matrix-rather-than-in-the-linear-model/21813/13 "2021-04-12T18:03:07Z")

</div>

Thanks @paul.buerkner. Yes, in the special case of two time point data, the heterogeneous autoregressive and unstructured \mathbf{\Sigma\_r} matrices are the same. But this breaks down as soon as you have three time points or larger, which is closer to my original use case. For posterity, I’ll walk that through.

Simulate 3-time point data.

```
# load
library(tidyverse)
library(brms)
library(patchwork)

# total sample
n <- 5e3

# poplation values
s1 <- 1 # standard deviations
s2 <- 2
s3 <- 3
rho <- 0.75 # correlation

# the AR1 variance/covariance matrix
Sigma <- 
  matrix(c(s1 * s1, s1 * s2 * rho, s1 * s3 * rho^2,
           s2 * s1 * rho, s2 * s2, s2 * s3 * rho,
           s3 * s1 * rho^2, s3 * s2 * rho, s3 * s3),
         nrow = 3, byrow = T)

# simulate and wrangle the results into the long format
set.seed(1)

d_long <-
  mvtnorm::rmvnorm(n = n, sigma = Sigma) %>% 
  data.frame() %>% 
  set_names(c("y1", "y2", "y3")) %>% 
  mutate(id = 1:n()) %>% 
  pivot_longer(-id, values_to = "y") %>% 
  mutate(time = ifelse(name == "y1", 0, 
                       ifelse(name == "y2", 1, 2)))

```

Fit the AR(1) models with both the homogeneous and heterogeneous variaces using the `ar(gr = id, cov = TRUE)` syntax.

```
# AR(1) with homogeneous variances
fit6 <-
  brm(data = d_long,
      y ~ time + ar(gr = id, cov = TRUE))

# AR(1) with heterogeneous variances
fit7 <-
  brm(data = d_long,
      bf(y ~ time + ar(gr = id, cov = TRUE),
         sigma ~ 0 + factor(time)))

```

Extract the posterior draws and wrangle, using Paul’s formula.

```
sigma <-
  bind_rows(
    # homogeneous variances
    posterior_samples(fit6) %>% 
      transmute(rho = `ar[1]`,
                `sigma[1]` = sigma / sqrt(1 - `ar[1]`^2),
                `sigma[2]` = sigma / sqrt(1 - `ar[1]`^2),
                `sigma[3]` = sigma / sqrt(1 - `ar[1]`^2)),
    # heterogeneous variances
    posterior_samples(fit7) %>% 
      transmute(rho = `ar[1]`,
                `sigma[1]` = exp(b_sigma_factortime0) / sqrt(1 - `ar[1]`^2),
                `sigma[2]` = exp(b_sigma_factortime1) / sqrt(1 - `ar[1]`^2),
                `sigma[3]` = exp(b_sigma_factortime2) / sqrt(1 - `ar[1]`^2))
  ) %>% 
  transmute(`sigma[1]^2` = `sigma[1]`^2,
            `sigma[2]^2` = `sigma[2]`^2,
            `sigma[3]^2` = `sigma[3]`^2,
            `sigma[2]*sigma[1]*rho` = `sigma[2]` * `sigma[1]` * rho,
            `sigma[3]*sigma[1]*rho^2` = `sigma[3]` * `sigma[1]` * rho^2,
            `sigma[3]*sigma[2]*rho` = `sigma[3]` * `sigma[2]` * rho,
            `sigma[1]*sigma[2]*rho` = `sigma[1]` * `sigma[2]` * rho,
            `sigma[1]*sigma[3]*rho^2` = `sigma[1]` * `sigma[3]` * rho^2,
            `sigma[2]*sigma[3]*rho` = `sigma[2]` * `sigma[3]` * rho)

```

Plot the results.

```
levels <- c("sigma[1]^2", "sigma[1]*sigma[2]*rho", "sigma[1]*sigma[3]*rho^2",
            "sigma[2]*sigma[1]*rho", "sigma[2]^2", "sigma[2]*sigma[3]*rho",
            "sigma[3]*sigma[1]*rho^2", "sigma[3]*sigma[2]*rho", "sigma[3]^2")

p1 <-
  sigma %>% 
  slice(1:4000) %>% 
  pivot_longer(everything()) %>% 
  group_by(name) %>% 
  summarise(median = median(value)) %>% 
  mutate(cell = factor(name, levels = levels),
         label = round(median, digits = 1)) %>% 
  
  ggplot(aes(x = 0, y = 0, fill = median, label = label)) +
  geom_tile() +
  geom_text(color = "white") +
  scale_x_continuous(NULL, breaks = NULL, expand = c(0, 0)) +
  scale_y_continuous(NULL, breaks = NULL, expand = c(0, 0)) +
  scale_fill_viridis_c(option = "A", limits = c(0, 10), breaks = NULL) +
  labs(subtitle = "AR1 with homogeneous variances") +
  facet_wrap(~ cell, labeller = label_parsed)

p2 <-
  sigma %>% 
  slice(4001:8000) %>% 
  pivot_longer(everything()) %>% 
  group_by(name) %>% 
  summarise(median = median(value)) %>% 
  mutate(cell = factor(name, levels = levels),
         label = round(median, digits = 1)) %>% 
  
  ggplot(aes(x = 0, y = 0, fill = median, label = label)) +
  geom_tile() +
  geom_text(aes(color = median < 5)) +
  scale_color_manual(values = c("black", "white"), breaks = NULL) +
  scale_x_continuous(NULL, breaks = NULL, expand = c(0, 0)) +
  scale_y_continuous(NULL, breaks = NULL, expand = c(0, 0)) +
  scale_fill_viridis_c(option = "A", limits = c(0, 10), breaks = NULL) +
  labs(subtitle = "AR1 with heterogeneous variances") +
  facet_wrap(~ cell, labeller = label_parsed)

# combine
p1 + p2 + plot_annotation(title = expression("Posterior medians for "*Sigma[r]))

```

 ![Screen Shot 2021-04-12 at 12.58.25 PM](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/1/14559f220b2da25f8eff4d236dd66014afdfa01f.png)

For comparison, here’s the data-generating variance/covariance matrix.

```
Sigma %>% round(digits = 1)

```

```
     [,1] [,2] [,3]
[1,] 1.0 1.5 1.7
[2,] 1.5 4.0 4.5
[3,] 1.7 4.5 9.0

```

Conceptually, the **brms** parameterization still doesn’t make sense, to me. But I’m glad I understand the parameters well enough to transform them into the metric I’m looking for.

---

<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:** [April 12, 2021, 6:19pm UTC](https://discourse.mc-stan.org/t/ar-1-in-the-error-matrix-rather-than-in-the-linear-model/21813/14 "2021-04-12T18:19:58Z")

</div>

indeed please dont use ar() for your 3 time+ use case if your actual target is an unstr matrix. I didn’t mean to imply that. I just used the 2 times procedure to explain to you what the ar() is doing in a case where we have an equivalence.

---

<div class="post-metadata">

**Author:** ![Solomon](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/solomon/32/2495_2.png) [@Solomon](https://discourse.mc-stan.org/u/Solomon)\
**Post date:** [April 12, 2021, 6:27pm UTC](https://discourse.mc-stan.org/t/ar-1-in-the-error-matrix-rather-than-in-the-linear-model/21813/15 "2021-04-12T18:27:33Z")

</div>

To clarify, do you mean “please dont use [unst()] for your 3 time+ use case”? The ar() function works great. Also, I almost forgot to say thank you for sharing the link to the slides.

---

<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:** [April 12, 2021, 6:36pm UTC](https://discourse.mc-stan.org/t/ar-1-in-the-error-matrix-rather-than-in-the-linear-model/21813/16 "2021-04-12T18:36:51Z")

</div>

No I just ment that one should not use ar() if what one actually wants is an unstr() matrix for 3+ time points.

---

<div class="post-metadata">

**Author:** ![Solomon](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/solomon/32/2495_2.png) [@Solomon](https://discourse.mc-stan.org/u/Solomon)\
**Post date:** [April 12, 2021, 6:38pm UTC](https://discourse.mc-stan.org/t/ar-1-in-the-error-matrix-rather-than-in-the-linear-model/21813/17 "2021-04-12T18:38:46Z")

</div>

That makes sense. Cheers!
