-
-
Notifications
You must be signed in to change notification settings - Fork 18
Winsorize based on the MAD #179
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3cb5a12
addresses #177 & #49 & #47 for winsorizing based on the MAD
rempsyc 807b8fe
forgot to push updated documentation
rempsyc 0a7cc4e
new argument "method", updated NEWS, resolved failed test, #179
rempsyc 658b2b4
update winsorize.numeric
mattansb c626eca
minor modifications to docs
rempsyc d45ca3f
removed tidyr from Suggests, replaced `tidyr::pivot_longer` with `dat…
rempsyc 03f85bf
added new tests for new winsorization methods, insight::format_messag…
rempsyc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -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 | ||
|
|
@@ -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) { | ||
|
|
||
| if (threshold <= 0) { | ||
| if (isTRUE(verbose)) { | ||
| warning("'threshold' for winsorization must be a scalar greater than 0. Did not winsorize data.", call. = FALSE) | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See above.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| } | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.