Skip to content

Follow-up to #519 for layer_residual_quantiles(by_key = ) - #520

Open
JavierMtzRdz wants to merge 1 commit into
devfrom
jmr/improve-warn-pqw
Open

JavierMtzRdz wants to merge 1 commit into
devfrom
jmr/improve-warn-pqw

Conversation

@JavierMtzRdz

@JavierMtzRdz JavierMtzRdz commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Checklist

Please:

  • Make sure this PR is against "dev", not "main".
  • Request a review from one of the current epipredict main reviewers:
    dajmcdon.
  • Make sure to bump the version number in DESCRIPTION and NEWS.md.
    Always increment the patch version number (the third number), unless you are
    making a release PR from dev to main, in which case increment the minor
    version number (the second number).
  • Describe changes made in NEWS.md, making sure breaking changes
    (backwards-incompatible changes to the documented interface) are noted.
    Collect the changes under the next release number (e.g. if you are on
    0.7.2, then write your changes under the 0.8 heading).
  • Consider pinning the epiprocess version in the DESCRIPTION file if
    • You anticipate breaking changes in epiprocess soon
    • You want to co-develop features in epipredict and epiprocess

Change explanations for reviewer

  • Keys with no usable residuals are now named in the error.
  • Keys that appear at prediction time but not in training now trigger a warning that names them since their quantiles are NA.
  • The two warnings about requested keys missing from the training data are merged into one message.
  • Long cli messages are wrapped with paste(). This fixes a warning that split one sentence across two bullets.
  • The redundant grps variable is removed in favour of common.

Magic GitHub syntax to mark associated Issue(s) as resolved when this is merged into the default branch

  • Resolves #{issue number}

Reprex

Before (dev, 43e1f69)

devtools::load_all()
#> ℹ Loading epipredict
#> Loading required package: epidatasets
#> 
#> Loading required package: parsnip
#> 
#> Registered S3 methods overwritten by 'epipredict':
#>   method                          from   
#>   print.step_naomit               recipes
#>   vec_arith.quantile_pred         hardhat
#>   vec_arith.numeric.quantile_pred hardhat
#>   vec_math.quantile_pred          hardhat
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> 
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> 
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
jhu <- covid_case_death_rates %>%
  filter(time_value > "2021-11-01", geo_value %in% c("ak", "ca", "ny"))

# 1. tx has only 3 days of data, too few for a single 7-day flatline residual
tx_short <- covid_case_death_rates %>%
  filter(geo_value == "tx", time_value > max(jhu$time_value) - 3)
jhu_tx <- bind_rows(jhu, tx_short) %>% as_epi_df()
try(flatline_forecaster(jhu_tx, "death_rate", flatline_args_list(
  quantile_by_key = "geo_value")))
#> Error in slather(la, components, workflow, new_data) : 
#>   Residual quantiles could not be calculated due to missing residuals.
#> ℹ This may be due to `n_train` < `ahead` in your <epi_recipe>.

# 2. train without ak, then predict for ak
r <- epi_recipe(jhu) %>%
  step_epi_lag(death_rate, lag = c(0, 7, 14)) %>%
  step_epi_ahead(death_rate, ahead = 7) %>%
  step_epi_naomit()
f <- frosting() %>%
  layer_predict() %>%
  layer_naomit(.pred) %>%
  layer_residual_quantiles(quantile_levels = c(.1, .9), by_key = "geo_value")
wf_no_ak <- epi_workflow(r, linear_reg(), f) %>%
  fit(filter(jhu, geo_value != "ak"))
predict(wf_no_ak, get_test_data(r, jhu)) %>%
  pivot_quantiles_wider(.pred_distn) %>%
  select(geo_value, `0.1`, `0.9`)
#> Warning: Some grouping keys are not in data.frame returned by the `residuals()` method.
#> Groupings may not be correct.
#> # A tibble: 3 × 3
#>   geo_value  `0.1`  `0.9`
#> * <chr>      <dbl>  <dbl>
#> 1 ak        NA     NA    
#> 2 ca         0.103  0.204
#> 3 ny         0.374  0.474

Created on 2026-11-27 with reprex v2.1.1

After (jmr/improve-warn-pqw, e7bea84)

devtools::load_all()
#> ℹ Loading epipredict
#> Loading required package: epidatasets
#> 
#> Loading required package: parsnip
#> 
#> Registered S3 methods overwritten by 'epipredict':
#>   method                          from   
#>   print.step_naomit               recipes
#>   vec_arith.quantile_pred         hardhat
#>   vec_arith.numeric.quantile_pred hardhat
#>   vec_math.quantile_pred          hardhat
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> 
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> 
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
jhu <- covid_case_death_rates %>%
  filter(time_value > "2021-11-01", geo_value %in% c("ak", "ca", "ny"))

# 1. tx has only 3 days of data, too few for a single 7-day flatline residual
tx_short <- covid_case_death_rates %>%
  filter(geo_value == "tx", time_value > max(jhu$time_value) - 3)
jhu_tx <- bind_rows(jhu, tx_short) %>% as_epi_df()
try(flatline_forecaster(jhu_tx, "death_rate", flatline_args_list(
  quantile_by_key = "geo_value")))
#> Error in slather(la, components, workflow, new_data) : 
#>   Residual quantiles could not be calculated for some keys.
#> ✖ All training residuals are missing (`NA`) for: "tx".
#> ℹ This typically happens when a key has too few training observations for the
#>   forecast horizon.

# 2. train without ak, then predict for ak
r <- epi_recipe(jhu) %>%
  step_epi_lag(death_rate, lag = c(0, 7, 14)) %>%
  step_epi_ahead(death_rate, ahead = 7) %>%
  step_epi_naomit()
f <- frosting() %>%
  layer_predict() %>%
  layer_naomit(.pred) %>%
  layer_residual_quantiles(quantile_levels = c(.1, .9), by_key = "geo_value")
wf_no_ak <- epi_workflow(r, linear_reg(), f) %>%
  fit(filter(jhu, geo_value != "ak"))
predict(wf_no_ak, get_test_data(r, jhu)) %>%
  pivot_quantiles_wider(.pred_distn) %>%
  select(geo_value, `0.1`, `0.9`)
#> Warning: Some grouping keys are not in data.frame returned by the `residuals()` method.
#> Groupings may not be correct.
#> Warning: Prediction data contains a key not present in the training data: "ak".
#> ℹ Because this key has no training residuals, predictive distribution quantiles
#>   (`.pred_distn`) will be `NA`.
#> # A tibble: 3 × 3
#>   geo_value  `0.1`  `0.9`
#> * <chr>      <dbl>  <dbl>
#> 1 ak        NA     NA    
#> 2 ca         0.103  0.204
#> 3 ny         0.374  0.474

Created on 2026-11-27 with reprex v2.1.1

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant