How to generate serially correlated error terms? I am not sure whether I should put my question here or stack overflow. Please let me know if I am wrong.
I am trying to generate serially correlated error terms such as
$e_t = \rho * e_{t-1} + v_t$ where $v_t\sim N(0, \sigma^2)$ follows normal distribution with zero mean and constant variance. I found the following code but not sure why e[t] <- rho * e[t-1] + e[t] is valid. How can e[t] be repeated? What could be the justification for the simulation?
rho <- 0.9
e <- rnorm(mean = 0, sd = 1, n = T)
for(t in 2:T){
e[t] <- rho * e[t-1] + e[t]
}


If we strictly follow the mathematical formula $e_t = \rho e_{t-1} + v_t$, we would write this code:
e <- numeric(T)
e[1] <- rnorm(1, mean = 0, sd = 1)
v <- rnorm(T, mean = 0, sd = 1)
for(t in 2:T) {
  e[t] <- rho * e[t-1] + v[t]
}

But this code is equivalent to the one in your post. You can use e <- rnorm(T, 0, 1) instead of v <- rnorm(T, 0, 1) because anyway e[t] is replaced with rho * e[t-1] + e[t].
I'm not sure my explanation is good... think about why the two following pieces of code produce the same vector e:
T <- 5
rho <- 0.9

set.seed(666)
e <- numeric(T)
e[1] <- rnorm(1, mean = 0, sd = 1)
v <- rnorm(T, mean = 0, sd = 1)
for(t in 2:T) {
  e[t] <- rho * e[t-1] + v[t]
}
e
# 0.7533110  0.3228455  2.3187288 -0.1300186  0.6413795

set.seed(666)
e1 <- rnorm(1, mean = 0, sd = 1)
e <- rnorm(T, mean = 0, sd = 1)
e[1] <- e1
for(t in 2:T) {
  e[t] <- rho * e[t-1] + e[t]
}
e
# 0.7533110  0.3228455  2.3187288 -0.1300186  0.6413795
```

