## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
Sys.setlocale("LC_TIME", "English")

## ----setup, message = FALSE, echo = FALSE, warning = FALSE--------------------
library(fEGarch)

## ----eval = FALSE, echo = TRUE------------------------------------------------
# install.packages("fEGarch")

## ----eval = FALSE, echo = TRUE, warning = FALSE-------------------------------
# library(fEGarch)

## ----message = FALSE----------------------------------------------------------
library(zoo)
rt <- window(SP500, start = "2010-01-01", end = "2019-12-31")

## -----------------------------------------------------------------------------
spec <- egarch_spec(orders = c(1, 1), cond_dist = "ged")

## -----------------------------------------------------------------------------
model1 <- spec %>%
  fEGarch(rt, n_test = 250)
model1

## -----------------------------------------------------------------------------
vola <- sigma(model1)      # Fitted conditional standard deviations
means <- cmeans(model1)    # Fitted (conditional) means  
res <- residuals(model1)   # (Standardized) residuals
params <- pars(model1)     # Estimated parameters
serrors <- se(model1)      # Standard errors

## ----fig.height = 3.5, fig.width = 7, fig.align='center'----------------------
plot(model1, which = 1, xlab = "Year", main = "S&P500 log-returns", ylab = "Log-return")

## ----fig.height = 3.5, fig.width = 7, fig.align='center'----------------------
plot(model1, which = 3, xlab = "Year", main = "Fitted conditional standard deviations",
     ylab = "Volatility")

## ----fig.height = 3.5, fig.width = 7, fig.align='center'----------------------
plot(model1, which = 4, xlab = "Year", main = "Residual value", ylab = "Standardized residuals")

## -----------------------------------------------------------------------------
model2 <- fiaparch(rt, cond_dist = "std", n_test = 250)
model2

## -----------------------------------------------------------------------------
model2 <- fiaparch_spec(cond_dist = "std") %>% fAParch(rt, n_test = 250)
model2

## -----------------------------------------------------------------------------
model2 <- fAParch_spec(
  cond_dist = "std",
  model_type = "aparch",
  long_memo = TRUE,
  adjust = FALSE,
  orders = c(1, 1)
) %>% fAParch(rt, n_test = 250)
model2

## -----------------------------------------------------------------------------
model3 <- fAParch_spec(
  cond_dist = "std",
  model_type = "aparch",
  long_memo = TRUE,
  adjust = TRUE,
  orders = c(1, 1)
) %>% fAParch(rt, n_test = 250)

model3 <- afiaparch_spec(cond_dist = "std") %>%
  fAParch(rt, n_test = 250)

model3 <- afiaparch(rt, cond_dist = "std", n_test = 250)
model3

## -----------------------------------------------------------------------------
vol_fcasts <- model1 %>% predict_roll()

## -----------------------------------------------------------------------------
risk_fcasts <- vol_fcasts %>% measure_risk()

## ----fig.height = 3.5, fig.width = 7, fig.align='center'----------------------
plot(risk_fcasts, which = 0.975, main = "97.5%", xlab = "Month")

## -----------------------------------------------------------------------------
risk_fcasts %>% backtest_suite()

## ----eval = FALSE-------------------------------------------------------------
# egarch_spec(cond_dist = "ged") %>%
#   fEGarch(rt, n_test = 250) %>%
#   predict_roll() %>%
#   measure_risk() %>%
#   backtest_suite()

