Solved – How to simulate regression residuals with serial correlation or autocorrelation

autocorrelationrregressiontime series

I can easily simulate a data set where the regression residuals are iid with 0 mean and constant variance.

set.seed(123)
x = rnorm(100)
y = 50 + 25* x + rnorm(100)

df = data.frame(y=y, x=x)

The data assumes that the population regression equation is $$y = \beta_0 + \beta_1X_1 + \epsilon$$

And $\epsilon$ is white noise (0 mean, constant variance).

I want to simulate a data set where the residuals are correlated and assume the following population regression equation
$$
\begin{aligned}
y_t &= \beta_0 + \beta_1X_{1,t} + \epsilon_t, \\
\epsilon_t &= \rho\epsilon_{t-1} + v_t, \\
\end{aligned}
$$
and $v_t$ is white noise (0 mean, constant variance).

How would I create this data frame in R?

Best Answer

Found it.

set.seed(123)

ar.epsilon <- arima.sim(list(order = c(1,0,0), ar = 0.7), n = 200, sd=20)

plot(ar.epsilon)

x=rnorm(200)

y = 50 + 25*x + ar.epsilon

df2 = data.frame(x=x, y=y)

lm.mod <- lm(y~., data=df2)

summary(lm.mod)
Related Question