Understanding the ar structure

I’m trying to fit an ar1 model. When I fit it in brms and check the stan code I get this in the model section:

// matrix storing lagged residuals
matrix[N, max_lag] Err = rep_matrix(0, N, max_lag);
// initialize linear predictor term
vector[N] mu = Intercept + rep_vector(0.0, N) + err;
// include ARMA terms
for (n in 1:N) {
  for (i in 1:J_lag[n]) {
    Err[n + 1, i] = err[n + 1 - i];
  }
  mu[n] += Err[n, 1:Kar] * ar;
}

However, I see no relationship between this code and the formula in Wikipedia (Autoregressive model - Wikipedia):

X_t = c + p_iX_{t-1} + e_t

In fact, the comment “matrix storing lagged residuals” seems suspicious, since an ar model does not model residuals with lags…

Does anyone know how to interpret the code in terms of the formula? Or is the code wrong?

Thanks,

Edgar

This is because the autocorrelation structures in brms use a slightly different approach, rather than directly using the previous observations

See this forum post for a bit more background: Autocorrelation added to the model results in outrageous computation time - #2 by martinmodrak