2
2
# '
3
3
# ' @param frosting a `frosting` postprocessor
4
4
# ' @param ... Unused, include for consistency with other layers.
5
- # ' @param probs numeric vector of probabilities with values in (0,1) referring to the desired quantile.
6
- # ' @param symmetrize logical. If `TRUE` then interval will be symmetrical.
5
+ # ' @param probs numeric vector of probabilities with values in (0,1)
6
+ # ' referring to the desired quantile.
7
+ # ' @param symmetrize logical. If `TRUE` then interval will be symmetric.
7
8
# ' @param .flag a logical to determine if the layer is added. Passed on to
8
9
# ' `add_layer()`. Default `TRUE`.
9
10
# ' @param id a random id string
10
11
# '
11
- # ' @return an updated `frosting` postprocessor with additional columns of the residual quantiles added to the prediction
12
+ # ' @return an updated `frosting` postprocessor with additional columns of the
13
+ # ' residual quantiles added to the prediction
12
14
# ' @export
13
15
# ' @examples
14
- # ' jhu <- case_death_rate_subset %>%
16
+ # ' jhu <- case_death_rate_subset %>%
15
17
# ' dplyr::filter(time_value > "2021-11-01", geo_value %in% c("ak", "ca", "ny"))
16
18
# '
17
19
# ' r <- epi_recipe(jhu) %>%
18
20
# ' step_epi_lag(death_rate, lag = c(0, 7, 14)) %>%
19
21
# ' step_epi_ahead(death_rate, ahead = 7) %>%
20
- # ' recipes::step_naomit(recipes::all_predictors()) %>%
21
- # ' recipes::step_naomit(recipes::all_outcomes(), skip = TRUE)
22
+ # ' step_epi_naomit()
22
23
# '
23
24
# ' wf <- epi_workflow(r, parsnip::linear_reg()) %>%
24
25
# ' parsnip::fit(jhu)
25
26
# '
26
27
# ' latest <- get_test_data(recipe = r, x = jhu)
27
28
# '
28
- # ' f <- epipredict::: frosting() %>%
29
- # ' layer_predict() %>%
30
- # ' layer_residual_quantile (probs = c(0.0275, 0.975), symmetrize = FALSE) %>%
31
- # ' layer_naomit(.pred)
32
- # ' wf1 <- wf %>% epipredict::: add_frosting(f)
29
+ # ' f <- frosting() %>%
30
+ # ' layer_predict() %>%
31
+ # ' layer_residual_quantiles (probs = c(0.0275, 0.975), symmetrize = FALSE) %>%
32
+ # ' layer_naomit(.pred)
33
+ # ' wf1 <- wf %>% add_frosting(f)
33
34
# '
34
35
# ' p <- predict(wf1, latest)
35
36
# ' p
36
- layer_residual_quantile <- function (frosting , ... ,
37
+ layer_residual_quantiles <- function (frosting , ... ,
37
38
probs = c(0.0275 , 0.975 ),
38
39
symmetrize = TRUE ,
39
40
.flag = TRUE ,
40
- id = rand_id(" residual_quantile " )) {
41
+ id = rand_id(" residual_quantiles " )) {
41
42
rlang :: check_dots_empty()
42
43
add_layer(
43
44
frosting ,
44
- layer_residual_quantile_new (
45
+ layer_residual_quantiles_new (
45
46
probs = probs ,
46
47
symmetrize = symmetrize ,
47
48
id = id
@@ -50,12 +51,12 @@ layer_residual_quantile <- function(frosting, ...,
50
51
)
51
52
}
52
53
53
- layer_residual_quantile_new <- function (probs , symmetrize , id ) {
54
- layer(" residual_quantile " , probs = probs , symmetrize = symmetrize , id = id )
54
+ layer_residual_quantiles_new <- function (probs , symmetrize , id ) {
55
+ layer(" residual_quantiles " , probs = probs , symmetrize = symmetrize , id = id )
55
56
}
56
57
57
58
# ' @export
58
- slather.layer_residual_quantile <-
59
+ slather.layer_residual_quantiles <-
59
60
function (object , components , the_fit , the_recipe , ... ) {
60
61
if (is.null(object $ probs )) return (components )
61
62
@@ -64,8 +65,7 @@ slather.layer_residual_quantile <-
64
65
q <- quantile(c(r , s * r ), probs = object $ probs , na.rm = TRUE )
65
66
66
67
estimate <- components $ predictions $ .pred
67
- interval <- data.frame (outer(estimate , q , " +" ))
68
- names(interval )<- probs_to_string(object $ probs )
69
- components $ predictions <- dplyr :: bind_cols(components $ predictions ,interval )
68
+ dstn <- dist_quantiles(map(estimate , " +" , q ), object $ probs )
69
+ components $ predictions $ .quantiles <- dstn
70
70
components
71
71
}
0 commit comments