rtime-seriesvolatility

How to plot GARCH model volatility against the time series data?


I am a beginner at R Code and Econometrics. Currently I have created a threshold GARCH model for forecasting volatility of asset prices. I am having a hard time understanding how to plot two charts on top of each other trying to compare volatility against actual time series, or even using that to see how the model performs with actual data.

spec.tgarch <- ugarchspec(variance.model = list(model = "fGARCH",
                                                submodel = "TGARCH",
                                               garchOrder = c(1,1)),
                         mean.model = list(armaOrder=c(1,0)))
tGarchModel <- ugarchfit(spec.tgarch, data=dl_data)
tGarchModel

vol <- ts(tGarchModel@fit$sigma^2,end = c(2024,1), frequency = 12)
plot(vol, xlab="", ylab="", main="Threshold GARCH Volatility")

plot(ts_data, type = "l", col = "blue", ylab = "Price", xlab = "Time", main = "Comparison of Actual Volatility vs Model Predicted Volatility")
lines(vol, col = "red")

# Add a legend
legend("topright", legend = c("Actual Volatility", "Model Predicted Volatility"), col = c("blue", "red"), lty = 1)

Yet what I manage to do is having only the time series data added to the plot (blue) and no red line appears. Also, the legend appears too.


Solution

  • I'm not completely sure what you are wanting to plot, but assuming that you want to see the returns with the modeled volatility overland, perhaps something like this?

    spec.tgarch <- ugarchspec(
      variance.model = list(
        model = "fGARCH",
        submodel = "TGARCH",
        garchOrder = c(1,1)
      ),
      mean.model = list(armaOrder=c(1,0))
    )
    tGarchModel <- ugarchfit(spec.tgarch, data=dl_data)
    tGarchModel
    
    plt <- plot(
      dl_data,
      col = "darkgrey",
      xlab="",
      ylab="",
      main="Threshold GARCH Volatility",
      grid.col = "lightgrey"
    )
    plt <- addSeries(sigma(tGarchModel), col = "red", on = 1)
    plt <- addSeries(-sigma(tGarchModel), col = "red", on = 1)
    plt
    

    enter image description here