diff --git a/R/transform_log.R b/R/transform_log.R new file mode 100644 index 0000000..f9ebaf7 --- /dev/null +++ b/R/transform_log.R @@ -0,0 +1,20 @@ +#' transform_log +#' +#' log-transformation of a numeric vector. For details about log-transformation please see you basic school math textbook. +#' @param x a numeric vector +#' @return log-transformed vector \code{x} +#' @examples +#' example_vector=c(1,2,3,4,5,6,7,8,9,10) +#' transform_log(example_vector) +#' @export + +transform_log <- function(x){ + + if( is.null(x) ) stop("Input vector is not allowed to be NULL.") + if( any(is.na(x)) ) stop("There is at least one NA value in input vector.") + if( any(x <= 0) ) stop("There is at least one negative value.") + if( any(is.numeric(x) == FALSE) ) stop("There is at least one non-numeric value.") + y<-log(x) + return(y) + +} \ No newline at end of file diff --git a/R/windsorize.R b/R/windsorize.R index b4e15e6..279890f 100644 --- a/R/windsorize.R +++ b/R/windsorize.R @@ -1,10 +1,36 @@ -#' Windsorize -#' -#' Do some windsorization. -#' @export -windsorize <- function(x, p = .90) { - q <- quantile(x, p) - x[x >= q] <- q - x -} - +#' Windsorize +#' +#' Its purposes is to eliminate outliers in a following way. Values of (0.5 +- p/2)th quantiles are calculated and all +#' values above(below) those quantiles are replaced by the quantiles. +#' @param x a numeric vector +#' @param p quantile +#' @return Windsorized vector \code{x} +#' @examples +#' example_vector=c(-1000,1,2,3,4,5,6,7,8,9,1000) +#' windsorize(example_vector, 0.9) +#' +#' example_vector=rnorm(100) +#' windsorize(example_vector, 0.9) +#' @export +#' @import stats + +windsorize <- function(x, p = .90) { + + if(is.null(x)) {stop("Input vector cannot be NULL.")} + if(any(is.na(x))) {stop("There should be no NA's in input vector.")} + if(all(is.numeric(x)==FALSE)) {stop("There should only numeric values in the input vector.")} + + if(is.na(p)==TRUE) {stop("Input quantile should be a number between 0 and 1 ")} + if(is.numeric(p)==FALSE) {stop("Input quantile should be a number between 0 and 1 ")} + if(p > 1) {stop("Input quantile should be a number between 0 and 1 ")} + if(p < 0) {stop("Input quantile should be a number between 0 and 1 ")} + + + q_u <- quantile(x, 0.5 + p/2) + x[x >= q_u] <- q_u + + q_l <- quantile(x, 0.5 - p/2) + x[x <= q_l] <- q_l + + return(x) +}