Skip to content

[Issue 182] Update epi_df examples, check additional_metadata type at construction #183

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

Merged
merged 6 commits into from
Aug 5, 2022
Merged
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
27 changes: 19 additions & 8 deletions R/epi_df.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,9 @@ NULL
#' then the current day-time will be used.
#' @param additional_metadata List of additional metadata to attach to the
#' `epi_df` object. The metadata will have `geo_type`, `time_type`, and
#' `as_of` fields; named entries from the passed list or will be included as
#' well.
#' `as_of` fields; named entries from the passed list will be included as
#' well. If your tibble has additional keys, be sure to specify them as a
#' character vector in the `other_keys` component of `additional_metadata`.
#' @param ... Additional arguments passed to methods.
#' @return An `epi_df` object.
#'
Expand All @@ -117,7 +118,11 @@ new_epi_df = function(x = tibble::tibble(), geo_type, time_type, as_of,
if (!is.data.frame(x)) {
Abort("`x` must be a data frame.")
}


if (!is.list(additional_metadata)) {
Abort("`additional_metadata` must be a list type.")
}

# If geo type is missing, then try to guess it
if (missing(geo_type)) {
geo_type = guess_geo_type(x$geo_value)
Expand Down Expand Up @@ -184,8 +189,9 @@ new_epi_df = function(x = tibble::tibble(), geo_type, time_type, as_of,
#' then the current day-time will be used.
#' @param additional_metadata List of additional metadata to attach to the
#' `epi_df` object. The metadata will have `geo_type`, `time_type`, and
#' `as_of` fields; named entries from the passed list or will be included as
#' well.
#' `as_of` fields; named entries from the passed list will be included as
#' well. If your tibble has additional keys, be sure to specify them as a
#' character vector in the `other_keys` component of `additional_metadata`.
#' @param ... Additional arguments passed to methods.
#' @return An `epi_df` object.
#'
Expand Down Expand Up @@ -230,7 +236,7 @@ new_epi_df = function(x = tibble::tibble(), geo_type, time_type, as_of,
#'
#' ex2 <- ex2_input %>% dplyr::rename(geo_value = state, time_value = reported_date) %>%
#' as_epi_df(geo_type = "state", as_of = "2020-06-03",
#' additional_metadata = c(other_keys = "pol"))
#' additional_metadata = list(other_keys = "pol"))
#'
#' attr(ex2,"metadata")
#'
Expand All @@ -244,8 +250,13 @@ new_epi_df = function(x = tibble::tibble(), geo_type, time_type, as_of,
#'
#' ex3 <- ex3_input %>%
#' tsibble::as_tsibble() %>% # needed to add the additional metadata
#' dplyr::mutate(state = rep("MA",6)) %>%
#' as_epi_df(additional_metadata = c(other_keys = "state"))
#' # add 2 extra keys
#' dplyr::mutate(
#' state = rep("MA",6),
#' pol = rep(c("blue", "swing", "swing"), each = 2)) %>%
#' # the 2 extra keys we added have to be specified in the other_keys
#' # component of additional_metadata.
#' as_epi_df(additional_metadata = list(other_keys = c("state", "pol")))
#'
#' attr(ex3,"metadata")
as_epi_df = function(x, ...) {
Expand Down
12 changes: 4 additions & 8 deletions man/as_epi_archive.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 16 additions & 10 deletions man/as_epi_df.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 21 additions & 21 deletions man/epi_archive.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 4 additions & 8 deletions man/epi_slide.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 4 additions & 8 deletions man/epix_as_of.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 4 additions & 8 deletions man/epix_slide.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions man/new_epi_df.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions tests/testthat/test-epi_df.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,18 @@ test_that("new_epi_df works as intended", {
expect_identical(attributes(epi_tib)$metadata$time_type, "day")
expect_true(lubridate::is.POSIXt(attributes(epi_tib)$metadata$as_of))
})

test_that("as_epi_df errors when additional_metadata is not a list", {
# This is the 3rd example from as_epi_df
ex_input <- jhu_csse_county_level_subset %>%
dplyr::filter(time_value > "2021-12-01", state_name == "Massachusetts") %>%
dplyr::slice_tail(n = 6) %>%
tsibble::as_tsibble() %>%
dplyr::mutate(
state = rep("MA",6),
pol = rep(c("blue", "swing", "swing"), each = 2))

expect_error(
as_epi_df(ex_input, additional_metadata = c(other_keys = "state", "pol")),
"`additional_metadata` must be a list type.")
})
2 changes: 1 addition & 1 deletion tests/testthat/test-methods-epi_df.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ toy_epi_df <- tibble::tibble(
), times = 2),
geo_value = rep(c("ca", "hi"), each = 5),
indicator_var = as.factor(rep(1:2, times = 5)),
) %>% as_epi_df(additional_metadata = c(other_keys = "indicator_var"))
) %>% as_epi_df(additional_metadata = list(other_keys = "indicator_var"))

att_toy = attr(toy_epi_df, "metadata")

Expand Down
14 changes: 9 additions & 5 deletions vignettes/epiprocess.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ head(ex2)

ex2 <- ex2 %>% rename(geo_value = state, time_value = reported_date) %>%
as_epi_df(geo_type = "state", as_of = "2020-06-03",
additional_metadata = c(other_keys = "pol"))
additional_metadata = list(other_keys = "pol"))

attr(ex2,"metadata")
```
Expand All @@ -200,17 +200,21 @@ ex3 <- jhu_csse_county_level_subset %>%
attr(ex3,"metadata") # geo_type is county currently
```

Now we add state (MA) as a new column and a key to the metadata. Reminder that lower case state name abbreviations are what we would expect if this were a `geo_value` column.
```{r}
Now we add `state` (MA) and `pol` as new columns to the data and as new keys to the metadata. Reminder that lower case state name abbreviations are what we would expect if this were a `geo_value` column.

```{r}
ex3 <- ex3 %>%
as_tibble() %>% # needed to add the additional metadata
mutate(state = rep(tolower("MA"),6)) %>%
as_epi_df(additional_metadata = c(other_keys = "state"))
mutate(
state = rep(tolower("MA"),6),
pol = rep(c("blue", "swing", "swing"), each = 2)) %>%
as_epi_df(additional_metadata = list(other_keys = c("state", "pol")))

attr(ex3,"metadata")
```

Note that the two additional keys we added, `state` and `pol`, are specified as a character vector in the `other_keys` component of the `additional_metadata` list. They must be specified in this manner so that downstream actions on the `epi_df`, like model fitting and prediction, can recognize and use these keys.

Currently `other_keys` metadata in `epi_df` doesn't impact `epi_slide()`, contrary to `other_keys` in `as_epi_archive` which affects how the update data is interpreted.

## Working with `epi_df` objects downstream
Expand Down