R/preprocess_function.R

#' Preprocessing Data to Be Tested with Statiotest.
#'
#' Trims, detrends and demeans a vector of numbers.
#'
#' @param x1 A vector of numbers.
#' @param idemean A boolean, True is Default which means the sample mean
#' substracted from data.
#' @param idetrend A boolean, Default = True, which means that a detrending
#' is done using the pracma package and OLS, which might or might not impact
#' the data depending on presence of trend.
#' @param itrim A number that gives the percentage trimming in each tail.
#' @return The vector of preprocessed data.
#' @examples
#'preprocess(x1 = rnorm(1000), idemean = TRUE, idetrend = TRUE, itrim = 0)

#' @export
preprocess <- function( x1 = NULL, idemean = TRUE, idetrend = TRUE, itrim = 1 ){

  # Check for correct values of arguments

  #create local copy of x1 so that original is not modified in place
  y <- x1
  #demeaning
  if (idemean == TRUE){
    y <- ( y - mean(y, na.rm = TRUE))
  }

  #detrending
  if (idetrend == TRUE){
    y <- pracma::detrend(y)
  }

  #itrim% trimming each tail; quantile is a function in default stats package

  if (itrim > 0){
    ql <- stats::quantile(y, probs = itrim / 100)
    qh <- stats::quantile(y, probs = (100 - itrim) / 100)
    y <- y[y > ql & y < qh]

  }

  # Return the demeaned, detrended and trimmed vector and
  # notice the new length of the vector if trimming is true
  y
} #end preprocess function
D-Roberts/statiotest documentation built on May 6, 2019, 12:55 p.m.