rm(list=ls())
source("http://klein.uk/R/myfunctions.R")
ls()
sp <- read.csv("http://klein.uk/R/Lent/s&p500 daily.csv")
str(sp)
date <- as.Date(sp$Date, format="%d/%m/%Y")
library(zoo)
adj <- zoo(sp$Adj, as.Date(date))
plot(adj)
adj88 <- window(adj, start=as.Date("1988-01-01"))
plot(adj88)
dlsp <- diff(log(adj88), lag=1)
plot(dlsp)
dlsp.v <- as.vector(dlsp)
adf.test.1(dlsp.v, int=F, trend=F, k=1)
par(mfrow=c(2,1))
acf(na.omit(dlsp.v)); pacf(na.omit(dlsp.v))
arima202 <- arima(dlsp, order=c(2,0,2))
library(lmtest)
coeftest(arima202)
nNA <- which(is.na(arima202$resid)==F)
library(TSA)
acf(rstandard(arima202)[nNA]); pacf(rstandard(arima202)[nNA])
acf(rstandard(arima202)[nNA]^2); pacf(rstandard(arima202)[nNA]^2)
library(rgarch)
spec <- ugarchspec(variance.model = list(model = "fGARCH", submodel="GARCH",
garchOrder = c(9,0)),
mean.model = list(armaOrder = c(2,2), include.mean=T))
sgarch.fit <- ugarchfit(data=dlsp.v, spec = spec)
sgarch.fit
nNA <- which(is.na(sgarch.fit@fit$resid)==F)
stdresid <- sgarch.fit@fit$residuals/sgarch.fit@fit$sigma
acf(stdresid[nNA]); pacf(stdresid[nNA])
ljung.box.test.1(stdresid[nNA], seq(1,15,1))
acf(stdresid[nNA]^2); pacf(stdresid[nNA]^2)
ljung.box.test.1(stdresid[nNA]^2, seq(1,15,1))
spec <- ugarchspec(variance.model = list(model = "fGARCH", submodel="GARCH",
garchOrder = c(2,1)),
mean.model = list(armaOrder = c(1,12), include.mean=T),
fixed.pars = list(ma2=0, ma3=0, ma4=0, ma5=0, ma6=0, ma7=0, ma8=0, ma9=0, ma10=0, ma11=0))
sgarch.fit <- ugarchfit(data=dlsp, spec = spec)
sgarch.fit
nNA <- which(is.na(sgarch.fit@fit$resid)==F)
stdresid <- sgarch.fit@fit$residuals/sgarch.fit@fit$sigma
acf(stdresid[nNA]); pacf(stdresid[nNA])
ljung.box.test.1(stdresid[nNA], seq(1,15,1))
library(moments)
jarque.test(stdresid)
jarque.test(dlsp.v)
?ugarchforecast
plot(ugarchforecast(sgarch.fit, n.ahead = 50))
?ugarchspec
spec <- ugarchspec(variance.model = list(model = "fGARCH", submodel="TGARCH",
garchOrder = c(2,1)),
mean.model = list(armaOrder = c(1,12), include.mean=T),
fixed.pars = list(ma2=0, ma3=0, ma4=0, ma5=0, ma6=0, ma7=0, ma8=0, ma9=0, ma10=0, ma11=0))
sgarch.fit <- ugarchfit(data=dlsp.v, spec = spec)
sgarch.fit
spec <- ugarchspec(variance.model = list(model = "iGARCH", garchOrder = c(2,1)),
mean.model = list(armaOrder = c(1,12), include.mean=T),
fixed.pars = list(ma2=0, ma3=0, ma4=0, ma5=0, ma6=0, ma7=0, ma8=0, ma9=0, ma10=0, ma11=0))
sgarch.fit <- ugarchfit(data=dlsp.v, spec = spec)
sgarch.fit
spec <- ugarchspec(variance.model = list(model = "eGARCH", garchOrder = c(2,1)),
mean.model = list(armaOrder = c(1,12), include.mean=T),
fixed.pars = list(ma2=0, ma3=0, ma4=0, ma5=0, ma6=0, ma7=0, ma8=0, ma9=0, ma10=0, ma11=0))
sgarch.fit <- ugarchfit(data=dlsp.v, spec = spec)
sgarch.fit
spec <- ugarchspec(variance.model = list(model = "fGARCH", submodel="GARCH",
garchOrder = c(2,1)),
mean.model = list(armaOrder = c(1,12), include.mean=T, garchInMean=T),
fixed.pars = list(ma2=0, ma3=0, ma4=0, ma5=0, ma6=0, ma7=0, ma8=0, ma9=0, ma10=0, ma11=0))
sgarch.fit <- ugarchfit(data=dlsp, spec = spec)
sgarch.fit
nNA <- which(is.na(sgarch.fit@fit$resid)==F)
stdresid <- sgarch.fit@fit$residuals/sgarch.fit@fit$sigma
ljung.box.test.1(stdresid[nNA], seq(1,15,1))
ljung.box.test.1(stdresid[nNA]^2, seq(1,15,1))
jarque.test(stdresid)
?ugarchforecast
plot(ugarchforecast(sgarch.fit, n.ahead = 50))