# Correlated errors in a factor model

**URL:** <https://discourse.mc-stan.org/t/correlated-errors-in-a-factor-model/27611>\
**Category:** Modeling\
**Created:** [May 24, 2022, 10:13am UTC](https://discourse.mc-stan.org/t/correlated-errors-in-a-factor-model/27611 "2022-05-24T10:13:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![JohnDoe](https://avatars.discourse-cdn.com/v4/letter/j/65b543/32.png) [@JohnDoe](https://discourse.mc-stan.org/u/JohnDoe)\
**Post date:** [May 24, 2022, 10:13am UTC](https://discourse.mc-stan.org/t/correlated-errors-in-a-factor-model/27611/1 "2022-05-24T10:13:40Z")

</div>

Consider a standard factor model such as the one taken from [this blog](https://agabrioblog.onrender.com/tutorial/factorial-anova-stan/factorial-anova-stan/):

```nohighlight
data {
  int<lower=1> n;
  int<lower=1> nX;
  vector [n] y;
  matrix [n,nX] X;
}

parameters {
  vector[nX] beta;
  real<lower=0> sigma;
}

transformed parameters {
  vector[n] mu;
  mu = X*beta;
}

model {
  // Likelihood
  y~normal(mu,sigma);
  // Priors
  beta ~ normal(0,100);
  sigma~cauchy(0,5);
}

generated quantities {
  vector[n] log_lik;
   for (i in 1:n) {
   log_lik[i] = normal_lpdf(y[i] | mu[i], sigma); 
   }
  }

```

Now consider that one wants to add correlated residuals (error terms) between items in this model. What would be the best way to do so? Do you need to literally calculate the residuals in the `generated quantities` block and then assume a multivariate normal distribution for them in the `model` block? Or should this be specified in some other way?

---

<div class="post-metadata">

**Author:** ![edm](https://avatars.discourse-cdn.com/v4/letter/e/f08c70/32.png) [@edm](https://discourse.mc-stan.org/u/edm)\
**Post date:** [May 27, 2022, 3:41pm UTC](https://discourse.mc-stan.org/t/correlated-errors-in-a-factor-model/27611/2 "2022-05-27T15:41:35Z")

</div>

John, I don’t think it is possible to model things that are defined in generated quantities. If you are looking to have just a few correlated residuals, you might be able to add something to `mu` as shown [here](https://agabrioblog.onrender.com/tutorial/autocorrelation-stan/autocorrelation-stan/) (scroll down to the “Incorporating lagged residuals” section).

If you want all the residuals to be correlated, you probably have to move to a multivariate normal distribution, where `sigma` goes down the diagonal of the covariance matrix and off-diagonal entries are for your residual covariances.
