Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephSpejbl authored May 5, 2019
1 parent 287054e commit d73ed84
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 10 deletions.
20 changes: 20 additions & 0 deletions R/transform_log.R
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)

}
46 changes: 36 additions & 10 deletions R/windsorize.R
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)
}

1 comment on commit d73ed84

@JosephSpejbl
Copy link
Owner Author

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

Please sign in to comment.