Learn R Programming

CoSMoS (version 2.1.0)

AR1: Autoregressive model of first order

Description

Generates time series from an AR1 model.

Usage

AR1(n, alpha, mean = 0, sd = 1)

Arguments

n

number of values

alpha

lag-1 autocorrelation

mean

mean

sd

standard deviation

Examples

Run this code
# NOT RUN {
library(CoSMoS)

## generate 500 values from an AR1 having lag-1 autocorrelation 0.8,
## mean value equal to 0, and standard deviation equal to 1.
n <- 500

## generate white noise for comparsion
x <- rnorm(n)
ggplot() +
 geom_line(aes(x = 1:n,
               y = x)) +
   labs(x = '',
        y = 'value') +
   theme_classic()

## generete values using AR1
y <- AR1(n, .8)
ggplot() +
  geom_line(aes(x = 1:n,
                y = y)) +
  labs(x = '',
       y = 'value') +
  theme_classic()

# }

Run the code above in your browser using DataLab