Due Date: November 1, 2021

Instructions

Please complete each problem below to the best of your ability. Where plain text is required, you can type directly into the .RMD file. Where code and output is required, be sure to include all code in the code chunks provided. The assignment must be submitted, via email, as both the .RMD file and the knitted file (whether .html or .pdf, whichever is best for yourself)

Problem 1

Complete Chapter 4, Exercise 1 from the textbook.

#Simulate discrete white noise from an exponential distribution and plot the
#histogram and the correlogram. For example, you can use the R command
#w <- rexp(1000)-1 for exponential white noise. Comment on the plots.

set.seed(1)
w <- rexp(1000)-1
hist(w)

#There is a reduction of frequency as "W" increases

Problem 2

Complete Chapter 4, Exercise 2 from the textbook.

Problem 2A

#Simulate time series of length 100 from an AR(1) model with  equal
#to −0.9, −0.5, 0.5, and 0.9. Estimate the parameter of each model and
#make predictions for 1 to 10 steps ahead.

#Case 1 

n <- 100
n_ahead <- 10

alpha1 <- -0.9
x <- arima.sim(model = list(ar=alpha1), n = n)
par(mfrow = c(2,1), mar = c(5,4,1,0))
plot(x)
acf(x)

fit <- ar(x)
fit$ar
## [1] -0.8290606
pred <- predict(fit, n.ahead = n_ahead)
lower <- pred$pred - 2*pred$se
upper <- pred$pred + 2*pred$se
ts.plot(x, pred$pred, lower, upper, lty = c(1,2,3,3), col = c("black", "green", "red", "red"))

##Case 2

alpha1 <- -0.5
x <- arima.sim(model = list(ar=alpha1), n = n)
par(mfrow = c(2,1), mar = c(5,4,1,0))
plot(x)
acf(x)

fit <- ar(x)
fit$ar
## [1] -0.6127766
pred <- predict(fit, n.ahead = n_ahead)
lower <- pred$pred - 2*pred$se
upper <- pred$pred + 2*pred$se
ts.plot(x, pred$pred, lower, upper, lty = c(1,2,3,3), col = c("black", "green", "red", "red"))

##Case 3

alpha1 <- 0.5
x <- arima.sim(model = list(ar=alpha1), n = n)
par(mfrow = c(2,1), mar = c(5,4,1,0))
plot(x)
acf(x)

fit <- ar(x)
fit$ar
## [1] 0.5009435
pred <- predict(fit, n.ahead = n_ahead)
lower <- pred$pred - 2*pred$se
upper <- pred$pred + 2*pred$se
ts.plot(x, pred$pred, lower, upper, lty = c(1,2,3,3), col = c("black", "green", "red", "red"))

##Case 4

alpha1 <- 0.9
x <- arima.sim(model = list(ar=alpha1), n = n)
par(mfrow = c(2,1), mar = c(5,4,1,0))
plot(x)
acf(x)

fit <- ar(x)
fit$ar
## [1] 0.8853054
pred <- predict(fit, n.ahead = n_ahead)
lower <- pred$pred - 2*pred$se
upper <- pred$pred + 2*pred$se
ts.plot(x, pred$pred, lower, upper, lty = c(1,2,3,3), col = c("black", "green", "red", "red"))

Problem 2B

#Simulate time series of length 100 from an AR(1) model with  equal
#to 1.01, 1.02, and 1.05. Estimate the parameters of these models.

# Simulate an AR model with 0.5 slope

set.seed(1)
x <- rnorm(100)
set.seed(1)
w <- rnorm(100)
for (t in 2:100){
  x[t] <- 0.5 * x[t - 1] + w[t]
}
plot(x, type = "l")

acf(x)

pacf(x)

# Simulate an AR model with 0.9 slope
y <- rnorm(100)
set.seed(1)
w <- rnorm(100)
for (t in 2:100){
  x[t] <- 0.9 * x[t - 1] + w[t]
}
plot(y, type = "l")

acf(y)

pacf(y)

# Simulate an AR model with -0.75 slope
z <- rnorm(100)
set.seed(1)
w <- rnorm(100)
for (t in 2:100){
  x[t] <- -0.75 * x[t - 1] + w[t]
}
plot(z, type = "l")

acf(z)

pacf(z)

Problem 3

Complete Chapter 4, Exercise 4 from the textbook. Show all parts below

#a) Simulate a time series of length 1000 for the following model, giving
#appropriate R code and placing the simulated data in a vector x:

y <- arima.sim(n = 1000, list(order = c(1,1,0), ar = 1/2, sd = 1))

x <- y[2:1000]-y[1:999]
#Plot the correlogram and partial correlogram for the simulated data.
#Comment on the plots.

acf(x)

#The ACF plot of the process shows correlations are fast decaying but never reach zero. This indicates AR like pattern
#Fit an AR model to the data in x giving the parameter estimates and
#order of the fitted AR process.

x.ar <- ar(x, method = "mle")
#Construct 95% confidence intervals for the parameter estimates of
#the fitted model. Do the model parameters fall within the confidence
#intervals? Explain your results.

paste0("95% CI: ", x.ar$ar + c(-2, 2) * sqrt(x.ar$asy.var))
## Warning in c(-2, 2) * sqrt(x.ar$asy.var): Recycling array of length 1 in vector-array arithmetic is deprecated.
##   Use c() or as.vector() instead.
## [1] "95% CI: 0.385776910595098" "95% CI: 0.499246961785142"
#Is the model in Equation (4.25) stationary or non-stationary? Justify
#your answer.

#Root B = 2 is outside the unit circle, so the model is stationary.
#Plot the correlogram of the residuals of the fitted model, and comment on the plot.

acf(x.ar$res[-1])

#the model fits the data very well.