library(fpp2)
## Warning: package 'fpp2' was built under R version 4.0.2
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
## ── Attaching packages ─────────────────────────────────────────────────────────────────── fpp2 2.4 ──
## ✓ ggplot2 3.3.0 ✓ fma 2.4
## ✓ forecast 8.13 ✓ expsmooth 2.3
## Warning: package 'forecast' was built under R version 4.0.2
## Warning: package 'fma' was built under R version 4.0.2
## Warning: package 'expsmooth' was built under R version 4.0.2
##
library(forecast)
library(fGarch)
## Warning: package 'fGarch' was built under R version 4.0.2
## Loading required package: timeDate
## Loading required package: timeSeries
## Loading required package: fBasics
library(ggplot2)
library(kableExtra)
## Warning: package 'kableExtra' was built under R version 4.0.2
library(rugarch)
## Warning: package 'rugarch' was built under R version 4.0.2
## Loading required package: parallel
##
## Attaching package: 'rugarch'
## The following object is masked from 'package:stats':
##
## sigma
library(tseries)
## Warning: package 'tseries' was built under R version 4.0.2
xus <- read.csv("//Users/DRG/Desktop/MSAE/XTEXVA01USM664N.csv")
xus <- xus[order(xus$DATE),]
xusts1 <- ts(xus[,2]/1000000000,start=c(1990,1),end=c(2020,9),frequency=12)
autoplot(xusts1) + ggtitle("US Exports, billions of US$") + ylab("")

ETS
ets1 <- ets(xusts1, model = "ZZZ")
summary(ets1)
## ETS(M,Ad,M)
##
## Call:
## ets(y = xusts1, model = "ZZZ")
##
## Smoothing parameters:
## alpha = 0.896
## beta = 4e-04
## gamma = 1e-04
## phi = 0.98
##
## Initial states:
## l = 32.0359
## b = 0.1468
## s = 1.0029 1.0096 1.0535 0.9868 0.9904 0.9462
## 1.0203 1.0133 0.9994 1.0844 0.9551 0.9381
##
## sigma: 0.0356
##
## AIC AICc BIC
## 2932.145 2934.100 3002.540
##
## Training set error measures:
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 0.2409143 3.277903 2.090429 0.276122 2.529727 0.282053 0.1746311
autoplot(ets1)

checkresiduals(ets1)

##
## Ljung-Box test
##
## data: Residuals from ETS(M,Ad,M)
## Q* = 111.08, df = 7, p-value < 2.2e-16
##
## Model df: 17. Total lags used: 24
ets1 %>% forecast(h=12) %>%
autoplot() + autolayer(ts(ets1$fitted,frequency=12, start=c(1990,1),end=c(2020,9)), series="Fitted Values")

Arima
aa1 <- auto.arima(xusts1)
summary(aa1)
## Series: xusts1
## ARIMA(1,1,1)(1,0,0)[12]
##
## Coefficients:
## ar1 ma1 sar1
## -0.904 0.9688 0.8416
## s.e. 0.035 0.0186 0.0310
##
## sigma^2 estimated as 13.52: log likelihood=-1006.84
## AIC=2021.68 AICc=2021.79 BIC=2037.32
##
## Training set error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set 0.007190722 3.657239 2.43898 -0.01705353 2.975696 0.3290815
## ACF1
## Training set -0.02432848
checkresiduals(aa1)

##
## Ljung-Box test
##
## data: Residuals from ARIMA(1,1,1)(1,0,0)[12]
## Q* = 48.451, df = 21, p-value = 0.0005968
##
## Model df: 3. Total lags used: 24
aa1 %>% forecast(h=12) %>%
autoplot() + autolayer(ts(ets1$fitted, start=c(1990,1),end=c(2020,9),frequency=12), series="Fitted Values")

GARCH
garchxus <- garchFit(data = xusts1)
##
## Series Initialization:
## ARMA Model: arma
## Formula Mean: ~ arma(0, 0)
## GARCH Model: garch
## Formula Variance: ~ garch(1, 1)
## ARMA Order: 0 0
## Max ARMA Order: 0
## GARCH Order: 1 1
## Max GARCH Order: 1
## Maximum Order: 1
## Conditional Dist: norm
## h.start: 2
## llh.start: 1
## Length of Series: 369
## Recursion Init: mci
## Series Scale: 36.0844
##
## Parameter Initialization:
## Initial Parameters: $params
## Limits of Transformations: $U, $V
## Which Parameters are Fixed? $includes
## Parameter Matrix:
## U V params includes
## mu -23.32148590 23.32149 2.332149 TRUE
## omega 0.00000100 100.00000 0.100000 TRUE
## alpha1 0.00000001 1.00000 0.100000 TRUE
## gamma1 -0.99999999 1.00000 0.100000 FALSE
## beta1 0.00000001 1.00000 0.800000 TRUE
## delta 0.00000000 2.00000 2.000000 FALSE
## skew 0.10000000 10.00000 1.000000 FALSE
## shape 1.00000000 10.00000 4.000000 FALSE
## Index List of Parameters to be Optimized:
## mu omega alpha1 beta1
## 1 2 3 5
## Persistence: 0.9
##
##
## --- START OF TRACE ---
## Selected Algorithm: nlminb
##
## R coded nlminb Solver:
##
## 0: 488.03409: 2.33215 0.100000 0.100000 0.800000
## 1: 475.69335: 2.27301 0.0315531 0.108821 0.766704
## 2: 444.06972: 2.19091 0.0121478 0.178154 0.776259
## 3: 365.41758: 1.50621 1.00000e-06 0.252809 0.791249
## 4: 364.98628: 1.50667 0.000558700 0.252787 0.791212
## 5: 364.61606: 1.50781 0.000238343 0.252738 0.791114
## 6: 363.87480: 1.51042 0.000623268 0.252725 0.790991
## 7: 345.88840: 1.58828 8.31947e-05 0.254756 0.789464
## 8: 345.39775: 1.58834 0.000475322 0.254748 0.789449
## 9: 340.07737: 1.61402 0.000901379 0.281277 0.733706
## 10: 339.68400: 1.60079 0.00149265 0.289896 0.713850
## 11: 339.45811: 1.60086 0.00116031 0.289890 0.713837
## 12: 339.45098: 1.60101 0.00109694 0.290169 0.713678
## 13: 339.44113: 1.60111 0.00114973 0.290736 0.713330
## 14: 339.42831: 1.60117 0.00110327 0.291800 0.712524
## 15: 339.37709: 1.59946 0.00118786 0.301008 0.704645
## 16: 339.32094: 1.60271 0.00111968 0.307915 0.703024
## 17: 339.25062: 1.60632 0.000967118 0.302802 0.689586
## 18: 339.01374: 1.61251 0.00188992 0.307932 0.676361
## 19: 338.61812: 1.60893 0.00193351 0.341085 0.650595
## 20: 338.46511: 1.61309 0.00196544 0.367539 0.619681
## 21: 338.33344: 1.60684 0.00198155 0.397073 0.595790
## 22: 338.32365: 1.60767 0.00204405 0.403379 0.589982
## 23: 338.31915: 1.60800 0.00212966 0.412257 0.581624
## 24: 338.31884: 1.60793 0.00215376 0.414335 0.579453
## 25: 338.31880: 1.60789 0.00216073 0.414722 0.578917
## 26: 338.31880: 1.60788 0.00216115 0.414661 0.578918
## 27: 338.31880: 1.60788 0.00216093 0.414623 0.578943
## 28: 338.31880: 1.60788 0.00216088 0.414620 0.578946
##
## Final Estimate of the Negative LLH:
## LLH: 1661.501 norm LLH: 4.502714
## mu omega alpha1 beta1
## 58.0194949 2.8136517 0.4146198 0.5789462
##
## R-optimhess Difference Approximated Hessian Matrix:
## mu omega alpha1 beta1
## mu -4.1055356 0.3096018 0.573116 4.19351
## omega 0.3096018 -1.7939226 -9.176471 -20.80876
## alpha1 0.5731160 -9.1764706 -907.082637 -971.43988
## beta1 4.1935098 -20.8087631 -971.439878 -1203.30118
## attr(,"time")
## Time difference of 0.02953815 secs
##
## --- END OF TRACE ---
##
##
## Time to Estimate Parameters:
## Time difference of 0.1526811 secs
## Warning: Using formula(x) is deprecated when x is a character vector of length > 1.
## Consider formula(paste(x, collapse = " ")) instead.
summary(garchxus)
##
## Title:
## GARCH Modelling
##
## Call:
## garchFit(data = xusts1)
##
## Mean and Variance Equation:
## data ~ garch(1, 1)
## <environment: 0x7fe32eeec290>
## [data = xusts1]
##
## Conditional Distribution:
## norm
##
## Coefficient(s):
## mu omega alpha1 beta1
## 58.01949 2.81365 0.41462 0.57895
##
## Std. Errors:
## based on Hessian
##
## Error Analysis:
## Estimate Std. Error t value Pr(>|t|)
## mu 58.0195 0.4986 116.364 < 2e-16 ***
## omega 2.8137 1.0206 2.757 0.005836 **
## alpha1 0.4146 0.1106 3.748 0.000178 ***
## beta1 0.5789 0.1046 5.535 3.11e-08 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Log Likelihood:
## -1661.501 normalized: -4.502714
##
## Description:
## Wed Nov 25 16:06:34 2020 by user:
##
##
## Standardised Residuals Tests:
## Statistic p-Value
## Jarque-Bera Test R Chi^2 31.45587 1.477219e-07
## Shapiro-Wilk Test R W 0.8478552 0
## Ljung-Box Test R Q(10) 2176.68 0
## Ljung-Box Test R Q(15) 3101.484 0
## Ljung-Box Test R Q(20) 3888.625 0
## Ljung-Box Test R^2 Q(10) 53.50955 5.987219e-08
## Ljung-Box Test R^2 Q(15) 112.7859 0
## Ljung-Box Test R^2 Q(20) 129.9285 0
## LM Arch Test R TR^2 103.1522 1.110223e-16
##
## Information Criterion Statistics:
## AIC BIC SIC HQIC
## 9.027108 9.069501 9.026876 9.043949
garch_fc = predict(garchxus, n.ahead=12, plot=TRUE)
