Description Usage Arguments Details Value
View source: R/parsnip-garch_reg.R
#' Low-Level GARCH function for translating modeltime to forecast
#'
#' @param formula A dataframe of xreg (exogenous regressors)
#' @param data A numeric vector of values to fit
#' @param a The order of the non-seasonal auto-regressive (AR) terms. Often denoted "p" in pdq-notation.
#' @param g The order of the non-seasonal auto-regressive (AR) terms. Often denoted "p" in pdq-notation.
#' @param ... Additional arguments passed to forecast::Arima
#'
#' @export
garch_fit_impl <- function(formula, data, a = 1, g = 1, ar_no_apply = NULL, ma_no_apply = NULL, period = "auto", ...)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | # X & Y
others <- list(...)
y <- all.vars(formula)[1]
x <- attr(stats::terms(formula, data = data), "term.labels")
outcome <- data[[y]]
predictors <- data %>% dplyr::select(dplyr::all_of(x))
# INDEX & PERIOD
# Determine Period, Index Col, and Index
index_tbl <- modeltime::parse_index_from_data(predictors)
period <- modeltime::parse_period_from_index(index_tbl, period)
idx_col <- names(index_tbl)
idx <- timetk::tk_index(index_tbl)
# XREGS
# Clean names, get xreg recipe, process predictors
# xreg_recipe <- create_xreg_recipe(predictor, prepare = TRUE)
# xreg_matrix <- juice_xreg_recipe(xreg_recipe, format = "matrix")
# FIT
outcome <- stats::ts(outcome, frequency = period)
fit_garch <- tseries::garch(outcome, order = c(a, g), ...)
# RETURN
modeltime::new_modeltime_bridge(
class = "garch_fit_impl",
# Models
models = list(
model_1 = fit_garch
),
# Data - Date column (matches original), .actual, .fitted, and .residuals columns
data = tibble::tibble(
!! idx_col := idx,
.actual = as.numeric(outcome),
.fitted = fit_garch$fitted.values[,1],
.residuals = fit_garch$residuals
),
extras = list(
y_var = y,
period = period,
otros = others
),
# Description - Convert arima model parameters to short description
desc = stringr::str_glue('GARCH ({fit_garch$order[1]}, {fit_garch$order[2]}) Model')
)
|
1 2 3 4 5 6 7 8 9 10 11 |
formula |
A dataframe of xreg (exogenous regressors) |
data |
A numeric vector of values to fit |
a |
The order of ARCH part |
g |
The order of GARCH part |
ar |
The order of the non-seasonal auto-regressive (AR) terms. Often denoted "p" in pdq-notation. |
ma |
The order of the non-seasonal auto-regressive (AR) terms. Often denoted "p" in pdq-notation. |
tune_by |
Parameter for tuning. |
period |
Period |
... |
Additional arguments passed to |
#' @export print.garch_fit_impl <- function(x, ...) print(x$models$model_1) invisible(x) Low-Level GARCH function for translating modeltime to forecast
A fitted model
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.