## simulate a time series of returns
x <- garchSim( garchSpec(), n = 500)
class(x)
## fit a GARCH model
fit <- garchFit(~ garch(1, 1), data = x, trace = FALSE)
head(VaR(fit))
head(ES(fit))
## use plot method for fitted GARCH models
plot(fit, which = 14) # VaR
plot(fit, which = 15) # ES
plot(fit, which = 16) # VaR & ES
## plot(fit) # choose the plot interactively
## diy plots
## overlay VaR and ES over returns
## here x is from class 'timeSeries', so we convert VaR/ES to timeSeries
## don't forget to negate the result of VaR()/ES(),
plot(x)
lines(timeSeries(-VaR(fit)), col = "red")
lines(timeSeries(-ES(fit)), col = "blue")
## alternatively, plot losses (rather than returns) and don't negate VaR()/ES()
plot(-x)
lines(timeSeries(VaR(fit)), col = "red")
lines(timeSeries(ES(fit)), col = "blue")
Run the code above in your browser using DataLab