R/get_garima_score.R

Defines functions get_garima_score

Documented in get_garima_score

#' Get residuals of generalized ARMA-Model
#'
#' Estimates order of ARMA process with auto.arima
#' Takes estimated Distribution of x as link function in model
#'
#' @param x vector containing time-series values. are assumed to be from day to day / week to week / month to month
#' @return vector with residuals of garmaFit
#' @export
get_garima_score <- function(x){

  library(forecast)
  library(gamlss)
  library(gamlss.util)

  assertthat::assert_that(is.numeric(x), msg = "x must be numeric")

  order.proxy <- arimaorder(auto.arima(x, max.d = 0))[c(1,3)]

  distribution_class <- get_distribution_class(x)
  if(distribution_class == "real0to1") x <- (x+0.01)/1.011

  distributions <- get_distribution(x, dist_class = distribution_class)

  fit <- fit_garma(y = x, order = order.proxy, dists = distributions)

  if(!exists("fit")) return(rep(NA, nrow(df)))

  res <- fit$residuals
  if(max(order.proxy) > 0) res[1:(1 + max(order.proxy))] <- NA
  res[res == Inf] = NA
  res[res == -Inf] = NA
  res[is.nan(res)] = NA
  return(res)
}
td-berlin/anomalizer documentation built on Feb. 21, 2020, 2:03 a.m.