#6. Simulate an AR(2) time series of length n = 72 with φ1 = 0.7 and φ2 = −0.4.
#(a) Calculate and plot the theoretical autocorrelation function for this model. Plot
# sufficient lags until the correlations are negligible.
#(b) Calculate and plot the sample ACF for your simulated series. How well do the
# values and patterns match the theoretical ACF from part (a)?
#(c) What are the theoretical partial autocorrelations for this model?
#(d) Calculate and plot the sample ACF for your simulated series. How well do the
# values and patterns match the theoretical ACF from part (a)?
#(e) Calculate and plot the sample PACF for your simulated series. How well do the
# values and patterns match the theoretical ACF from part (c)?
library(TSA)
##
## Attaching package: 'TSA'
## The following objects are masked from 'package:stats':
##
## acf, arima
## The following object is masked from 'package:utils':
##
## tar
series=arima.sim(n=72,list(ar=c(0.7,-0.4)))
phi1=0.7
phi2=-0.4
ACF=ARMAacf(ar=c(phi1,phi2),lag.max=10)
ACF
## 0 1 2 3 4 5
## 1.00000000 0.50000000 -0.05000000 -0.23500000 -0.14450000 -0.00715000
## 6 7 8 9 10
## 0.05279500 0.03981650 0.00675355 -0.01119912 -0.01054080
#graphics.off()
plot(y=ACF[-1],x=1:10,xlab='Lag',ylab='ACF',type='h',ylim=c(-0.6,0.6), lwd=2, col="blue"); abline(h=0)

acf(series, lwd=2, main="")

PACFtheo=ARMAacf(ar=c(0.7,-0.4),pacf=T)
PACFtheo
## [1] 0.5 -0.4
pacf(series, lwd=2)

pacf(series)

#7. Simulate an MA(2) time series of length n = 36 with θ1 = 0.7 and θ2 = −0.4.
#(a) What are the theoretical autocorrelations for this model?
#(b) Calculate and plot the sample ACF for your simulated series. How well do the
#values and patterns match the theoretical ACF from part (a)?
#(c) Calculate and plot the theoretical partial autocorrelation function for this
#model. Plot sufficient lags until the correlations are negligible. (Hint: See Equation
#(6.2.6) on page 114.)
#(d) Calculate and plot the sample PACF for your simulated series. How well do
#the values and patterns match the theoretical ACF from part (c)?
series=arima.sim(n=36,list(ma=c(-0.7,0.4)))
theta1=0.7
theta2=-0.4
ACF=ARMAacf(ma=c(-theta1,-theta2),lag.max=10)
plot(y=ACF[-1],x=1:10,xlab='Lag',ylab='ACF',type='h',ylim=c(-.6,.6), lwd=2, col="blue"); abline(h=0)

acf(series, lwd=2, main="")

series=arima.sim(n=1000,list(ma=c(-0.7,0.4)))
pacf(series,ci.col=NULL, lwd=2)

pacf(series,lwd=2)

#8. Simulate a nonstationary time series according to the model ARIMA(0,1,1)
#with n = 60 and θ = 0.8.
#(a) Perform the (augmented) Dickey-Fuller test on the series with k = 0 in
#Equation (6.4.1) on page 128. (With k = 0, this is the Dickey-Fuller test
#and is not augmented.) Comment on the results.
#(b) Perform the augmented Dickey-Fuller test on the series with k chosen by the
#software that is, the “best” value for k. Comment on the results.
#(c) Repeat parts (a) and (b) but use the differences of the simulated series.
#Comment on the results. (Here, of course, you should reject the unit root
#hypothesis.)
#install.packages('CADFtest')
library(CADFtest)
## Loading required package: dynlm
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Loading required package: sandwich
## Loading required package: tseries
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
## Loading required package: urca
## Registered S3 methods overwritten by 'CADFtest':
## method from
## bread.mlm sandwich
## estfun.mlm sandwich
set.seed(24 )
series=arima.sim(n=60,list(order=c(0, 1,1),ma=-0.8))[-1]
CADFtest(series, type = "drift", max.lag.y = 0)
##
## ADF test
##
## data: series
## ADF(0) = -4.6256, p-value = 0.0003745
## alternative hypothesis: true delta is less than 0
## sample estimates:
## delta
## -0.5436687
ar( (series))
##
## Call:
## ar(x = (series))
##
## Coefficients:
## 1 2 3
## 0.3458 0.0757 0.2063
##
## Order selected 3 sigma^2 estimated as 1.038
CADFtest(series, type = "drift", max.lag.y = 3)
##
## ADF test
##
## data: series
## ADF(3) = -2.3975, p-value = 0.147
## alternative hypothesis: true delta is less than 0
## sample estimates:
## delta
## -0.3898622
CADFtest(diff(series), type = "drift", max.lag.y = 0)
##
## ADF test
##
## data: diff(series)
## ADF(0) = -11.28, p-value = 3.407e-12
## alternative hypothesis: true delta is less than 0
## sample estimates:
## delta
## -1.395088
ar(diff(series))
##
## Call:
## ar(x = diff(series))
##
## Coefficients:
## 1 2
## -0.5216 -0.3369
##
## Order selected 2 sigma^2 estimated as 1.101
CADFtest(diff(series), type = "drift", max.lag.y = 2)
##
## ADF test
##
## data: diff(series)
## ADF(2) = -6.2908, p-value = 1.361e-06
## alternative hypothesis: true delta is less than 0
## sample estimates:
## delta
## -2.105427
#9The data file named robot contains a time series obtained from an industrial
#robot.
#The robot was put through a sequence of maneuvers, and the distance from a
#desired ending point was recorded in inches. This was repeated 324 times to
#form the time series.
#(a) Display the time series plot of the data. Based on this information, do these
#data appear to come from a stationary or nonstationary process?
#(b) Calculate and plot the sample ACF and PACF for these data. Based on this
#additional information, do these data appear to come from a stationary or
#nonstationary process?
#(c) Calculate and interpret the sample EACF.
#(d) Use the best subsets ARMA approach to specify a model for these data.
#Compare these results with what you discovered in parts (a), (b), and (c).
data(robot)
plot.ts(robot, type="o", col="blue", lwd=2)

par(mfrow=c(1,2))
acf(robot, lwd=2, main="")
pacf(robot, lwd=2 )

eacf(robot)
## AR/MA
## 0 1 2 3 4 5 6 7 8 9 10 11 12 13
## 0 x x x x x x x x x o x x x x
## 1 x o o o o o o o o o o o o o
## 2 x x o o o o o o o o o o o o
## 3 x x o o o o o o o o o o o o
## 4 x x x x o o o o o o o o x o
## 5 x x x o o o o o o o o o x o
## 6 x o o o o x o o o o o o o o
## 7 x o o x o x x o o o o o o o
ARMArobots=armasubsets(y = robot, nar = 14, nma = 14,y.name = "test", ar.method = "ols")
## Warning in leaps.setup(x, y, wt = wt, nbest = nbest, nvmax = nvmax, force.in =
## force.in, : 2 linear dependencies found
## Reordering variables and trying again:
plot(ARMArobots)
