Skip to content

Fix handling of keys in epi_keys_mold #114

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

Closed
1 task done
mgyliu opened this issue Aug 2, 2022 · 1 comment
Closed
1 task done

Fix handling of keys in epi_keys_mold #114

mgyliu opened this issue Aug 2, 2022 · 1 comment
Assignees

Comments

@mgyliu
Copy link
Contributor

mgyliu commented Aug 2, 2022

Creating this issue to track. I will implement this in my vignette PR (TODO mgyliu: link the PR)

Related to epiprocess issue cmu-delphi/epiprocess#182

Action items

  • Change the way epi_keys_mold handles multiple other_keys in epi_df metadata

The problem

When the metadata for an epi_df looks like this

> attr(ex1,"metadata")
$geo_type
[1] "state"

$time_type
[1] "day"

$as_of
[1] "2020-06-03"

$other_keys
[1] "county_code" "another_key"

calling predict fails in epi_keys_mold because of the purrr::map_chr line. map_chr doesn't like that other_keys has a nested list in it.

Example to replicate

ex1_input <- tibble::tibble(
  geo_value = rep(c("ca", "fl", "pa"), each = 2),
  county_code = c("06059","06061","06067",
                  "12111","12113","12117"),
  another_key = 1:6, # <- I added this additional key 
  time_value = rep(seq(as.Date("2020-06-01"), as.Date("2020-06-03"),
                       by = "day"), length.out = length(geo_value)),
  value = 1:length(geo_value) + 0.01 * rnorm(length(geo_value))) %>% 
  tsibble::as_tsibble(
    index = time_value, 
    key = c(geo_value, county_code,another_key))

ex1 <- as_epi_df(x = ex1_input, geo_type = "state", time_type = "day", as_of = "2020-06-03")
attr(ex1,"metadata")

# Output 
# $geo_type
# [1] "state"
# 
# $time_type
# [1] "day"
# 
# $as_of
# [1] "2020-06-03"
# 
# $other_keys
# [1] "county_code" "another_key"

rec1 <- epi_recipe(ex1) %>%
  step_epi_ahead(value, ahead=3) %>%
  step_epi_naomit()

wf1 <- epi_workflow(rec1, parsnip::linear_reg()) %>% parsnip::fit(ex1)
stats::predict(wf1, ex1)

The last line spits out this error:

Error in `stop_bad_type()`:
! Result 3 must be a single string, not a character vector of length 2
Run `rlang::last_error()` to see where the error occurred.
Backtrace:
1: stop(fallback)
2: signal_abort(cnd, .file)
3: abort(message, x = x, expected = expected, actual = actual, what = what, 
4: stop_bad_type(x, expected, actual = actual, what = what, arg = arg, 
5: stop_bad_vector(x, expected_ptype, expected_length, what = what, 
6: purrr:::stop_bad_element_vector(c("county_code", "another_key"
7: purrr::map_chr(mold$extras$roles[molded_names %in% keys], names)
8: epi_keys_mold(mold)
9: grab_forged_keys(components$forged, components$mold, new_data)
10: predict.epi_workflow(wf1, ex1)
11: stats::predict(wf1, ex1)

The fix

The fix is to replace the last 2 lines with

mold_keys <- map(mold$extras$roles[molded_names %in% keys], names)
unlist(unname(mold_keys))
@dajmcdon
Copy link
Contributor

Closed in #116

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

No branches or pull requests

2 participants