Skip to content

enh: pivot_quantiles_longer adds quantile_level to epi_df other_keys #460

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: epipredict
Title: Basic epidemiology forecasting methods
Version: 0.1.15
Version: 0.1.16
Authors@R: c(
person("Daniel J.", "McDonald", , "[email protected]", role = c("aut", "cre")),
person("Ryan", "Tibshirani", , "[email protected]", role = "aut"),
Expand Down
6 changes: 4 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ Pre-1.0.0 numbering scheme: 0.x will indicate releases, while 0.0.x will indicat
`data(<dataset name>, package = "epidatasets")`, `epidatasets::<dataset name>`
or, after loading the package, the name of the dataset alone (#382).
- `step_adjust_latency()` no longer allows empty column selection.
- Addresses upstream breaking changes from cmu-delphi/epiprocess#595 (`growth_rate()`).
- Addresses upstream breaking changes from cmu-delphi/epiprocess#595 (`growth_rate()`).
`step_growth_rate()` has lost its `additional_gr_args_list` argument and now
has an `na_rm` argument.
- Moves `epiprocess` out of depends (#440). No internals have changed, but downstream
users may need to add `library(epiprocess)` to existing code.
- Removes dependence on the `distributional` package, replacing the quantiles
- Removes dependence on the `distributional` package, replacing the quantiles
with `hardhat::quantile_pred()`. Some associated functions are deprecated with
`lifecycle` messages.
- Rename `check_enough_train_data()` to `check_enough_data()`, and generalize it
Expand All @@ -38,6 +38,8 @@ Pre-1.0.0 numbering scheme: 0.x will indicate releases, while 0.0.x will indicat
- Replace `dist_quantiles()` with `hardhat::quantile_pred()`
- Allow `quantile()` to threshold to an interval if desired (#434)
- `arx_forecaster()` detects if there's enough data to predict
- `pivot_quantiles_longer()` now appropriately adds `quantile_level` to the
`epi_df` other keys

## Bug fixes

Expand Down
6 changes: 5 additions & 1 deletion R/pivot_quantiles.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,12 @@ pivot_quantiles_longer <- function(.data, ...) {
long_tib <- as_tibble(.data[[col]])
.data <- select(.data, !all_of(col))
names(long_tib)[1:2] <- c(glue::glue("{col}_value"), glue::glue("{col}_quantile_level"))
left_join(.data, long_tib, by = ".row") %>%
out <- left_join(.data, long_tib, by = ".row") %>%
select(!.row)
if (inherits(.data, "epi_df")) {
attr(out, "metadata")$other_keys <- c(attr(.data, "metadata")$other_keys, glue::glue("{col}_quantile_level"))
}
out
}

#' Pivot a column containing `quantile_pred` wider
Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/test-pivot_quantiles.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ test_that("quantile pivotting longer behaves", {
expect_length(pivot_quantiles_longer(tib, d1), 4L)
expect_identical(nrow(pivot_quantiles_longer(tib, d1)), 6L)
expect_identical(pivot_quantiles_longer(tib, d1)$d1_value, c(1:3, 2:4))

# add quantile_level to epi_df other_keys, if epi_df
tib <- tibble(geo_value = c("a", "b"), time_value = as.Date(c("2021-01-01", "2021-01-02")), d1 = d1, d2 = d2)
epi_df <- tib %>% as_epi_df() %>% pivot_quantiles_longer(d1)
expect_equal(key_colnames(epi_df), c("geo_value","d1_quantile_level", "time_value"))
})

test_that("nested_quantiles is deprecated, but works where possible", {
Expand Down