library(Quandl)
## Loading required package: xts
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
library(tseries)
library(forecast)
## Loading required package: timeDate
## This is forecast 6.2
library(urca)
## Warning: package 'urca' was built under R version 3.2.4
library(fGarch)
## Warning: package 'fGarch' was built under R version 3.2.4
## Loading required package: timeSeries
##
## Attaching package: 'timeSeries'
## The following object is masked from 'package:zoo':
##
## time<-
## Loading required package: fBasics
## Warning: package 'fBasics' was built under R version 3.2.4
##
## Rmetrics Package fBasics
## Analysing Markets and calculating Basic Statistics
## Copyright (C) 2005-2014 Rmetrics Association Zurich
## Educational Software for Financial Engineering and Computational Science
## Rmetrics is free software and comes with ABSOLUTELY NO WARRANTY.
## https://www.rmetrics.org --- Mail to: info@rmetrics.org
library(stargazer)
##
## Please cite as:
## Hlavac, Marek (2015). stargazer: Well-Formatted Regression and Summary Statistics Tables.
## R package version 5.2. http://CRAN.R-project.org/package=stargazer
library(rugarch)
## Warning: package 'rugarch' was built under R version 3.2.4
## Loading required package: parallel
msf <- Quandl("GOOG/NASDAQ_MSFT", api_key="5NyVfHfxdScCYsskDP59", type="zoo")
ms<-msf$Close
lmsf<- log(ms)
dmsf<- diff(lmsf,1)
par(mfrow=c(1,2))
plot(lmsf,xlab="Time Period", ylab="Log of MSF")
plot(dmsf,xlab="Time Period", ylab="Log Difference of MSF")
Here, closing stock price of Microsoft has been considered. Analyzing the plots, we can say that the log differenced data is stationary.
par(mfrow=c(2,1))
acf(coredata(dmsf),type='correlation',na.action=na.pass,lag=36, ,main="ACF of dmsf")
acf(coredata(dmsf),type='partial',na.action=na.pass,lag=36,main="PACF of dmsf")
ar4ma1 <- arima(coredata(dmsf), order=c(4,0,1))
ar4ma1
##
## Call:
## arima(x = coredata(dmsf), order = c(4, 0, 1))
##
## Coefficients:
## ar1 ar2 ar3 ar4 ma1 intercept
## -0.0186 -0.0527 -0.0305 -0.0250 -0.0201 8e-04
## s.e. 0.3189 0.0165 0.0201 0.0133 0.3190 2e-04
##
## sigma^2 estimated as 0.0005352: log likelihood = 17800.81, aic = -35587.62
tsdiag(ar4ma1,gof.lag=36)
ar3ma1 <- arima(coredata(dmsf), order=c(3,0,1))
ar3ma1
##
## Call:
## arima(x = coredata(dmsf), order = c(3, 0, 1))
##
## Coefficients:
## ar1 ar2 ar3 ma1 intercept
## 0.3826 -0.0370 -0.0129 -0.4211 8e-04
## s.e. 0.1701 0.0138 0.0162 0.1699 2e-04
##
## sigma^2 estimated as 0.0005353: log likelihood = 17800.2, aic = -35588.39
tsdiag(ar3ma1,gof.lag=36)
ar2ma2 <- arima(coredata(dmsf), order=c(2,0,2))
ar2ma2
##
## Call:
## arima(x = coredata(dmsf), order = c(2, 0, 2))
##
## Coefficients:
## ar1 ar2 ma1 ma2 intercept
## 0.1336 0.201 -0.1744 -0.2493 8e-04
## s.e. 1.6474 1.231 1.6299 1.2749 2e-04
##
## sigma^2 estimated as 0.0005355: log likelihood = 17799.2, aic = -35586.4
tsdiag(ar2ma2,gof.lag=36)
Therefore, based on AIC and diagnostic test, ARIMA(3,0,1) is the best model.
ARCH Model:
par(mfrow=c(1,2))
acf(ar3ma1$residuals^2,type='correlation',na.action=na.pass,lag=36,main="ACF of Squared Residuals")
acf(ar3ma1$residuals^2,type='partial',na.action=na.pass,lag=24,main="PACF of Squared Residuals")
sar3ma1 <- arima(ar3ma1$residuals^2, order=c(3,0,1))
tsdiag(sar3ma1,gof.lag= 36)
arch <-garchFit(~arma(3,1) + garch(7,0), data=dmsf, trace=FALSE)
par(mfrow=c(2,2))
plot(arch@residuals,type="l", xlab="",ylab="",main="Residual")
plot(arch@sigma.t,type="l", xlab="",ylab="",main="Conditional Deviation")
plot(arch@residuals/arch@sigma.t, type="l", xlab="",ylab="",main="Standard Residuals")
plot((arch@residuals/arch@sigma.t)^2, type="l", xlab="",ylab="",main="Squared Standard Residuals")
ch1 <- arch@residuals/arch@sigma.t
ch2 <- (arch@residuals/arch@sigma.t)^2
par(mfcol=c(2,2), cex=0.6, mar=c(2,2,3,1))
acf(ch1, type="correlation", lag.max= 36, ylab="", main="ACF for standardized residuals")
acf(ch1, type="partial", lag.max= 36, ylab="", main="PACF for standardized residuals")
acf(ch2, type="correlation", lag.max= 36, ylab="", main="ACF for squared standardized residuals")
acf(ch2, type="partial", lag.max=36, ylab="", main="PACF for squared standardized residuals")
Q(10) Statistic:
qch1 <- arima(ch1, order=c(10,0,0))
tsdiag(qch1,gof.lag=36)
Q(20) Statistic:
qch2 <- arima(ch2, order=c(10,0,0))
tsdiag(qch2,gof.lag=36)
Jarque-Bera Test:
normalTest(dmsf, method="jb")
##
## Title:
## Jarque - Bera Normalality Test
##
## Test Results:
## STATISTIC:
## X-squared: 66354.9472
## P VALUE:
## Asymptotic p Value: < 2.2e-16
##
## Description:
## Wed Mar 23 20:38:19 2016 by user: ASMSHAKIL
GARCH(m,s) with Student-t innovation
garch22<-garchFit(~garch(2,2), data=coredata(dmsf), trace=FALSE, cond.dist="sstd")
## Warning in sqrt(diag(fit$cvar)): NaNs produced
par(mfrow=c(2,2))
plot(garch22@residuals,type="l", xlab="",ylab="",main="Residuals")
plot(garch22@sigma.t,type="l", xlab="",ylab="",main="Conditional Standard Deviation")
plot(garch22@residuals/garch22@sigma.t, type="l", xlab="",ylab="",main="Standardized Residuals")
plot((garch22@residuals/garch22@sigma.t)^2, type="l", xlab="",ylab="",main="Squared Standardized Residuals")
ex1 <- garch22@residuals/garch22@sigma.t
ex2 <- (garch22@residuals/garch22@sigma.t)^2
par(mfcol=c(2,2), cex=0.6, mar=c(2,2,3,1))
acf(ex1, type="correlation", lag.max=36, ylab="", main="ACF for residuals")
acf(ex1, type="partial", lag.max=36, ylab="", main="PACF for residuals")
acf(ex2, type="correlation", lag.max=36, ylab="", main="ACF for standardized residuals")
acf(ex2, type="partial", lag.max=36, ylab="", main="PACF for squared standardized residuals")
e2 <- arima(ex2, order=c(3,0,0))
tsdiag(e2,gof.lag=36)
TGARCH(m,s):
Tgarch<- garchFit(~garch(2,2), data=coredata(dmsf), trace=FALSE, leverage=TRUE)
## Warning in sqrt(diag(fit$cvar)): NaNs produced
par(mfrow=c(2,2))
plot(Tgarch@residuals,type="l", xlab="",ylab="",main="Residuals")
plot(Tgarch@sigma.t,type="l", xlab="",ylab="",main="Conditional Standard Deviation")
plot(Tgarch@residuals/Tgarch@sigma.t, type="l", xlab="",ylab="",main="Standardized Residuals")
plot((Tgarch@residuals/Tgarch@sigma.t)^2, type="l", xlab="",ylab="",main="Squared Standardized Residuals")
eh2 <- arima(ex2, order=c(10,0,0))
tsdiag(eh2,gof.lag=36)
GARCH model with t-Student innovations:
garchS <- garchFit(~ garch(7,0), data = dmsf, trace = F, cond.dist = "sstd")
garchS
##
## Title:
## GARCH Modelling
##
## Call:
## garchFit(formula = ~garch(7, 0), data = dmsf, cond.dist = "sstd",
## trace = F)
##
## Mean and Variance Equation:
## data ~ garch(7, 0)
## <environment: 0x0000000069d58fc0>
## [data = dmsf]
##
## Conditional Distribution:
## sstd
##
## Coefficient(s):
## mu omega alpha1 alpha2 alpha3 alpha4
## 0.00092692 0.00012196 0.17098959 0.17446704 0.09603789 0.15396924
## alpha5 alpha6 alpha7 skew shape
## 0.10359261 0.09792705 0.14606113 1.04751371 4.09515436
##
## Std. Errors:
## based on Hessian
##
## Error Analysis:
## Estimate Std. Error t value Pr(>|t|)
## mu 9.269e-04 1.941e-04 4.776 1.79e-06 ***
## omega 1.220e-04 9.821e-06 12.418 < 2e-16 ***
## alpha1 1.710e-01 2.366e-02 7.226 4.98e-13 ***
## alpha2 1.745e-01 2.537e-02 6.878 6.07e-12 ***
## alpha3 9.604e-02 2.110e-02 4.551 5.34e-06 ***
## alpha4 1.540e-01 2.373e-02 6.488 8.71e-11 ***
## alpha5 1.036e-01 2.115e-02 4.897 9.72e-07 ***
## alpha6 9.793e-02 2.144e-02 4.568 4.93e-06 ***
## alpha7 1.461e-01 2.288e-02 6.385 1.71e-10 ***
## skew 1.048e+00 1.462e-02 71.626 < 2e-16 ***
## shape 4.095e+00 2.031e-01 20.167 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Log Likelihood:
## 19182.47 normalized: 2.529667
##
## Description:
## Wed Mar 23 20:38:55 2016 by user: ASMSHAKIL