I’m clearly out of my realm of competence with most of the rugarch functions, but I thought it might be nice to provide an example combining plot.xts and uGARCHroll.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#install.packages("xtsExtra", repos="http://R-Forge.R-project.org") | |
require(quantmod) | |
require(rugarch) | |
require(xtsExtra) #if you get an error, see first line and install from r-forge | |
getSymbols("DEXJPUS",src="FRED") | |
DEXJPUS<-1/to.weekly(DEXJPUS) | |
ugarch.panel <- function(index,x,type,cex,col,pch,...){ | |
spec = ugarchspec( | |
variance.model=list(garchOrder=c(1,1)), | |
mean.model=list(armaOrder=c(1,1), include.mean=T)) | |
#get ugarchroll; I cannot say I completely understand what I'm doing here | |
ugr <- ugarchroll(spec, | |
data = na.omit(x), | |
forecast.length = floor(NROW(na.omit(x)) / 1000) * 1000, | |
n.ahead = 1, | |
refit.every = 50, | |
refit.window = "moving") | |
#get garch coefficients | |
ugr.var <- merge(x,as.data.frame(ugr,which="VaR")) | |
ugr.var <- as.xts(apply(ugr.var,MARGIN=2,na.fill,fill=c(0,"extend")),order.by=index(x))[,2:4] | |
print(tail(ugr.var)) | |
default.panel(index,ugr.var[,3],type="h",cex,pch,col,...) | |
default.panel(index,ugr.var[,1],type="l",cex,pch,col="red",...) | |
default.panel(index,ugr.var[,2],type="l",cex,pch,col="gray50",...) | |
text(x=index[1],y=par("usr")[4],"VaR from rugarch ugarchroll",adj=c(0,1),cex=0.8) | |
} | |
plot.xts(na.omit(merge(DEXJPUS[,4],ROC(DEXJPUS[,4],n=1,type="discrete"))), | |
screens=c(1,2), | |
minor.ticks=FALSE, major.format="%Y", | |
panel=c(default.panel,ugarch.panel), | |
main="Japanese Yen (source: St. Louis Federal Reserve)") | |
Hello
ReplyDeleteI am getting:
Error in plot.default(xycoords$x, xycoords$y, type = type, axes = FALSE, :
argument 7 matches multiple formal arguments
In addition: Warning message:
In plot.xts(na.omit(merge(DEXJPUS[, 4], ROC(DEXJPUS[, 4], n = 1, :
only the univariate series will be plotted
Trace back in klr's blog posts, and you'll see that he's using a new version of plot.xts that is still being worked on quite heavily. Make sure you're using the most recent version from svn in the package xtsExtra on r-forge. pcc
DeleteThat looks like you're using xts:::plot.xts. You need to use xtsExtra:::plot.xts.
Deletethx for the post.. oddly i get the error 'Error in plot.xts(na.omit(merge(DEXJPUS[, 4], ROC(DEXJPUS[, 4], n = 1, :
ReplyDeleteobject 'default.panel' not found'
even just copy/paste your code. could not find where default.panel function was coming from.
see above, or if the install from r-forge does not work, go to https://r-forge.r-project.org/scm/viewvc.php/pkg/xtsExtra/R/plot.R?view=markup&root=xts and copy/paste into a script. Run or source that copied/pasted code.
ReplyDeletedefault.panel comes from xtsExtra::plot.xts
weird, i have xtsExtra installed and loaded.. but yes when i ran the plot.R code separately everything worked fine after that. will keep banging around in the repository and looking there if stuff isn't working for this new package. thx again.
ReplyDeleteThis isn't working. I have all the packages in their latest versions.
ReplyDeleteThe problem is with ... ugarch.panel, it's not doing the math right ...
Error in xts(x, order.by = order.by, frequency = frequency, ...) :
NROW(x) must match length(order.by)
In addition: There were 17 warnings (use warnings() to see them)
Warning messages:
1: In .makefitmodel(garchmodel = "sGARCH", f = .sgarchLLH, ... : NaNs produced
2: In arima(data, order = c(modelinc[2], 0, modelinc[3]), ... :
possible convergence problem: optim gave code=1
3: In .makefitmodel(garchmodel = "sGARCH", f = .sgarchLLH, ... : NaNs produced
etc. ...: NaNs produced