I was recently asked to report the r-squared statistics together with the estimations of GARCH models with exogenous regressors on the conditional mean equation. However, there is no function to get these statistics automatically using the rugarch
package.
My question is straightforward:
Can I use the residuals of the fitted model (uGARCHfit
class object) to calculate the r-squared and the adjusted r-squared manually or should I use the estimated sigma
from the fitted model to weight an lm
object and extract the r-squared from there?
I did some research but I couldn't find anything in the vignette of the package.
Two exchanges on cross-validated touched the issue:
How to measure the goodness of fit of a GARCH model?
How to extract the adjusted r squared from uGARCHfit class data?
But they don't provide a definitive answer.
Here is a reproducible example of what I want to do:
library(rugarch)
library(zoo)
library(dplyr)
library(purrr)
library(tidyr)
library(fredr)
# Get and clean the data
fredr_set_key("your_key_here") # fredr package requires a key to download the data.
df = map_dfr(c('DEXBZUS', 'DCOILWTICO'), fredr) %>%
filter(date > '2010-01-01')
df1 = df %>%
spread(series_id, value) %>%
set_names('date', 'oil', 'brl') %>%
mutate_at(vars(-date), list(~ log(.) - lag(log(.)))) %>%
drop_na
oil = as.matrix(zoo(df1$oil, df1$date))
brl = as.matrix(zoo(df1$brl, df1$date))
# Estimate the model
m1_spec = ugarchspec(variance.model = list(model = 'eGARCH',
garchOrder = c(1,1)),
mean.model = list(armaOrder = c(1,0),
external.regressors = oil),
distribution.model = 'norm')
m1_fit = ugarchfit(spec = m1_spec, data = brl,
solver.control = list(trace = 1))
# Calculate the r-squared statistics
r2 = 1 - (sum(residuals(m1_fit)^2) / sum((brl - mean(brl))^2))
r2a = 1 - ((1 - r2) * (length(brl) - 1) / (length(brl) - 3))
```