# [R] question about ar1 time series

Vincent Goulet vincent.goulet at act.ulaval.ca
Tue Jul 17 01:48:51 CEST 2007

```Le 07-07-16 à 09:50, Josué Polanco a écrit :

> Hello everybody,
>
> I recently wrote a "program" that to generate AR1 time series, here
> the code:
>
>
> #By Jomopo. Junio-2007, Leioa, Vizcaya
> #This program to create the AR1 syntetic series (one by one)
> #Where the mean is zero, but the variance of the serie AR1 and
> #the coef. of AR1 are be changed. If var serie AR1 = 1 then is
> standarized!
> #Final version for AR1 time series program
> #Mon Jul 16 11:58:03 CEST 2007 Checked again in R-prompt, and it's OK!
>
> #Creating the sintetic AR1 series... where the "white-noise"
> #has a mean = 0, and the var = sigmaz_c = stand_dev^2 is whatever
> value,
> #if sigmaz_c = 1 then this "white-noise" is a "Gaussian-noise."
> #rho1 (or alpha in another text-books ;-)) < 1 (in fact 0 < rho1 <
> 1) so that
> #the system can be stationary.
> #Where var_serie is the variance of the serie
>
> cat("\n Hello, this is creat_AR1_synt_ser.R. \n These are the input
> parameters: synt_series(ar1_length, rho1, ...), where rho1 is the
> correlat. coef.\n")
>
>
> ar1 <- function(x, rho1, af)
> {
>    return(x*rho1 + runif(1, -af, af))
> }
>
> #Spin-up for the AR1 series. For this case is enough with this amount
> spinup <- function(x0, rho1, af)
> {
>    xt <- x0
>    for (i in 1:100) {
>      xtp1 <- ar1(xt, rho1, af)
>      xt   <- xtp1
>    }
>    return(xt)
> }
>
> #Wherein "ar1_length" is the number of data in AR1 series
> #rho1 is a correlation coef.
> #sigmaz_c is optional
> synt_series <- function(ar1_length, rho1, var_serie)
> {
>    if( (var_serie <= 0) || rho1 <= -1 || rho1 >= 1 )
>      stop("The variance of the serie should be > 0, or the rho1
> parameter should be between (-1, 1) for that the serie can be
> stationary, be careful with this, bye. \n")
>
>    syntdata    <- rep(0, ar1_length)
> #af = adjustement factor, i.e. for that the var of random numbers =
> var of white noise (check the manual of runif)
>    af          <- sqrt( 3 * var_serie * (1 - rho1) * (1 + rho1) )
>    x0          <- runif(1, -af, af)
>    syntdata[1] <- spinup(x0, rho1, af)
>
>    for (i in 2:ar1_length) {
>      syntdata[i]  <- ar1(syntdata[i - 1], rho1, af)
>    }
>    return(syntdata)
> }
>
>
> I would like some suggestions and hints.

Here's one: look at arima.sim() and ease your life a lot. ;-)

>
> Thanks a lot for your help!
>
> --
> Josué Mosés Polanco Martínez
> Correo-e alternativo jomopo at linuxmail.org
> ----
> It is a wasted day unless you have learned something new and made
> someone smile -Mark Weingartz.
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help