Skip to content

Commit b7b4416

Browse files
dajmcdondsweber2
authored andcommitted
tests pass, checks don't
1 parent 7278c69 commit b7b4416

21 files changed

+829
-1066
lines changed

NAMESPACE

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ S3method(vec_arith,quantile_pred)
119119
S3method(vec_arith.numeric,quantile_pred)
120120
S3method(vec_arith.quantile_pred,numeric)
121121
S3method(vec_math,quantile_pred)
122+
S3method(vec_proxy_equal,quantile_pred)
122123
S3method(weighted_interval_score,quantile_pred)
123124
export("%>%")
124125
export(Add_model)
@@ -137,7 +138,6 @@ export(arx_class_epi_workflow)
137138
export(arx_classifier)
138139
export(arx_fcast_epi_workflow)
139140
export(arx_forecaster)
140-
export(as_epi_df)
141141
export(as_tibble)
142142
export(autoplot)
143143
export(bake)
@@ -149,12 +149,14 @@ export(climate_args_list)
149149
export(climatological_forecaster)
150150
export(default_epi_recipe_blueprint)
151151
export(detect_layer)
152+
export(dist_quantiles)
152153
export(epi_recipe)
153154
export(epi_recipe_blueprint)
154155
export(epi_workflow)
155156
export(extract_argument)
156157
export(extract_frosting)
157158
export(extract_layers)
159+
export(extract_quantile_levels)
158160
export(extrapolate_quantiles)
159161
export(filter)
160162
export(fit)
@@ -182,13 +184,15 @@ export(layer_quantile_distn)
182184
export(layer_residual_quantiles)
183185
export(layer_threshold)
184186
export(layer_unnest)
187+
export(nested_quantiles)
185188
export(new_default_epi_recipe_blueprint)
186189
export(new_epi_recipe_blueprint)
187190
export(pivot_longer)
188191
export(pivot_quantiles_longer)
189192
export(pivot_quantiles_wider)
190193
export(pivot_wider)
191194
export(prep)
195+
export(quantile_pred)
192196
export(quantile_reg)
193197
export(rand_id)
194198
export(remove_epi_recipe)
@@ -276,6 +280,7 @@ importFrom(ggplot2,geom_linerange)
276280
importFrom(ggplot2,geom_point)
277281
importFrom(ggplot2,geom_ribbon)
278282
importFrom(glue,glue)
283+
importFrom(hardhat,extract_quantile_levels)
279284
importFrom(hardhat,extract_recipe)
280285
importFrom(hardhat,quantile_pred)
281286
importFrom(hardhat,refresh_blueprint)
@@ -284,6 +289,7 @@ importFrom(lubridate,"%m-%")
284289
importFrom(lubridate,leap_year)
285290
importFrom(lubridate,month)
286291
importFrom(lubridate,yday)
292+
importFrom(lifecycle,deprecated)
287293
importFrom(magrittr,"%>%")
288294
importFrom(magrittr,extract2)
289295
importFrom(recipes,bake)
@@ -330,8 +336,12 @@ importFrom(tidyr,pivot_wider)
330336
importFrom(tidyr,unnest)
331337
importFrom(tidyselect,all_of)
332338
importFrom(utils,capture.output)
339+
importFrom(vctrs,as_list_of)
340+
importFrom(vctrs,new_vctr)
333341
importFrom(vctrs,vec_arith)
334342
importFrom(vctrs,vec_arith.numeric)
335343
importFrom(vctrs,vec_cast)
336344
importFrom(vctrs,vec_math)
345+
importFrom(vctrs,vec_proxy_equal)
346+
importFrom(vctrs,vec_recycle_common)
337347
importFrom(workflows,extract_preprocessor)

R/epipredict-package.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#' @importFrom dplyr inner_join
1111
#' @importFrom dplyr summarize filter mutate select left_join rename ungroup
1212
#' @importFrom epiprocess growth_rate growth_rate_params is_epi_df
13+
#' @importFrom lifecycle deprecated
1314
#' @importFrom magrittr extract2
1415
#' @importFrom rlang := !! %||% as_function global_env set_names !!! caller_arg
1516
#' @importFrom rlang is_logical is_true inject enquo enquos expr sym arg_match

R/extrapolate_quantiles.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
#' @export
1919
#'
2020
#' @examples
21-
#' dstn <- quantile_dstn(rbind(1:4, 8:11), c(.2, .4, .6, .8))
21+
#' dstn <- quantile_pred(rbind(1:4, 8:11), c(.2, .4, .6, .8))
2222
#' # extra quantiles are appended
23-
#' as.tibble(extrapolate_quantiles(dstn, probs = c(.25, 0.5, .75)))
23+
#' as_tibble(extrapolate_quantiles(dstn, probs = c(.25, 0.5, .75)))
2424
extrapolate_quantiles <- function(x, probs, replace_na = TRUE, ...) {
2525
UseMethod("extrapolate_quantiles")
2626
}

R/layer_point_from_distn.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ layer_point_from_distn_new <- function(type, name, id) {
7878
slather.layer_point_from_distn <-
7979
function(object, components, workflow, new_data, ...) {
8080
dstn <- components$predictions$.pred
81-
if (!inherits(dstn, "distribution")) {
82-
rlang::warn(
81+
if (!(inherits(dstn, "quantile_pred") | inherits(dstn, "distribution"))) {
82+
cli_warn(
8383
c("`layer_point_from_distn` requires distributional predictions.",
84-
i = "These are of class {class(dstn)}. Ignoring this layer."
84+
i = "These are of class {.cls {class(dstn)}}. Ignoring this layer."
8585
)
8686
)
8787
return(components)

R/layer_threshold_preds.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ layer_threshold_new <-
6363

6464
# restrict various objects to the interval [lower, upper]
6565
#' @export
66+
#' @keywords internal
6667
snap <- function(x, lower, upper, ...) {
6768
UseMethod("snap")
6869
}

R/pivot_quantiles.R

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,53 @@
1+
#' Turn a vector of quantile distributions into a list-col
2+
#'
3+
#' `r lifecycle::badge("deprecated")`
4+
#'
5+
#' This function is deprecated. The recommended alternative is
6+
#' [hardhat::quantile_pred()] with [tibble::as_tibble()]
7+
8+
#'
9+
#' @param x a `distribution` containing `dist_quantiles`
10+
#'
11+
#' @return a list-col
12+
#' @export
13+
#'
14+
#' @examples
15+
#' .pred_quantile <- quantile_pred(matrix(rnorm(20), 5), c(.2, .4, .6, .8))
16+
#' nested_quantiles(.pred_quantile)
17+
#'
18+
#' .pred_quantile %>%
19+
#' as_tibble() %>%
20+
#' tidyr::nest(.by = .row) %>%
21+
#' dplyr::select(-.row)
22+
#'
23+
nested_quantiles <- function(x) {
24+
lifecycle::deprecate_warn("0.1.11", "nested_quantiles()", "hardhat::quantile_pred()")
25+
if (inherits(x, "distribution")) {
26+
if (requireNamespace("distributional")) {
27+
x <- vctrs::vec_data(x)
28+
return(distributional:::dist_apply(x, .f = function(z) {
29+
as_tibble(vctrs::vec_data(z)) %>%
30+
mutate(across(everything(), as.double)) %>%
31+
vctrs::list_of()
32+
}))
33+
} else {
34+
cli_abort(c(
35+
"`nested_quantiles()` is deprecated and the {.pkg distributional}",
36+
`!` = "package is not installed.",
37+
i = "See {.fn hardhat::quantile_pred}."
38+
))
39+
}
40+
}
41+
if (inherits(x, "quantile_pred")) {
42+
return(x %>% as_tibble() %>% tidyr::nest(.by = .row) %>%
43+
dplyr::select(data))
44+
}
45+
cli_abort(c(
46+
"`nested_quantiles()` is deprecated. See {.fn hardhat::quantile_pred}."
47+
))
48+
}
49+
50+
151
#' Pivot a column containing `quantile_pred` longer
252
#'
353
#' A column that contains `quantile_pred` will be "lengthened" with

R/quantile_pred-methods.R

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,40 @@
1+
#' A distribution parameterized by a set of quantiles
2+
#'
3+
#' `r lifecycle::badge("deprecated")`
4+
#'
5+
#' This function is deprecated. The recommended alternative is
6+
#' [hardhat::quantile_pred()].
7+
#'
8+
#' @param values A vector (or list of vectors) of values.
9+
#' @param quantile_levels A vector (or list of vectors) of probabilities
10+
#' corresponding to `values`.
11+
#'
12+
#' When creating multiple sets of `values`/`quantile_levels` resulting in
13+
#' different distributions, the sizes must match. See the examples below.
14+
#'
15+
#' @return A vector of class `"distribution"`.
16+
#'
17+
#' @export
18+
#' @keywords internal
19+
#'
20+
#' @importFrom vctrs as_list_of vec_recycle_common new_vctr
21+
dist_quantiles <- function(values, quantile_levels) {
22+
lifecycle::deprecate_warn("0.1.11", "dist_quantiles()", "hardhat::quantile_pred()")
23+
if (is.list(values)) {
24+
n <- length(values)
25+
values <- unlist(values)
26+
return(quantile_pred(matrix(values, nrow = n, byrow = TRUE), quantile_levels))
27+
} else if (is.matrix(values)) {
28+
return(quantile_pred(values, quantile_levels))
29+
} else if (is.vector(values)) {
30+
return(quantile_pred(matrix(values, nrow = 1), quantile_levels))
31+
}
32+
cli_abort(c(
33+
"`dist_quantiles()` is deprecated and the format of `values` could not",
34+
`!` = "be automatically converted to work with the replacement.",
35+
i = "See {.fn hardhat::quantile_pred}."
36+
))
37+
}
138

239
# placeholder to avoid errors, but not ideal
340
#' @importFrom hardhat quantile_pred
@@ -6,6 +43,74 @@ mean.quantile_pred <- function(x, na.rm = FALSE, ...) {
643
median(x, ...)
744
}
845

46+
# These next 3 functions should probably be added via PR to {hardhat}
47+
# Only the third is actually *needed* at the moment.
48+
# The second doesn't work correctly (not sure why), but leaving here for the
49+
# future.
50+
#
51+
# We only export the third.
52+
#
53+
# self-self method, should work only if attr(quantile_levels) are compatible
54+
# #' @importFrom vctrs vec_ptype2 vec_cast
55+
# #' @importFrom hardhat extract_quantile_levels
56+
# #' @export
57+
# #' @keywords internal
58+
# vec_ptype2.quantile_pred.quantile_pred <- function(
59+
# x, y, ..., x_arg = "", y_arg = "", call = caller_env()
60+
# ) {
61+
# if (all(extract_quantile_levels(y) %in% extract_quantile_levels(x))) {
62+
# return(x)
63+
# }
64+
# if (all(extract_quantile_levels(x) %in% extract_quantile_levels(y))) {
65+
# return(y)
66+
# }
67+
# vctrs::stop_incompatible_type(
68+
# x, y, x_arg = x_arg, y_arg = y_arg,
69+
# details = "`quantile_levels` must be compatible (a superset/subset relation)."
70+
# )
71+
# }
72+
73+
# currently doesn't work
74+
# #' @export
75+
# vec_cast.quantile_pred.quantile_pred <- function(
76+
# x, to, ..., x_arg = caller_arg(x), to_arg = caller_arg(to),
77+
# call = caller_env()
78+
# ) {
79+
# to_ql <- extract_quantile_levels(to)
80+
# x_ql <- extract_quantile_levels(x)
81+
# x_in_to <- x_ql %in% to_ql
82+
# to_in_x <- to_ql %in% x_ql
83+
# if (all(x_in_to)) {
84+
# mat <- matrix(NA, ncol = length(to_ql))
85+
# mat[ , to_in_x] <- c(as.matrix(x))
86+
# } else if (all(to_in_x)) {
87+
# mat <- as.matrix(x)[ , x_in_to, drop = FALSE]
88+
# } else {
89+
# vctrs::stop_incompatible_type(
90+
# x, to, x_arg = x_arg, y_arg = to_arg,
91+
# details = "`quantile_levels` must be compatible (a superset/subset relation)."
92+
# )
93+
# }
94+
# quantile_pred(mat, to_ql)
95+
# }
96+
97+
98+
# Convert the quantile_pred to a data frame (named with the .quantile_levels)
99+
# This powers vec_proxy_equal (and hence ==, !=, is.na, etc)
100+
# It also powers vec_proxy_compare, so, given matching colnames, these should
101+
# work out of the box.
102+
#
103+
#' @importFrom vctrs vec_proxy_equal
104+
#' @export
105+
vec_proxy_equal.quantile_pred <- function(x, ...) {
106+
as_tibble(x) %>%
107+
tidyr::pivot_wider(
108+
names_from = .quantile_levels,
109+
values_from = .pred_quantile
110+
) %>%
111+
dplyr::select(-.row)
112+
}
113+
9114

10115
# quantiles by treating quantile_pred like a distribution -----------------
11116

R/reexports-tidymodels.R

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#' @importFrom generics fit
2+
#' @export
3+
generics::fit
4+
5+
#' @importFrom generics forecast
6+
#' @export
7+
generics::forecast
8+
9+
#' @importFrom recipes prep
10+
#' @export
11+
recipes::prep
12+
13+
#' @importFrom recipes bake
14+
#' @export
15+
recipes::bake
16+
17+
#' @importFrom recipes rand_id
18+
#' @export
19+
recipes::rand_id
20+
21+
#' @importFrom tibble tibble as_tibble
22+
#' @export
23+
tibble::tibble
24+
25+
#' @export
26+
tibble::as_tibble
27+
28+
#' @importFrom generics tidy
29+
#' @export
30+
generics::tidy
31+
32+
#' @importFrom hardhat quantile_pred extract_quantile_levels
33+
#' @export
34+
hardhat::quantile_pred
35+
36+
#' @export
37+
hardhat::extract_quantile_levels

R/utils-misc.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ format_varnames <- function(x, empty = "*none*") {
4747
if (length(x) == 0L) {
4848
empty
4949
} else {
50-
as.character(syms(x))
50+
as.character(rlang::syms(x))
5151
}
5252
}
5353

man/dist_quantiles.Rd

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

man/extrapolate_quantiles.Rd

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

0 commit comments

Comments
 (0)