我试图在R中将多个图组合成一个pdf。到目前为止,我一直在使用par()函数。但它不适用于绘制dcc.fit。 当绘制dcc拟合时,我需要进行绘图选择,当我尝试组合图形时,它们只是分别显示每个图形。我现在第二天这样做了,我没有希望了。
par()的代码:
par(mfrow=c(2,2))
plot(GSPC.ret, main="USA")
plot(dcc.fit, which=4, ask=F, main="Canada")
plot(MXX.ret, main="Mexico")
plot(BVSP.ret, main="Brazil")
我使用getsymbols从Yahoo获取数据:
# download data
symbol.vec = c("^GSPC", "^GSPTSE")
getSymbols(symbol.vec, from = "2003-01-02", to = "2012-12-31")
colnames(GSPC)
start(GSPC)
end(GSPC)
# extract adjusted closing prices
GSPC = GSPC[, "GSPC.Adjusted", drop=F]
GSPTSE = GSPTSE[, "GSPTSE.Adjusted", drop=F]
# plot prices
plot(GSPC)
plot(GSPTSE)
# calculate log-returns for GARCH analysis
GSPC.ret = CalculateReturns(GSPC, method="log")*100
GSPTSE.ret = CalculateReturns(GSPTSE, method="log")*100
# remove first NA observations
GSPC.ret = GSPC.ret[-1,]
GSPTSE.ret = GSPTSE.ret[-1,]
colnames(GSPC.ret) = "GSPC"
colnames(GSPTSE.ret) = "GSPTSE"
# create combined data series
GSPC.GSPTSE.ret = merge(GSPC.ret,GSPTSE.ret)
# plot returns
plot(GSPC.ret)
plot(GSPTSE.ret)
# remove NA (newsata <- GSPC.GSPTSE.ret)
is.na(GSPC.GSPTSE.ret)
GSPC.GSPTSE.ret[!complete.cases(GSPC.GSPTSE.ret)]
GSPC.GSPTSE.ret <- na.omit(GSPC.GSPTSE.ret)
# change for without NA
GSPC.ret <- GSPC.GSPTSE.ret$GSPC
GSPTSE.ret <- GSPC.GSPTSE.ret$GSPTSE
来自http://faculty.washington.edu/ezivot/econ589/econ589multivariateGarch.r
的DCC GARCH模型代码 #
# DCC estimation
#
# univariate normal GARCH(1,1) for each series
garch11.spec = ugarchspec(mean.model = list(armaOrder = c(0,0)),
variance.model = list(garchOrder = c(1,1),
model = "sGARCH"),
distribution.model = "norm")
# dcc specification - GARCH(1,1) for conditional correlations
dcc.garch11.spec = dccspec(uspec = multispec( replicate(2, garch11.spec) ),
dccOrder = c(1,1),
distribution = "mvnorm")
dcc.garch11.spec
dcc.fit = dccfit(dcc.garch11.spec, data = GSPC.GSPTSE.ret)
class(dcc.fit)
slotNames(dcc.fit)
names(dcc.fit@mfit)
names(dcc.fit@model)
# many extractor functions - see help on DCCfit object
# coef, likelihood, rshape, rskew, fitted, sigma,
# residuals, plot, infocriteria, rcor, rcov
# show, nisurface
# show dcc fit
dcc.fit
# plot method
plot(dcc.fit)
# conditional sd of each series
plot(dcc.fit, which=2)
# conditional correlation
plot(dcc.fit, which=4)
我想绘制每个国家/地区的条件相关性。我只需要选择4。
Make a plot selection (or 0 to exit):
1: Conditional Mean (vs Realized Returns)
2: Conditional Sigma (vs Realized Absolute Returns)
3: Conditional Covariance
4: Conditional Correlation
5: EW Portfolio Plot with conditional density VaR limits
Selection: 4
图表只是单独显示。
有没有办法将这些图组合成单个pdf?
感谢您的帮助。 谢谢!
答案 0 :(得分:0)
您可以尝试从估算中获取时间序列,并根据需要将它们放入绘图中。有如何将相关性作为动物园对象获取。
#to get time varying correlation
rho.est.line <- list()
rho.est.line = rcor(dcc.fit, type="R") # plot(rho.est.line[1,2,])
outputcorr <- matrix((rho.est.line[1,2,]), nrow = dim(returns)[1], byrow = TRUE)
rho.est = data.frame((outputcorr))
rho.est.zoo = zoo(rho.est, order.by=index(returns[,1]))