1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
#' (Signed) rank transformation
#'
#' Transform numeric values with the integers of their rank (i.e., 1st smallest,
#' 2nd smallest, 3rd smallest, etc.). Setting the `sign` argument to `TRUE` will
#' give you signed ranks, where the ranking is done according to absolute size
#' but where the sign is preserved (i.e., 2, 1, -3, 4).
#'
#' @param x Object.
#' @param sign Logical, if `TRUE`, return signed ranks.
#' @param method Treatment of ties. Can be one of `"average"` (default),
#' `"first"`, `"last"`, `"random"`, `"max"` or `"min"`. See [rank()] for
#' details.
#' @param zeros How to handle zeros. If `"na"` (default), they are marked as
#' `NA`. If `"signrank"`, they are kept during the ranking and marked as zeros.
#' This is only used when `sign = TRUE`.
#' @param ... Arguments passed to or from other methods.
#' @inheritParams extract_column_names
#' @inheritParams standardize.data.frame
#'
#' @inheritSection center Selection of variables - the `select` argument
#'
#' @examples
#' ranktransform(c(0, 1, 5, -5, -2))
#'
#' # By default, zeros are converted to NA
#' suppressWarnings(
#' ranktransform(c(0, 1, 5, -5, -2), sign = TRUE)
#' )
#' ranktransform(c(0, 1, 5, -5, -2), sign = TRUE, zeros = "signrank")
#'
#' head(ranktransform(trees))
#' @return A rank-transformed object.
#'
#' @family transform utilities
#'
#' @export
ranktransform <- function(x, ...) {
UseMethod("ranktransform")
}
#' @rdname ranktransform
#' @export
ranktransform.numeric <- function(x,
sign = FALSE,
method = "average",
zeros = "na",
verbose = TRUE,
...) {
# no change if all values are `NA`s
if (all(is.na(x))) {
return(x)
}
zeros <- match.arg(zeros, c("na", "signrank"))
method <- match.arg(
method,
c("average", "first", "last", "random", "max", "min")
)
# Warning if only one value and return early
if (insight::has_single_value(x)) {
if (is.null(names(x))) {
name <- deparse(substitute(x))
} else {
name <- names(x)
}
if (verbose) {
insight::format_warning(
paste0(
"Variable `",
name,
"` contains only one unique value and will not be normalized."
)
)
}
return(x)
}
# Warning if only two values present but don't return early
if (length(unique(x)) == 2L) {
if (is.null(names(x))) {
name <- deparse(substitute(x))
} else {
name <- names(x)
}
if (verbose) {
# nolint
insight::format_warning(
paste0(
"Variable `",
name,
"` contains only two different values. Consider converting it to a factor."
)
)
}
}
if (sign) {
if (zeros == "na") {
out <- rep(NA, length(x))
ZEROES <- x == 0
if (any(ZEROES) && verbose) {
insight::format_warning("Zeros detected. These cannot be sign-rank transformed.") # nolint
}
out[!ZEROES] <- sign(x[!ZEROES]) * rank(abs(x[!ZEROES]),
ties.method = method,
na.last = "keep"
)
} else if (zeros == "signrank") {
out <- sign(x) * rank(abs(x), ties.method = method, na.last = "keep")
}
} else {
out <- rank(x, ties.method = method, na.last = "keep")
}
out
}
#' @export
ranktransform.factor <- function(x, ...) {
x
}
#' @export
ranktransform.grouped_df <- function(x,
select = NULL,
exclude = NULL,
sign = FALSE,
method = "average",
ignore_case = FALSE,
regex = FALSE,
zeros = "na",
verbose = TRUE,
...) {
info <- attributes(x)
grps <- attr(x, "groups", exact = TRUE)[[".rows"]]
# evaluate arguments
select <- .select_nse(select,
x,
exclude,
ignore_case,
regex = regex,
verbose = verbose
)
x <- as.data.frame(x)
for (rows in grps) {
x[rows, ] <- ranktransform(
x[rows, , drop = FALSE],
select = select,
exclude = exclude,
sign = sign,
method = method,
...
)
}
# set back class, so data frame still works with dplyr
attributes(x) <- info
x
}
#' @rdname ranktransform
#' @export
ranktransform.data.frame <- function(x,
select = NULL,
exclude = NULL,
sign = FALSE,
method = "average",
ignore_case = FALSE,
regex = FALSE,
zeros = "na",
verbose = TRUE,
...) {
# evaluate arguments
select <- .select_nse(select,
x,
exclude,
ignore_case,
regex = regex,
verbose = verbose
)
x[select] <- lapply(x[select], ranktransform, sign = sign, method = method)
x
}
|