R/handle_exceptions.R

Defines functions handle_short_ts

Documented in handle_short_ts

#' handle_short_ts
#' @param tisefka historical data of time series
#' @param min_points minimum points for which we'll genrate the forecast
#' @param fcast_horizon forecast horizon
#' @export

handle_short_ts <- function(tisefka = NULL, ukud_unit = NULL, min_points = 12,fcast_horizon = 18){
  # ts_actuals <- cumsum(rnorm(8, 5,3))

  ts_actuals <- tisefka%>%dplyr::pull(2)
  if(is.null(ts_actuals))return(NULL)

  if(length(ts_actuals) > 12)cycle_D <- 12
  st_ts_model <- forecast::auto.arima(ts_actuals,D = cycle_D ,max.p = 3, max.q = 3)
  fcast_object <- forecast::forecast(st_ts_model, fcast_horizon)
  fcast_object$method <- "thaink2_naive"
  fcast_object$x      <- as.numeric(fcast_object$x)
  fcast_object$fitted <- as.numeric(fcast_object$fitted)
  fcast_object$upper  <- as.numeric(fcast_object$upper[,2])
  fcast_object$lower  <- as.numeric(fcast_object$lower[,2])

  future <- generate_future_dates( tisefka = tisefka , ukud_unit = ukud_unit, asurif_zdat = fcast_horizon)

  future <- future%>%
    tail(-nrow(tisefka))
  if(!(ukud_unit%in%c("months","quarters","years","days"))){
    fcast_object$forecast_dates <- as.POSIXct(head(future$ds, fcast_horizon), tz = "CET")
  }else{
    fcast_object$forecast_dates <- as.Date(head(future$ds, fcast_horizon), tz = "CET")
  }
  return(fcast_object)
}
Aqvayli06/SaldaeForecasting documentation built on Sept. 9, 2023, 7:21 p.m.