Skip to content

Commit

Permalink
update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
‘topepo’ committed Sep 12, 2024
1 parent 926d587 commit b575c34
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 45 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ importFrom(stats,as.formula)
importFrom(stats,binomial)
importFrom(stats,coef)
importFrom(stats,delete.response)
importFrom(stats,median)
importFrom(stats,model.frame)
importFrom(stats,model.matrix)
importFrom(stats,model.offset)
Expand Down
54 changes: 35 additions & 19 deletions R/aaa_quantiles.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,28 +45,42 @@ new_quantile_pred <- function(values = list(), quantile_levels = double()) {
)
}


#' Create a vector containing sets of quantiles
#'
#' [quantile_pred()] is a special vector class used to efficiently store
#' predictions from a quantile regression model. It requires the same quantile
#' levels for each row being predicted.
#'
#' @param values A matrix of values. Each column should correspond to one of
#' the quantile levels.
#' @param quantile_levels A vector of probabilities corresponding to `values`.
#' @param x An object produced by [quantile_pred()].
#' @param .rows,.name_repair,rownames Arguments not used but required by the
#' original S3 method.
#' @param ... Not currently used.
#'
#' @export
#' @return A vector of values associated with the quantile levels.
#'
#' @return
#' * [quantile_pred()] returns a vector of values associated with the
#' quantile levels.
#' * [extract_quantile_levels()] returns a numeric vector of levels.
#' * [as_tibble()] returns a tibble with rows `".pred_quantile"`,
#' `".quantile_levels"`, and `".row"`.
#' * [as.matrix()] returns an unnamed matrix with rows as sames, columns as
#' quantile levels, and entries are predictions.
#' @examples
#' v <- quantile_pred(matrix(rnorm(20), 5), c(.2, .4, .6, .8))
#' .pred_quantile <- quantile_pred(matrix(rnorm(20), 5), c(.2, .4, .6, .8))
#'
#' unclass(.pred_quantile)
#'
#' # Access the underlying information
#' attr(v, "quantile_levels")
#' unclass(v)
#' extract_quantile_levels(.pred_quantile)
#'
#' # tidy format
#' as_tibble(v)
#' # Matrix format
#' as.matrix(.pred_quantile)
#'
#' # matrix format
#' as.matrix(v)
#' # Tidy format
#' tibble::as_tibble(.pred_quantile)
quantile_pred <- function(values, quantile_levels = double()) {
check_quantile_pred_inputs(values, quantile_levels)
quantile_levels <- vctrs::vec_cast(quantile_levels, double())
Expand Down Expand Up @@ -170,6 +184,16 @@ restructure_rq_pred <- function(x, object) {
}

#' @export
#' @rdname quantile_pred
extract_quantile_levels <- function(x) {
if ( !inherits(x, "quantile_pred") ) {
cli::cli_abort("{.arg x} should have class {.val quantile_pred}.")
}
attr(x, "quantile_levels")
}

#' @export
#' @rdname quantile_pred
as_tibble.quantile_pred <-
function (x, ..., .rows = NULL, .name_repair = "minimal", rownames = NULL) {
lvls <- attr(x, "quantile_levels")
Expand All @@ -183,16 +207,8 @@ as_tibble.quantile_pred <-
}

#' @export
#' @rdname quantile_pred
as.matrix.quantile_pred <- function(x, ...) {
num_samp <- length(x)
matrix(unlist(x), nrow = num_samp)
}

#' @export
#' @rdname quantile_pred
extract_quantile_levels <- function(x) {
if ( !inherits(x, "quantile_pred") ) {
cli::cli_abort("{.arg x} should have class {.val quantile_pred}.")
}
attr(x, "quantile_levels")
}
2 changes: 1 addition & 1 deletion R/parsnip-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#' @importFrom stats .checkMFClasses .getXlevels as.formula binomial coef
#' @importFrom stats delete.response model.frame model.matrix model.offset
#' @importFrom stats model.response model.weights na.omit na.pass predict qnorm
#' @importFrom stats qt quantile setNames terms update
#' @importFrom stats qt quantile setNames terms update median
#' @importFrom tibble as_tibble is_tibble tibble
#' @importFrom tidyr gather
#' @importFrom utils capture.output getFromNamespace globalVariables head
Expand Down
42 changes: 33 additions & 9 deletions man/quantile_pred.Rd

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

13 changes: 13 additions & 0 deletions tests/testthat/helper-objects.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,16 @@ is_tf_ok <- function() {
}
res
}

# ------------------------------------------------------------------------------
# for quantile regression tests

data("Sacramento")

Sacramento_small <-
Sacramento %>%
dplyr::mutate(price = log10(price)) %>%
dplyr::select(price, beds, baths, sqft, latitude, longitude)

sac_train <- Sacramento_small[-(1:5), ]
sac_test <- Sacramento_small[ 1:5 , ]
18 changes: 2 additions & 16 deletions tests/testthat/test-linear_reg_quantreg.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
test_that('linear quantile regression via quantreg - single quantile', {
skip_if_not_installed("quantreg")

data("Sacramento")

Sacramento_small <-
Sacramento %>%
dplyr::select(price, beds, baths, sqft, latitude, longitude)

sac_train <- Sacramento_small[-(1:5), ]
sac_test <- Sacramento_small[ 1:5 , ]
# data in `helper-objects.R`

one_quant <-
linear_reg() %>%
Expand Down Expand Up @@ -60,14 +53,7 @@ test_that('linear quantile regression via quantreg - single quantile', {
test_that('linear quantile regression via quantreg - multiple quantiles', {
skip_if_not_installed("quantreg")

data("Sacramento")

Sacramento_small <-
Sacramento %>%
dplyr::select(price, beds, baths, sqft, latitude, longitude)

sac_train <- Sacramento_small[-(1:5), ]
sac_test <- Sacramento_small[ 1:5 , ]
# data in `helper-objects.R`

ten_quant <-
linear_reg() %>%
Expand Down

0 comments on commit b575c34

Please sign in to comment.