Streamlined GARCH Forecasting with the garchf Package in R
The garchf package offers a user-friendly interface for GARCH modeling by leveraging the capabilities of the rugarch backend while adopting the forecast package conventions. This combination allows for GARCH-based forecasts to be produced, assessed, and illustrated using the same frameworks found throughout the forecast ecosystem, akin to functions like forecast::thetaf(). The primary function, xgarchf(), allows users to fit various GARCH models to univariate time series data, incorporating options for different variance models, ARMA orders for the conditional mean, GARCH orders, and distributions. The result is a standard forecast object comprising point forecasts, prediction intervals, and simulated paths.
One of the appealing aspects of this integration is that it brings access to forecast's S3 methods, streamlining the plot generation process and making it easy to compute intricate probabilistic forecasting metrics such as Continuous Ranked Probability Score (CRPS), pinball loss, and Winkler scores through cross-validation techniques.
Using xgarchf for Analyzing Stock Data
To illustrate the application of xgarchf(), consider Google’s daily closing stock price returns. First, you would take the log differences of the prices, which is essential for time series analysis:
y <- diff(log(fpp2::goog200))
Next, various models can be fitted as follows:
fit1 <- xgarchf(y, h = 20, model = "eGARCH")
fit2 <- xgarchf(y, h = 20, model = "sGARCH")
fit3 <- xgarchf(y, h = 20, model = "gjrGARCH")
fit4 <- xgarchf(y, h = 20, model = "iGARCH")
Upon executing these fits, a summary of the results for each model can be printed, highlighting the minimum, maximum, and quartile values for the generated forecasts.
Model Evaluation and Graphical Analysis
The model diagnostics can be examined using the Ljung-Box test, assessing the residuals for auto-correlation:
Ljung-Box test data: Residuals Q* = 10.341, df = 10, p-value = 0.4111 Model df: 0. Total lags used: 10
As seen in the outputs obtained from various fitted models, graphical representation adds another layer of interpretation. Each forecast model can significantly differ in aspects such as volatility estimation and prediction intervals, leading analysts to choose among them based on specific performance metrics.
Cross-validation with the crossvalidation Package
Further enriching the modeling process, the crossvalidation package enables robust model evaluation. By performing cross-validation, you can assess how well the models generalize to unseen data. Here’s a snippet to perform cross-validation:
res <- crossvalidation::crossval_ts( y = y, initial_window = 150, horizon = 10, fixed_window = FALSE, fcast_func = garchf::xgarchf, eval_metric = eval_metric95, fit_params = list(arma_order=c(0, 0), garch_order = c(1, 1), model = "eGARCH", level=95), show_progress = FALSE )
This function allows for customizable evaluation metrics tailored to specific forecasting needs. You could define metrics such as root mean square error (RMSE), mean absolute error (MAE), and coverage, and capture their performance across different GARCH configurations.
Performance Metrics Summary
The results provide insights into each model’s predictive accuracy and reliability. Key metrics such as RMSE and MAE illustrate the models' performance, while metrics like coverage rates and specific losses offer deeper insights into what models may perform best under varying conditions.
RMSE MAE Coverage95 Winkler95 Min.:0.006459 Min.:0.004896 Min.:0.7000 Min.:0.03441 1st Qu.:0.008001 1st Qu.:0.006266 1st Qu.:0.9000 1st Qu.:0.06153 Median:0.009135 Median:0.007061 Median:1.0000 Median:0.06466 Mean :0.017189 Mean :0.010613 Mean :0.9475 Mean :0.20090 3rd Qu.:0.022415 3rd Qu.:0.015066 3rd Qu.:1.0000 3rd Qu.:0.47706 Max. :0.042741 Max. :0.023054 Max. :1.0000 Max. :0.96625
The garchf package thus stands as a valuable tool for analysts seeking to apply GARCH methodologies to forecasting with a familiar interface and the power of cross-validation to back their decisions.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Continue reading: ‘garchf’: GARCH probabilistic forecasting with package ‘forecast’-style interface (and ‘rugarch’ under the hood)