R/fit_garma.R

Defines functions fit_garma

Documented in fit_garma

#' Fit generalized ARMA-Model
#'
#' This function tries to fit a generalized ARMA-Model with given distribution of the dependent variable
#' taken as link-function. As numeric optimization sometimes failes, the function walks through every
#' distribution specified in given order and stops when estimation was successful. Optionally, a matrix of
#' regressor variables can be given. Mandatory, the ARMA order must be given as a 2-length-vector (AR, MA)
#'
#' @param y Dependent Variable
#' @param order vector containing AR und MA order
#' @param dists vector of gamlss.family-distributions of y taken as link function
#' @param x.matrix optional matrix of regressors, use to model seasonality
#' @return An garmaFit-object
#' @export
fit_garma <- function(y, order, dists, x.matrix = NA){
  library(tidyverse)
  library(gamlss)
  library(gamlss.util)

  if(is.na(x.matrix)) x.matrix <- rep(0, length(y))

  counter = 1
  success = FALSE
  while(!success & counter <= length(dists)){
    fit <- try(garmaFit(formula = y ~ 1, family = dists[counter], order = order))
    counter = counter + 1
    if("gamlss" %in% class(fit)) success = TRUE
  }
  if(!"gamlss" %in% class(fit)){
    return(NULL)
    } else { return(fit)}
}
td-berlin/anomalizer documentation built on Feb. 21, 2020, 2:03 a.m.