R/Stationarize.R

Defines functions Stationarize

Documented in Stationarize

#' Stationarize your dataset
#'
#' Stationarize your dataset with your chosen methodology and print the number of stationarized variables after stationarizing
#' @param x A dataset
#' @param Variables The colnames of your selected variables, put names(x) to take everything
#' @param Method A character specifying the methodology chosen from c("Difference", "Division", "Log", "Square")
#' @return A dataframe containing the new variables after the process
#' @export

Stationarize <- function(x, Variables, Method){

  tolower("BABA")

  Work <- dplyr::select(x, Variables)

  if( tolower(Method) == "difference"){
    Work <- data.frame(apply(Work, 2, diff))
    Success <- length(Variables) - nrow(Stationarity_Check(Work, k = 1)$Not_Stationary)
    message <- paste("You have", Success, "variables stationarized")
    zz <- readline(paste(message, "\n(press enter to continue)"))
    return(data.frame(Work))
  }

  if( tolower(Method) == "division"){
    Under <- apply(Work, 2, lag)
    return <- Work/Under
    Work <- return[-1,]
    Success <- length(Variables) - nrow(Stationarity_Check(Work, k = 1)$Not_Stationary)
    message <- paste("You have", Success, "variables stationarized")
    zz <- readline(paste(message, "\n(press enter to continue)"))
    return(data.frame(Work))
  }

  if( tolower(Method) == "log"){
    if(sum(Work <= 0) >0){print("Error log(): Negative values or zeros in the dataset")
      stop()}
    Work <- log(Work)
    Success <- length(Variables) - nrow(Stationarity_Check(Work, k = 1)$Not_Stationary)
    message <- paste("You have", Success, "variables stationarized")
    zz <- readline(paste(message, "\n(press enter to continue)"))
    return(data.frame(Work))
  }

  if( tolower(Method) == "square"){
    Work <- Work^2
    Success <- length(Variables) - nrow(Stationarity_Check(Work, k = 1)$Not_Stationary)
    message <- paste("You have", Success, "variables stationarized")
    zz <- readline(paste(message, "\n(press enter to continue)"))
    return(data.frame(Work))
  }

}
samchaineau/ModBrowse documentation built on Feb. 17, 2020, 1:57 p.m.