File: percentile.R

package info (click to toggle)
r-cran-recipes 1.0.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,636 kB
  • sloc: sh: 37; makefile: 2
file content (197 lines) | stat: -rw-r--r-- 4,875 bytes parent folder | download
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
196
197
#' Percentile Transformation
#'
#' `step_percentile` creates a *specification* of a recipe step that
#' replaces the value of a variable with its percentile from the training set.
#'
#' @inheritParams step_pca
#' @inheritParams step_center
#' @param ref_dist The computed percentiles is stored here once this
#'  preprocessing step has be trained by [prep()].
#' @param options A named list of options to pass to [stats::quantile()].
#'   See Details for more information.
#' @template step-return
#' @family individual transformation steps
#' @export
#' @rdname step_percentile
#'
#' @template case-weights-unsupervised
#'
#' @examplesIf rlang::is_installed("modeldata")
#' data(biomass, package = "modeldata")
#'
#' biomass_tr <- biomass[biomass$dataset == "Training", ]
#' biomass_te <- biomass[biomass$dataset == "Testing", ]
#'
#' rec <- recipe(
#'   HHV ~ carbon + hydrogen + oxygen + nitrogen + sulfur,
#'   data = biomass_tr
#' ) %>%
#'   step_percentile(carbon)
#'
#' prepped_rec <- prep(rec)
#'
#' prepped_rec %>%
#'   bake(biomass_te)
#'
#' tidy(rec, 1)
#' tidy(prepped_rec, 1)
step_percentile <-
  function(recipe,
           ...,
           role = NA,
           trained = FALSE,
           ref_dist = NULL,
           options = list(probs = (0:100) / 100),
           skip = FALSE,
           id = rand_id("percentile")) {
    add_step(
      recipe,
      step_percentile_new(
        terms = enquos(...),
        trained = trained,
        role = role,
        ref_dist = ref_dist,
        options = options,
        skip = skip,
        id = id,
        case_weights = NULL
      )
    )
  }

step_percentile_new <-
  function(terms, role, trained, ref_dist, options, skip, id, case_weights) {
    step(
      subclass = "percentile",
      terms = terms,
      role = role,
      trained = trained,
      ref_dist = ref_dist,
      options = options,
      skip = skip,
      id = id,
      case_weights = case_weights
    )
  }

#' @export
prep.step_percentile <- function(x, training, info = NULL, ...) {
  col_names <- recipes_eval_select(x$terms, training, info)
  check_type(training[, col_names], types = c("double", "integer"))

  wts <- get_case_weights(info, training)
  were_weights_used <- are_weights_used(wts, unsupervised = TRUE)
  if (isFALSE(were_weights_used)) {
    wts <- NULL
  }

  ## We'll use the names later so make sure they are available
  x$options$names <- TRUE

  if (!any(names(x$options) == "probs")) {
    x$options$probs <- (0:100) / 100
  } else {
    x$options$probs <- sort(unique(x$options$probs))
  }

  ref_dist <- purrr::map(
    training[, col_names],
    get_train_pctl,
    wts = wts,
    args = x$options
  )

  step_percentile_new(
    terms = x$terms,
    trained = TRUE,
    role = x$role,
    ref_dist = ref_dist,
    options = x$options,
    skip = x$skip,
    id = x$id,
    case_weights = were_weights_used
  )
}

get_train_pctl <- function(x, wts, args = NULL) {
  if (is.null(wts)) {
    res <- rlang::exec("quantile", x = x, !!!args)
  } else {
    wts <- as.double(wts)
    res <- rlang::exec("wrighted_quantile", x = x, wts = wts, !!!args)
  }

  # Remove duplicate percentile values
  res[!duplicated(res)]
}

wrighted_quantile <- function(x, wts, probs, ...) {
  order_x <- order(x)
  x <- x[order_x]
  wts <- wts[order_x]

  wts_norm <- cumsum(wts) / sum(wts)
  res <- purrr::map_dbl(probs, ~x[min(which(wts_norm >= .x))])

  names(res) <- paste0(probs * 100, "%")
  res
}

#' @export
bake.step_percentile <- function(object, new_data, ...) {
  vars <- names(object$ref_dist)
  check_new_data(vars, object, new_data)

  new_data[, vars] <-
    purrr::map2_dfc(new_data[, vars], object$ref_dist, pctl_by_approx)

  new_data
}

pctl_by_approx <- function(x, ref) {
  # In case duplicates were removed, get the percentiles from
  # the names of the reference object
  grid <- as.numeric(gsub("%$", "", names(ref)))
  stats::approx(x = ref, y = grid, xout = x)$y / 100
}

print.step_percentile <-
  function(x, width = max(20, options()$width - 35), ...) {
    title <- "Percentile transformation on "
    print_step(names(x$ref_dist), x$terms, x$trained, title, width,
               case_weights = x$case_weights)
    invisible(x)
  }

#' @rdname tidy.recipe
#' @export
tidy.step_percentile <- function(x, ...) {
  if (is_trained(x)) {
    if (length(x$ref_dist) == 0) {
      res <- tibble(
        terms = character(),
        value = numeric(),
        percentile = numeric()
      )
    } else {
      res <- map_dfr(x$ref_dist, format_pctl, .id = "term")
    }
  } else {
    term_names <- sel2char(x$terms)
    res <-
      tibble(
        terms = term_names,
        value = rlang::na_dbl,
        percentile = rlang::na_dbl
      )
  }
  res$id <- x$id
  res
}

format_pctl <- function(x) {
  tibble::tibble(
    value = unname(x),
    percentile = as.numeric(gsub("%$", "", names(x)))
  )
}