R/decomposeSPTM.R

#' Conversion from a long dataframe to a sptm object
#'
#' This function allows you to calculate a Gaussian process kernel
#' @param x ts
#' @param l length
#' @param f frequency
#' @param type type
#' @param filter filter
#' @keywords data conversion
#' @export
#' @examples
#' data("wq_analysis_week2")
#' SPTMData(wq.raw.obs, frequency = "quarter")
decomposeSPTM <- function (x,l,f, type = c("additive", "multiplicative"), filter = NULL)
{
  type <- match.arg(type)
  #l <- length(x)
  #f <- frequency(x)
  if (f <= 1 || length(na.omit(x)) < 2 * f)
    stop("time series has no or less than 2 periods")
  if (is.null(filter))
    filter <- if (!f%%2)
      c(0.5, rep_len(1, f - 1), 0.5)/f
  else rep_len(1, f)/f
  trend <- filter(x, filter)
  season <- if (type == "additive")
    x - trend
  else x/trend
  periods <- l%/%f
  index <- seq.int(1L, l, by = f) - 1L
  figure <- numeric(f)
  for (i in 1L:f) figure[i] <- mean(season[index + i], na.rm = TRUE)
  figure <- if (type == "additive")
    figure - mean(figure)
  else figure/mean(figure)
  seasonal <- ts(rep(figure, periods + 1)[seq_len(l)], start = start(x),
                 frequency = f)
  structure(list(x = x, seasonal = seasonal, trend = trend,
                 random = if (type == "additive") x - seasonal - trend else x/seasonal/trend,
                 figure = figure, type = type), class = "decomposed.ts")
}
ick003/SpTMixture documentation built on May 18, 2019, 2:32 a.m.