AutoRegressive Integrated Moving Average (ARIMA) Models are used to find the unpredictable deviation from the mean portion of the series. And if the data has high frequency then AutoRegressive Conditional Heteroskedasticity (ARCH) or Generalized AutoRegressive Conditional Heteroskedasticity (GARCH) models are used to estimate the volatility of the series.
ARCH GARCH model – Econistics.com
rm(list=ls())
library(FinTS) # For Arch Test
library(rugarch) # For Garch Models
library(tseries) # For unit root test
library(dynlm) # For using lags in the model
library(vars) # For using VAR
library(nlWaldTest) # For testing non-linear Wald Test
library(lmtest) # For BP Test
library(broom) # For table presentations
library(car) # For robust standard errors
library(sandwich)
library(knitr)
library(forecast)
library(ggplot2)
library(pdfetch) #for importing financial data
library(tsbox)
library(stats)
library(zoo)
library(vrtest)
Setting Theme
theme_set(theme_bw())
Collecting data
df <- pdfetch_YAHOO(fields = “close”, c(“^gspc”))
Convert to time-series objects
dfts <- ts(df, start = c(2007,3), end= c(2020,161), frequency = 365)
data.frame(dfts)
Visuals
plot.ts(dfts)
title(“Time Series Plot of GSPC”)
hist(dfts, main = “Histogram of GSPC”, freq = FALSE, col = “grey”)
plot.ts(diff(dfts), ylab= “Change in GSPC”)
title (“First difference of GSPC”)
Step 1: Normality Test and Log Transformation
shapiro.test(dfts)
df1 <- log(df) df <- df %>% mutate(logseries = log(“^gspc”))
Step 2: Check for Stationarity of variance and mean
plot.ts(df1)
title(“Time Series Plot of Log of GSPC”)
Auto.VR(df1) # Variance is not constant
adf.test(df1$’^gspc’, k = 3)
df2 <- diff(df1)
df2 <- na.remove(df2)
adf.test(df2, k = 2) # series is I(1)
Step 3: Determining AR and MA components
acf.df2 <- acf(df2, main = “ACF of GSPC”, lag.max = 50)
pacf.df2 <- pacf(df2, main = “PACF of GSPC”, lag.max = 50)
ARIMA (p,I,q) = (1, 1, 2)
Step 4: Estimating ARIMA model
auto.arima(df2)
ARIMA = (0,1,1)
arima011 <- arima(df1, order = c(0,1,1))
summary(arima011)
tsdiag(arima011)
checkresiduals(arima011)
autoplot(arima011)
arima212 <- arima(df1, order = c(2,1,2))
autoplot(arima212)
arimar <- arima011$residuals
ggtsdisplay(arimar, main = “GSP ARIMA Residuals”)
Step 5: Estimate mean equation r = beta + error
gsp.mean <- dynlm(arimar ~ 1)
summary(gsp.mean)
Step 6: Determination of ARCH Effect
ehatsq <- ts(resid(gsp.mean)^2)
gsp.archef <- dynlm(ehatsq ~ L(ehatsq))
summary(gsp.archef)
t <- nobs(gsp.mean)
q <- length(coef(gsp.archef))-1
rsq <- glance(gsp.archef)[[1]]
lm <- (t-q)*rsq
alpha <- 0.05
chicr <- qchisq(1-alpha, q)
lm
chicr
gsp.archef1 <- ArchTest(arimar, lags=1, demean = TRUE)
gsp.archef1
step 7 : Estimating arch garch equation
gsp.arch <- garch(arimar,c(1,3), control = garch.control(maxiter = 500, grad = “numerical”)) #first item in c() is garch effect and second is arch effect
sgsparch <- summary(gsp.arch)
sgsparch
hhat <- ts(2*gsp.arch$fitted.values[-1,1]^2)
plot.ts(hhat)
title(“Volatility of GSP”)
vol <- gsp.arch$fitted.values
vol