# Creating auto-correlated random values in R

We are trying to create auto-correlated random values which will be used as timeseries. We have no existing data we refer to and just want to create the vector from scratch.

On the one hand we need of course a random process with distribution and its SD.

On the other hand the autocorrelation influencing the random process has to be described. The values of the vector are autocorrelated with decreasing strength over several timelags. e.g. lag1 has 0.5, lag2 0.3, lag1 0.1 etc.

So in the end the vector should look something that: 2, 4, 7, 11, 10 , 8 , 5, 4, 2, -1, 2, 5, 9, 12, 13, 10, 8, 4, 3, 1, -2, -5

and so on.

I actually often run into that problem. My two favorite ways to generate a time series with auto-correlation in R depend on if I want a stationary process or not.

For a non stationary time series I use a Brownian motion. For example, for a length 1000 I do:

x <- diffinv(rnorm(999))


For a stationary time series I filter a Gaussian noise. For example this looks like:

x <- filter(rnorm(1000), filter=rep(1,3), circular=TRUE)


In that case, the auto-correlation at lag $\tau$ is 0 if $\tau > 2$. In other cases, we have to compute the correlation between sums of variables. For example for $\tau = 1$ the covariance is

$$Cov(X_1;X_2) = Cov(Y_1+Y_2+Y_3; Y_2+Y_3+Y_4) = Var(Y_2) + Var(Y_3) = 2.$$

So you see that the auto-covariance drops down linearly up until $n$ where $n$ is the length of the filter.

You can also want to do long memory time series (like fractional Brownian motion), but this is more involved. I have an R implementention of the Davies-Harte method that I can send you if you wish.

• To get realizations of long memory time series I would recommend Wornell's 1996 book (link : books.google.cl/books/about/… ), :-). Although the tractability of long memory processes it's not "that" easy, you can still do it. – Néstor May 27 '12 at 18:54
• I used your approach and it works in general, but I get slight deviations between the target function used in the filter and the resulting autocorrelation function. Please have a look at this question: stats.stackexchange.com/questions/176722/… – nnn Oct 13 '15 at 10:22

If you have a given autocovariance function, the best model (in terms of tractability) I can think of is a multivariate gaussian process, where, given the autocovariance function $R(\tau)$ at lag $\tau$, you can form the covariance matrix easily,

$$\Sigma=\left[ \begin{array}{cccc} R(0) & R(1) & ... & R(N) \\ R(1) & R(0) & ... & R(N-1) \\ \vdots & \ddots & ... & \vdots \\ R(N) & R(N-1) & ... & R(0) \end{array} \right]$$

Given this covariance matrix, you sample data from a multivariate gaussian with the given covariance matrix $\Sigma$, i.e., sample a vector from the distribution $$f(\vec{x})=\frac{1}{(2\pi)^{N/2}|\Sigma|^{1/2}}\text{exp}\left(-\frac{1}{2}(\vec{x}-\vec{\mu})^T\Sigma^{-1}(\vec{x}-\vec{\mu})\right)\text{,}$$ where $\vec{\mu}$ is the mean vector.

You can generate a correlated sequence by constructing an autoregressive process For example an AR(1) process $$X(t)=a X(t-1) + e(t)$$. Generate $$e(0)$$ using a uniform random number generator for your chosen distribution. Then Let $$X(0) = e(0)$$ Get $$X(1)=a X(0) + e(1)$$ and so on. The $$e(i)$$ are successively chosen at random using your uniform random numbers. To give $$X(i)$$ the mean and standard deviation you want, you can derive it from the mean and variance of the noise sequence $$e(i)$$. Choose $$e(i)$$ appropriately.

• It's probably worth pointing out the existence of the arima.sim() function here. – fmark May 27 '12 at 14:50
• Sure those who now R should make those suggestions for implementation in R as the OP wants to know. – Michael R. Chernick May 27 '12 at 15:06