Hello All,
Below are the steps I took to generate a garch model. I made some comments in the code chunck.
Best, Jordan
require(fpp)
require(forecast)
require(quantmod)
require(rugarch)
# API Call
H <- new.env()
getSymbols("HOG", env = H, src = "yahoo",
from = as.Date("2013-04-11"), to = as.Date("2018-04-11"))
## [1] "HOG"
HOG <- H$HOG
head(HOG)
## HOG.Open HOG.High HOG.Low HOG.Close HOG.Volume HOG.Adjusted
## 2013-04-11 52.13 52.88 51.99 52.21 1764900 46.54485
## 2013-04-12 52.09 52.15 51.56 52.00 984600 46.35764
## 2013-04-15 51.55 52.04 49.92 49.95 2281300 44.53008
## 2013-04-16 50.43 51.69 50.43 51.63 1454300 46.02780
## 2013-04-17 51.28 51.47 50.59 51.15 1692300 45.59987
## 2013-04-18 51.08 51.52 50.62 51.05 1547500 45.51072
plot(HOG$HOG.Adjusted)
# Decomposition
HOG.Adjusted <- ts(HOG$HOG.Adjusted, frequency = 252, start = c(2013,4,11))
decomp <- decompose(HOG.Adjusted, "multiplicative")
plot(decomp)
# Compond
HOG.Adjusted.clean <- diff(log(HOG.Adjusted)) * 100
plot(HOG.Adjusted.clean, main = "Daily Adjusted Componded Prices")
fit1 <- auto.arima(HOG.Adjusted.clean, trace = TRUE, test = "kpss", ic ="aic")
##
## Fitting models using approximations to speed things up...
##
## ARIMA(2,0,2)(1,0,1)[252] with non-zero mean : Inf
## ARIMA(0,0,0) with non-zero mean : 4953.932
## ARIMA(1,0,0)(1,0,0)[252] with non-zero mean : Inf
## ARIMA(0,0,1)(0,0,1)[252] with non-zero mean : Inf
## ARIMA(0,0,0) with zero mean : 4951.955
## ARIMA(0,0,0)(1,0,0)[252] with non-zero mean : Inf
## ARIMA(0,0,0)(0,0,1)[252] with non-zero mean : Inf
## ARIMA(0,0,0)(1,0,1)[252] with non-zero mean : Inf
## ARIMA(1,0,0) with non-zero mean : 4955.968
## ARIMA(0,0,1) with non-zero mean : 4954.908
## ARIMA(1,0,1) with non-zero mean : 4952.012
##
## Now re-fitting the best model(s) without approximations...
##
## ARIMA(0,0,0) with zero mean : 4951.955
##
## Best model: ARIMA(0,0,0) with zero mean
fit1
## Series: HOG.Adjusted.clean
## ARIMA(0,0,0) with zero mean
##
## sigma^2 estimated as 2.995: log likelihood=-2474.98
## AIC=4951.96 AICc=4951.96 BIC=4957.09
# Best Model = ARIMA(0,0,0)
# Test If We Can Reject The NUll
Box.test(fit1$residuals^2, lag = 12, type = "Ljung-Box")
##
## Box-Ljung test
##
## data: fit1$residuals^2
## X-squared = 64.58, df = 12, p-value = 3.26e-09
# armaOrder 0,0
HOG.garch.spec <- ugarchspec(variance.model=list(model="sGARCH", garchOrder=c(1,1)), mean.model=list(armaOrder=c(0,0)), distribution.model="std")
HOG.garch.fit <- ugarchfit(spec = HOG.garch.spec, data = HOG.Adjusted.clean)
HOG.garch.fit
##
## *---------------------------------*
## * GARCH Model Fit *
## *---------------------------------*
##
## Conditional Variance Dynamics
## -----------------------------------
## GARCH Model : sGARCH(1,1)
## Mean Model : ARFIMA(0,0,0)
## Distribution : std
##
## Optimal Parameters
## ------------------------------------
## Estimate Std. Error t value Pr(>|t|)
## mu 0.036033 0.038289 0.94109 0.346660
## omega 0.603154 0.289178 2.08576 0.037001
## alpha1 0.067731 0.030415 2.22687 0.025956
## beta1 0.706188 0.122730 5.75399 0.000000
## shape 4.180337 0.476957 8.76459 0.000000
##
## Robust Standard Errors:
## Estimate Std. Error t value Pr(>|t|)
## mu 0.036033 0.034687 1.0388 0.298888
## omega 0.603154 0.340301 1.7724 0.076326
## alpha1 0.067731 0.033582 2.0169 0.043706
## beta1 0.706188 0.133143 5.3040 0.000000
## shape 4.180337 0.555098 7.5308 0.000000
##
## LogLikelihood : -2299.923
##
## Information Criteria
## ------------------------------------
##
## Akaike 3.6644
## Bayes 3.6848
## Shibata 3.6644
## Hannan-Quinn 3.6721
##
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.05563 0.8135
## Lag[2*(p+q)+(p+q)-1][2] 1.10343 0.4659
## Lag[4*(p+q)+(p+q)-1][5] 4.55282 0.1927
## d.o.f=0
## H0 : No serial correlation
##
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.09418 0.7589
## Lag[2*(p+q)+(p+q)-1][5] 1.31256 0.7858
## Lag[4*(p+q)+(p+q)-1][9] 3.15659 0.7329
## d.o.f=2
##
## Weighted ARCH LM Tests
## ------------------------------------
## Statistic Shape Scale P-Value
## ARCH Lag[3] 0.3519 0.500 2.000 0.5530
## ARCH Lag[5] 3.0579 1.440 1.667 0.2815
## ARCH Lag[7] 3.7249 2.315 1.543 0.3880
##
## Nyblom stability test
## ------------------------------------
## Joint Statistic: 0.8945
## Individual Statistics:
## mu 0.06199
## omega 0.49317
## alpha1 0.24781
## beta1 0.44172
## shape 0.12016
##
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic: 1.28 1.47 1.88
## Individual Statistic: 0.35 0.47 0.75
##
## Sign Bias Test
## ------------------------------------
## t-value prob sig
## Sign Bias 0.3153 0.7526
## Negative Sign Bias 0.3037 0.7614
## Positive Sign Bias 1.0950 0.2737
## Joint Effect 3.1598 0.3676
##
##
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
## group statistic p-value(g-1)
## 1 20 17.77 0.5378
## 2 30 22.99 0.7772
## 3 40 32.75 0.7496
## 4 50 57.82 0.1817
##
##
## Elapsed time : 1.088104
plot(HOG.garch.fit, which = 2)
##
## please wait...calculating quantiles...
HOG.garch.forecast <- ugarchforecast(HOG.garch.fit, n.ahead = 15)
plot(HOG.garch.forecast, which = 1)