Repository navigation
fix: compute layer_residual_quantiles() by_key quantiles within each key - #519
Conversation
|
@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 @JavierMtzRdz would you be able to review this? |
|
@dajmcdon, absolutely. I’ll look into it! |
There was a problem hiding this comment.
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
Checklist
Please:
dajmcdon.
DESCRIPTIONandNEWS.md.(backwards-incompatible changes to the documented interface) are noted.
epiprocessversion in theDESCRIPTIONfile ifepiprocesssoonepipredictandepiprocessChange 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. Forlmfits, a key with no prediction errors with "Can't recyclex(size 3) to matchy(size 2)".The layer now groups residuals by
by_keyand joins the quantiles to the predictions by key. Related to #490; the default is unchanged.Testing: two new tests in
test-layer_residual_quantiles.Rfail 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.)