R/GenSeries.R

Defines functions GenSeries

Documented in GenSeries

#'Generates An AR1 Time Series
#'
#'This function generates AR1 processes containing n data points, where alpha 
#'is the autocorrelation at lag 1, and the mean and standard deviation are 
#'specified by the mean and std arguments.
#'
#'@param n Length of the timeseries to be generated.
#'@param alpha Autocorrelation at lag 1.
#'@param mean Mean of the data.
#'@param std Standard deviation of the data.
#'
#'@return AR1 timeseries.
#'
#'@keywords datagen
#'@author History:\cr
#'0.1  -  2012-04  (L. Auger)  -  Original code\cr
#'1.0  -  2012-04  (N. Manubens)  -  Formatting to CRAN
#'@examples
#'series <- GenSeries(1000, 0.35, 2, 1)
#'plot(series, type = 'l')
#'
#'@importFrom stats rnorm
#'@export
GenSeries <- function(n, alpha, mean, std) {
  res <- vector("numeric", n)
  x <- mean
  stdterm <- std * (sqrt(1 - alpha ^ 2) / (1 - alpha))
  for (i in 1:100) {
    x <- alpha * x + (1 - alpha) * rnorm(1, mean, stdterm)
  }
  for (i in 1:n) {
    x <- alpha * x + (1 - alpha) * rnorm(1, mean, stdterm)
    res[i] <- x
  }
  
  res
}

Try the s2dverification package in your browser

Any scripts or data that you put into this service are public.

s2dverification documentation built on April 20, 2022, 9:06 a.m.