Skip to content

Commit b575c34

Browse files
author
‘topepo’
committed
update docs
1 parent 926d587 commit b575c34

File tree

6 files changed

+85
-45
lines changed

6 files changed

+85
-45
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ importFrom(stats,as.formula)
414414
importFrom(stats,binomial)
415415
importFrom(stats,coef)
416416
importFrom(stats,delete.response)
417+
importFrom(stats,median)
417418
importFrom(stats,model.frame)
418419
importFrom(stats,model.matrix)
419420
importFrom(stats,model.offset)

R/aaa_quantiles.R

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,28 +45,42 @@ new_quantile_pred <- function(values = list(), quantile_levels = double()) {
4545
)
4646
}
4747

48-
4948
#' Create a vector containing sets of quantiles
5049
#'
50+
#' [quantile_pred()] is a special vector class used to efficiently store
51+
#' predictions from a quantile regression model. It requires the same quantile
52+
#' levels for each row being predicted.
53+
#'
5154
#' @param values A matrix of values. Each column should correspond to one of
5255
#' the quantile levels.
5356
#' @param quantile_levels A vector of probabilities corresponding to `values`.
57+
#' @param x An object produced by [quantile_pred()].
58+
#' @param .rows,.name_repair,rownames Arguments not used but required by the
59+
#' original S3 method.
60+
#' @param ... Not currently used.
5461
#'
5562
#' @export
56-
#' @return A vector of values associated with the quantile levels.
57-
#'
63+
#' @return
64+
#' * [quantile_pred()] returns a vector of values associated with the
65+
#' quantile levels.
66+
#' * [extract_quantile_levels()] returns a numeric vector of levels.
67+
#' * [as_tibble()] returns a tibble with rows `".pred_quantile"`,
68+
#' `".quantile_levels"`, and `".row"`.
69+
#' * [as.matrix()] returns an unnamed matrix with rows as sames, columns as
70+
#' quantile levels, and entries are predictions.
5871
#' @examples
59-
#' v <- quantile_pred(matrix(rnorm(20), 5), c(.2, .4, .6, .8))
72+
#' .pred_quantile <- quantile_pred(matrix(rnorm(20), 5), c(.2, .4, .6, .8))
73+
#'
74+
#' unclass(.pred_quantile)
6075
#'
6176
#' # Access the underlying information
62-
#' attr(v, "quantile_levels")
63-
#' unclass(v)
77+
#' extract_quantile_levels(.pred_quantile)
6478
#'
65-
#' # tidy format
66-
#' as_tibble(v)
79+
#' # Matrix format
80+
#' as.matrix(.pred_quantile)
6781
#'
68-
#' # matrix format
69-
#' as.matrix(v)
82+
#' # Tidy format
83+
#' tibble::as_tibble(.pred_quantile)
7084
quantile_pred <- function(values, quantile_levels = double()) {
7185
check_quantile_pred_inputs(values, quantile_levels)
7286
quantile_levels <- vctrs::vec_cast(quantile_levels, double())
@@ -170,6 +184,16 @@ restructure_rq_pred <- function(x, object) {
170184
}
171185

172186
#' @export
187+
#' @rdname quantile_pred
188+
extract_quantile_levels <- function(x) {
189+
if ( !inherits(x, "quantile_pred") ) {
190+
cli::cli_abort("{.arg x} should have class {.val quantile_pred}.")
191+
}
192+
attr(x, "quantile_levels")
193+
}
194+
195+
#' @export
196+
#' @rdname quantile_pred
173197
as_tibble.quantile_pred <-
174198
function (x, ..., .rows = NULL, .name_repair = "minimal", rownames = NULL) {
175199
lvls <- attr(x, "quantile_levels")
@@ -183,16 +207,8 @@ as_tibble.quantile_pred <-
183207
}
184208

185209
#' @export
210+
#' @rdname quantile_pred
186211
as.matrix.quantile_pred <- function(x, ...) {
187212
num_samp <- length(x)
188213
matrix(unlist(x), nrow = num_samp)
189214
}
190-
191-
#' @export
192-
#' @rdname quantile_pred
193-
extract_quantile_levels <- function(x) {
194-
if ( !inherits(x, "quantile_pred") ) {
195-
cli::cli_abort("{.arg x} should have class {.val quantile_pred}.")
196-
}
197-
attr(x, "quantile_levels")
198-
}

R/parsnip-package.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#' @importFrom stats .checkMFClasses .getXlevels as.formula binomial coef
2222
#' @importFrom stats delete.response model.frame model.matrix model.offset
2323
#' @importFrom stats model.response model.weights na.omit na.pass predict qnorm
24-
#' @importFrom stats qt quantile setNames terms update
24+
#' @importFrom stats qt quantile setNames terms update median
2525
#' @importFrom tibble as_tibble is_tibble tibble
2626
#' @importFrom tidyr gather
2727
#' @importFrom utils capture.output getFromNamespace globalVariables head

man/quantile_pred.Rd

Lines changed: 33 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/helper-objects.R

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,16 @@ is_tf_ok <- function() {
2424
}
2525
res
2626
}
27+
28+
# ------------------------------------------------------------------------------
29+
# for quantile regression tests
30+
31+
data("Sacramento")
32+
33+
Sacramento_small <-
34+
Sacramento %>%
35+
dplyr::mutate(price = log10(price)) %>%
36+
dplyr::select(price, beds, baths, sqft, latitude, longitude)
37+
38+
sac_train <- Sacramento_small[-(1:5), ]
39+
sac_test <- Sacramento_small[ 1:5 , ]

tests/testthat/test-linear_reg_quantreg.R

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
test_that('linear quantile regression via quantreg - single quantile', {
22
skip_if_not_installed("quantreg")
33

4-
data("Sacramento")
5-
6-
Sacramento_small <-
7-
Sacramento %>%
8-
dplyr::select(price, beds, baths, sqft, latitude, longitude)
9-
10-
sac_train <- Sacramento_small[-(1:5), ]
11-
sac_test <- Sacramento_small[ 1:5 , ]
4+
# data in `helper-objects.R`
125

136
one_quant <-
147
linear_reg() %>%
@@ -60,14 +53,7 @@ test_that('linear quantile regression via quantreg - single quantile', {
6053
test_that('linear quantile regression via quantreg - multiple quantiles', {
6154
skip_if_not_installed("quantreg")
6255

63-
data("Sacramento")
64-
65-
Sacramento_small <-
66-
Sacramento %>%
67-
dplyr::select(price, beds, baths, sqft, latitude, longitude)
68-
69-
sac_train <- Sacramento_small[-(1:5), ]
70-
sac_test <- Sacramento_small[ 1:5 , ]
56+
# data in `helper-objects.R`
7157

7258
ten_quant <-
7359
linear_reg() %>%

0 commit comments

Comments
 (0)