Due Date: November 15, 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 5, Exercise 1 from the textbook. ### Problem 1A

#Produce a time plot for {xt : t = 1, . . . , 100}, where xt = 70 + 2t −
#3t2+zt, {zt} is the AR(1) process zt = 0.5zt−1+wt, and {wt} is white
#noise with standard deviation 25.

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

Problem 1B

#Fit a quadratic trend to the series {xt}. Give the coefficients of the
#fitted model.

SIN <- matrix(nr = length(x), nc = 6)
COS <- matrix(nr = length(x), nc = 6)
for (i in 1:6) {
  COS[, i] <- cos(2 * pi * i * time(x))
  SIN[, i] <- sin(2 * pi * i * time(x))
}
TIME <- (time(x) - mean(time(x)))/sd(time(x))
sd(time(x))
## [1] 29.01149

Problem 1C

#Find a 95% confidence interval for the parameters of the quadratic
#model, and comment.
library(nlme)

x.gls <- gls(x ~ time(x), cor = corAR1(0.7))
confint(x.gls)
##                 2.5 %   97.5 %
## (Intercept) 52.348508 78.85943
## time(x)      1.777186  2.23092

Problem 1D

#Plot the correlogram of the residuals and comment.

res.ar <- ar(resid(x.gls), method = "mle")
res.ar$ar
## [1] 0.4439458
sd(res.ar$res[-(1:2)])
## [1] 18.05101
acf(res.ar$res[-(1:2)])

#There is white noise present 

Problem 1E

#Refit the model using GLS. Give the standard errors of the parameter
#estimates, and comment.

x.gls <- gls(x ~ time(x), cor = corAR1(0.7))

Problem 2

Complete Chapter 5, Exercise 6 from the textbook.

#Plot a histogram of the residual errors of the fitted AR model, and
#comment on the plot. Fit a back-to-back Weibull distribution to the
#residual errors.

font = read.csv(file ="C:/CS-637-Time-Series-and-Forecasting--main/Lecture Notes/data/Fontdsdt.csv")
font.ts <- ts(font)

SIN <- matrix(nr = length(font.ts), nc = 6)
COS <- matrix(nr = length(font.ts), nc = 6)
for (i in 1:6) {
  COS[, i] <- cos(2 * pi * i * time(font.ts))
  SIN[, i] <- sin(2 * pi * i * time(font.ts))
}
TIME <- (time(font.ts) - mean(time(font.ts)))/sd(time(font.ts))
sd(time(font.ts))
## [1] 249.5596
font.gls <- gls(font.ts ~ time(font.ts), cor = corAR1(0.7))
confint(font.gls)
##                       2.5 %       97.5 %
## (Intercept)   -0.0574350814 0.0562823192
## time(font.ts) -0.0001126944 0.0001150412
res.ar <- ar(resid(font.gls))
res.ar$ar
## [1] 0.1947692 0.0525467
sd(res.ar$res[-(1:2)])
## [1] 0.3368596
hist(res.ar$res[-(1:2)])