我需要根据GARCH(1,1),EGARCH(1,1)和TGARCH(1,1)计算10分钟时间间隔的EUR / USD对的平均波动率,并在一个图中显示。我的数据集存储在csv中。格式和数据频率为1分钟。我确实有以下代码,但仅仅在附图中计算和绘制它是不够的。
# installation of needed packages
# install.packages("fBasics")
# install.packages("tseries")
# install.packages("car")
# install.packages("FinTS")
# install.packages("fGarch")
# install.packages("rugarch")
# Library
library(xts)
library(fBasics) # e.g. basicStats()
library(tseries)# e.g. jarque.bera.test()
library(car) # e.g. durbinWatsonTest()
library(FinTS) # e.g. ArchTest()
library(fGarch) # e.g. garchFit()
library(rugarch) # e.g. ugarchfit()
# Data import
EUR <- read.csv("Data_Forecast/EURUSD.csv",
stringsAsFactors = FALSE) #o import data
# First and last 6 data
head(EUR)
tail(EUR)
# formatting the date and time
EUR$Date <- strptime(EUR$Date, format = "%d.%m.%Y %H:%M:%S")
str(EUR)
EUR <- EUR[EUR$Date >= strptime("2015-01-01 23:00:00", format = "%Y-%m-%d %H:%M:%S"),]
# we need just weekdays
wday_ <- as.POSIXlt(EUR$Date)$wday
EUR <- EUR[!wday_ %in% c(0,6) ,]
# lets create vector of hours and minutes based on the existing data
hour_ <- as.POSIXlt(EUR$Date)$hour
minute_ <- as.POSIXlt(EUR$Date)$min
###########################################################################
# Exclude first and last 5 minutes
exclude_ <- ((hour_ == 23 & minute_ < 6) | (hour_ == 22 & minute_ > 55) )*1
EUR <- EUR[exclude_ == 0,]
head(EUR)
tail(EUR)
# xts object creation
EUR.xts<-xts(EUR$Close,EUR$Date)
# plot of original close pricess
plot(EUR$Date,EUR$Close,type="l", col="blue",
main="EUR/USD")
# log-returns calculation
EUR$r<-diff.xts(log(EUR$Close))
# log returns plot
plot(EUR$Date,EUR$r,type="l", col="red",
main="Log-returns of USD/JPY")
abline(h=0,col="gray",lty=2) #abline functions adds line to the plot
######################################################################
#GARCH MODEL
#################################################################
# remove missing values and zeroes
data_ <- EUR[!is.na(EUR$r) & EUR$r!=0,]
k.garch11<-garchFit(~garch(1,1),
data = 100*data_$r,
include.mean=F,
cond.dist= "norm", # conditional distribution of errors
trace=FALSE) # if we don't want to see history of iterations
summary(k.garch11)
# lets see if the model converges on a shorter sample
k.garch11<-garchFit(~garch(1,1),
data = 100*data_$r[1:15000],
include.mean=F,
cond.dist= "norm", # conditional distribution of errors
trace=FALSE) # if we don't want to see history of iterations
k.garch11<-garchFit(~garch(1,1),
data = 100*data_$r[-c(1:15000)],
include.mean=F,
cond.dist= "norm", # conditional distribution of errors
trace=FALSE) # if we don't want to see history of iterations
plot(100*data_$r[30000:32000], type="l")
which(100*data_$r > 0.9)
k.garch11<-garchFit(~garch(1,1),
data = 100*data_$r[-15000],
include.mean=F,
cond.dist= "norm", # conditional distribution of errors
trace=FALSE) # if we don't want to see history of iterations
summary(k.garch11)
# Let's assume that the final model is GARCH(1,1)
str(k.garch11)
# Plot of conditional variance estimates
par(mfrow=c(2,1))
plot(k.garch11@data, # @data = original data values
type="l",col="red",ylab="r",main="Log-returns of EUR/USD")
plot(k.garch11@h.t, # @h.t = conditional variance
type="l",col="blue",
ylab="cvar",main="Estimated Volatility of EUR/USD")
par(mfrow=c(1,1))
# Do standardized residuals come from normal distribution?
stdres<-k.garch11@residuals/sqrt(k.garch11@h.t)
hist(stdres,breaks=20,prob=T,
main="Histogram of standardized residuals \n from GARCH(1,1) for EUR/USD")
############################################################################
# The EGARCH model
#########################################################################
# Let's examine whether conditional variance reacts asymmetrically
# to the news arriving to the market.
# Below estimation of the EGARCH(1,1) model.
# ugarchfit() from rugarch package
# lets first define a model specification
spec = ugarchspec(# variance equation
variance.model=list(model="eGARCH",garchOrder=c(1,1)),
# sGARCH would stand for standard GARCH model
# mean equation
mean.model=list(armaOrder=c(0,0),include.mean=F),
# assumed distribution of errors
distribution.model="norm")
# function doesn't accept missing values
k.egarch11 = ugarchfit( spec=spec, data=na.omit(EUR$r))
k.egarch11
# Plot of conditional standard deviation estimates (3)
# and News-Impact curve (12).
# ESC to exit
plot(k.egarch11)
##########################################################################
# The TGARCH model
###################################################################
# lets first define a model specification
spec = ugarchspec(# variance equation
variance.model=list(model="fGARCH",garchOrder=c(1,1),
submodel="TGARCH"),
# model="fGARCH" (family GARCH) together with submodel="TGARCH"
# mean equation
mean.model=list(armaOrder=c(0,0),include.mean=F),
# assumed distribution of errors
distribution.model="norm")
# function doesn't accept missing values
k.tgarch11 = ugarchfit( spec=spec, data=na.omit(EUR$r))
k.tgarch11
# Plot of News-Impact curve (12).
# ESC to exit
plot(k.tgarch11)
答案 0 :(得分:1)
我认为您要问的是如何叠加多个图。您附加的代码使用R中的基本绘图函数。假设您的示例中的每个绘图都正常,这就是使用基本绘图函数覆盖多个绘图的方式:
plot(k.garch11, xlim=c(X_min, X_max),ylim=c(Y_min, Y_max))
par(new=T)
plot(k.tgarch11,xlim=c(X_min, X_max),ylim=c(Y_min, Y_max))
par(new=T)
plot(k.egarch11,xlim=c(X_min, X_max),ylim=c(Y_min, Y_max))
xlim
和ylim
标准化轴限制,以便在数据范围内保持一致的轴。 X_min,X_max,Y_min,Y_max都是数字,由您指定。 par(new=T)
告诉R不要制作新的全新情节,而是将下一个情节叠加在当前情节之上。
通过阅读?par
可以获得其他绘图参数(例如轴标签)。
?legend
和?axis
将帮助您使图例和轴更漂亮。