Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Winsorize based on the MAD #179

Merged
merged 7 commits into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 41 additions & 16 deletions R/winsorize.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
#' @param data Dataframe or vector.
#' @param threshold The amount of winsorization.
#' @param verbose Toggle warnings.
#' @param robust Logical, if TRUE, winsorizing is done via the median absolute deviation (MAD).
#' @param ... Currently not used.
#'
#' @examples
#' winsorize(iris$Sepal.Length, threshold = 0.2)
#' winsorize(iris$Sepal.Length, threshold = 3, robust = TRUE)
#' winsorize(iris, threshold = 0.2)
#' @inherit data_rename seealso
#' @export
Expand All @@ -43,27 +45,50 @@ winsorize.character <- winsorize.factor
winsorize.logical <- winsorize.factor

#' @export
winsorize.data.frame <- function(data, threshold = 0.2, verbose = TRUE, ...) {
out <- sapply(data, winsorize, threshold = threshold, verbose = verbose)
winsorize.data.frame <- function(data, threshold = 0.2, verbose = TRUE, robust = FALSE, ...) {
out <- sapply(data, winsorize, threshold = threshold, verbose = verbose, robust = robust)
as.data.frame(out)
}

#' @rdname winsorize
#' @export
winsorize.numeric <- function(data, threshold = 0.2, verbose = TRUE, ...) {
if (threshold < 0 || threshold > 1) {
if (isTRUE(verbose)) {
warning("'threshold' for winsorization must be a scalar between 0 and 1. Did not winsorize data.", call. = FALSE)
}
return(data)
winsorize.numeric <- function(data, threshold = 0.2, verbose = TRUE, robust = FALSE, ...) {
if(robust == FALSE) {

if (threshold < 0 || threshold > 0.5) {
if (isTRUE(verbose)) {
warning("'threshold' for winsorization must be a scalar between 0 and 0.5. Did not winsorize data.", call. = FALSE)
}
return(data)
}

y <- sort(data)
n <- length(data)
ibot <- floor(threshold * n) + 1
itop <- length(data) - ibot + 1
xbot <- y[ibot]
xtop <- y[itop]

winval <- data
winval[winval <= xbot] <- xbot
winval[winval >= xtop] <- xtop
return(winval)
}

y <- sort(data)
n <- length(data)
ibot <- floor(threshold * n) + 1
itop <- length(data) - ibot + 1
xbot <- y[ibot]
xtop <- y[itop]
winval <- ifelse(data <= xbot, xbot, data)
ifelse(winval >= xtop, xtop, winval)
if(robust == TRUE) {
mattansb marked this conversation as resolved.
Show resolved Hide resolved

if (threshold <= 0) {
if (isTRUE(verbose)) {
warning("'threshold' for winsorization must be a scalar greater than 0. Did not winsorize data.", call. = FALSE)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

return(data)
}

med <- stats::median(data, na.rm = TRUE)
y <- data - med
sc <- stats::mad(y, center = 0, na.rm = TRUE) * threshold
y[y > sc] <- sc
y[y < -sc] <- -sc
y + med
}
}
5 changes: 4 additions & 1 deletion man/winsorize.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.