forked from quantargo/datacleaner
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
287054e
commit d73ed84
Showing
2 changed files
with
56 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
d73ed84
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
quantargo#19 - windsorize() - modification for treatment of outliers with low numbers
quantargo#20 - windsorize() - added treatment of NA and NULL
quantargo#22 - transform_log() - added new function for log-transformation