############################

# Author: Joshua M. Tebbs  #

# Date: 20 Dec 2009        #

# Update: 25 Jul 2011      #

# Purpose: STAT 520 R code #

# CHAPTER 2                #

############################

# Example 2.1.

# Page 30

white.noise <- rnorm(150,0,1)

plot(white.noise,ylab="Simulated white noise process",xlab="Time",type="o")

# Example 2.2.

# Uses white noise process from Example 2.1.

# Page 33

random.walk <- white.noise*0

for(i in 1:length(white.noise)){

random.walk[i]<-sum(white.noise[1:i])

}

plot(random.walk,ylab="Simulated random walk process",xlab="Time",type="o")

# Example 2.3.

# Uses white noise process from Example 2.1.

# Page 36

moving.average <- filter(x = white.noise, filter = rep(x = 1/3, times = 3), method = "convolution", sides = 1)

plot(moving.average,ylab="Simulated moving average process",xlab="Time",type="o")

# Example 2.4.

# Autoregressive model simulation

# Page 37

autoregressive <- arima.sim(model = list(ar = c(0.75)), n = 150, rand.gen = rnorm, sd = 1)

plot(autoregressive,ylab="Simulated autoregressive process",xlab="Time",type="o")

# Example 2.5.

# Sinusoidal process

# Page 38

mean.function <- 2*sin(2*pi*1:156/52+0.6*pi)

w <- rnorm(156,0,1)

par(mfrow=c(2,2))

plot(mean.function,ylab="",xlab="Time",type="l")

plot(mean.function+w,ylab="",xlab="Time",type="o")

plot(mean.function+2*w,ylab="",xlab="Time",type="o")

plot(mean.function+4*w,ylab="",xlab="Time",type="o")