Skip to content

fix: compute layer_residual_quantiles() by_key quantiles within each key - #519

Merged
JavierMtzRdz merged 1 commit into
cmu-delphi:devfrom
chenyangsu:fix/residual-quantiles-by-key
Nov 27, 2026
Merged

JavierMtzRdz merged 1 commit into
cmu-delphi:devfrom
chenyangsu:fix/residual-quantiles-by-key

Conversation

@chenyangsu

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.
  • Describe changes made in NEWS.md, making sure breaking changes
    (backwards-incompatible changes to the documented interface) are noted.
  • 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

With flatline_args_list(quantile_by_key = "geo_value"), every location gets one pooled interval: on the test data (ak, ca, ny) the 10-90% width was 0.59 for all three, while each location's own residuals give 2.83, 0.10 and 0.10. Residuals that carry the key are joined to the training keys but never grouped. For lm fits, a key with no prediction errors with "Can't recycle x (size 3) to match y (size 2)".

The layer now groups residuals by by_key and joins the quantiles to the predictions by key. Related to #490; the default is unchanged.

Testing: two new tests in test-layer_residual_quantiles.R fail on dev and pass here; full suite 902 passed, with the same 3 environment errors as on dev.

@dajmcdon, would you be able to review? (I can't request reviewers from a fork.)

@dajmcdon

Copy link
Copy Markdown
Contributor

@chenyangsu Thanks! I'm glad someone is using this package! We can try to review this as is, but it would be helpful if you could provide a reproducible example of the bad behaviour without the fix. Ideally using reprex::reprex() along with the desired behaviour with the fix.

@JavierMtzRdz would you be able to review this?

@JavierMtzRdz

Copy link
Copy Markdown
Contributor

@dajmcdon, absolutely. I’ll look into it!

@JavierMtzRdz JavierMtzRdz left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix looks good.

quantile_by_key = "geo_value” is supposed to split the residuals into groups and compute quantiles separately for each location. Currently, pivot_quantiles_wider never splits them. This seems to affect only flatline_forecaster since it follows the path that doesn’t group by key (after the if that checks whether residuals come with key columns).

The warnings here could use some improvement, but I’ll merge this and make those changes in a separate PR.

Here are some reproducible examples:

Without the fix (dev, 65bcece)

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. flatline: every location should get its own width
flatline_forecaster(jhu, "death_rate", flatline_args_list(
  quantile_levels = c(.1, .9), nonneg = FALSE, quantile_by_key = "geo_value"))$predictions %>%
  pivot_quantiles_wider(.pred_distn) %>%
  transmute(geo_value, width = `0.9` - `0.1`)
#> # A tibble: 3 × 2
#>   geo_value width
#>   <chr>     <dbl>
#> 1 ak        0.593
#> 2 ca        0.593
#> 3 ny        0.593

# 2. arx: ak has no forecast because its last 3 days are missing
jhu_ak_missing <- jhu %>%
  filter(!(geo_value == "ak" & time_value > max(time_value) - 3))
arx_forecaster(jhu_ak_missing, "death_rate", "death_rate", args_list = arx_args_list(
  quantile_levels = c(.1, .9), nonneg = FALSE, quantile_by_key = "geo_value"))$predictions %>%
  pivot_quantiles_wider(.pred_distn) %>%
  transmute(geo_value, width = `0.9` - `0.1`)
#> Warning: Some grouping keys are not in data.frame returned by the `residuals()` method.
#> Groupings may not be correct.
#> Error in `vec_arith()` at vctrs/R/type-vctr.R:657:5:
#> ! Can't recycle `x` (size 3) to match `y` (size 2).

With the fix(PR #519, 6ed660f)

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. flatline: every location should get its own width
flatline_forecaster(jhu, "death_rate", flatline_args_list(
  quantile_levels = c(.1, .9), nonneg = FALSE, quantile_by_key = "geo_value"))$predictions %>%
  pivot_quantiles_wider(.pred_distn) %>%
  transmute(geo_value, width = `0.9` - `0.1`)
#> # A tibble: 3 × 2
#>   geo_value width
#>   <chr>     <dbl>
#> 1 ak        2.83 
#> 2 ca        0.104
#> 3 ny        0.103

# 2. arx: ak has no forecast because its last 3 days are missing
jhu_ak_missing <- jhu %>%
  filter(!(geo_value == "ak" & time_value > max(time_value) - 3))
arx_forecaster(jhu_ak_missing, "death_rate", "death_rate", args_list = arx_args_list(
  quantile_levels = c(.1, .9), nonneg = FALSE, quantile_by_key = "geo_value"))$predictions %>%
  pivot_quantiles_wider(.pred_distn) %>%
  transmute(geo_value, width = `0.9` - `0.1`)
#> Warning: Some grouping keys are not in data.frame returned by the `residuals()` method.
#> Groupings may not be correct.
#> # A tibble: 2 × 2
#>   geo_value width
#>   <chr>     <dbl>
#> 1 ca        0.297
#> 2 ny        0.197

@JavierMtzRdz
JavierMtzRdz merged commit 43e1f69 into cmu-delphi:dev Nov 27, 2026
1 check passed
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.

3 participants