epiverse-trace / howto

https://epiverse-trace.github.io/howto/
Creative Commons Attribution 4.0 International
1 stars 2 forks source link

how to pass multiple `<epidist>` objects to one (or multiple) `{cfr}` data inputs #36

Open avallecam opened 7 months ago

avallecam commented 7 months ago

@joshwlambert @pratikunterwegs as mentioned in https://github.com/epiverse-trace/epiparameter/pull/254#issuecomment-1986401401, testing the epiparameter::distribution_tbl() interfase I tried to connect multiple <epidist> as inputs for one data for {cfr} and the most relevant stuck was in trying to input the delay_density list column of functions (containing multiple objects) in purrr::pmap().

This is just exploration, so no urgency on my side to solve this. I wanted to show you this trial given the tibble output from epiparameter. This is not a feature to solve in either package, so posted this in a neutral space. The issue, I think, is on reminding or looking at how to input a function argument to the .f, stored in one list column.

# remotes::install_github("epiverse-trace/epiparameter@parameter_tbl")

library(cfr)
library(epiparameter)
library(tidyverse)

nest_output <- epidist_db(
  disease = "COVID-19",
  epi_dist = "incubation period"
) %>% 
  epiparameter::distribution_tbl() %>% 
  select(
    disease,
    epi_distribution,
    prob_distribution,
    author,
    sample_size
  ) %>% 
  filter(!is.na(prob_distribution)) %>% 
  group_by(prob_distribution, author) %>% 
  filter(sample_size == max(sample_size)) %>% 
  ungroup() %>% 
  mutate(author = str_replace(author,"(.+) et al.","\\1")) %>% 
  # mutate(
  #   subset_chr = str_c("sample_size == ", sample_size)
  # ) %>% 
  select(
    disease,
    epi_dist = epi_distribution,
    author,
    # subset_chr
    ) %>%
  # rowwise() %>% 
  # mutate(subset = list(rlang::sym(subset_chr))) %>%
  # ungroup() %>% 
  # select(-subset_chr) %>% 
  # identity()
  # glimpse()
  mutate(
    get_epidist = pmap(
      # .l = select(.,-subset,-subset_chr),
      .l = select(.,disease,epi_dist,author),
      .f = epidist_db,
      single_epidist = TRUE
    )
  ) %>% 
  bind_cols(
    data = nest(ebola1976)
  ) %>% 
  rowwise() %>% 
  mutate(
    delay_density = list(function(x) density(get_epidist, x))
  ) %>% 
  ungroup()
#> Returning 15 results that match the criteria (11 are parameterised). 
#> Use subset to filter by entry variables or single_epidist to return a single entry. 
#> To retrieve the short citation for each use the 'get_citation' function
#> Using Yang L, Dai J, Zhao J, Wang Y, Deng P, Wang J (2020). "Estimation of
#> incubation period and serial interval of COVID-19: analysis of 178
#> cases and 131 transmission chains in Hubei province, China."
#> _Epidemiology and Infection_. doi:10.1017/S0950268820001338
#> <https://doi.org/10.1017/S0950268820001338>.. 
#> To retrieve the short citation use the 'get_citation' function
#> Using Bui L, Nguyen H, Levine H, Nguyen H, Nguyen T, Nguyen T, Nguyen T, Do
#> T, Pham N, Bui M (2020). "Estimation of the incubation period of
#> COVID-19 in Vietnam." _PLoS One_. doi:10.1371/journal.pone.0243889
#> <https://doi.org/10.1371/journal.pone.0243889>.. 
#> To retrieve the short citation use the 'get_citation' function
#> Using McAloon C, Collins Á, Hunt K, Barber A, Byrne A, Butler F, Casey M,
#> Griffin J, Lane E, McEvoy D, Wall P, Green M, O'Grady L, More S (2020).
#> "Incubation period of COVID-19: a rapid systematic review and
#> meta-analysis of observational research." _BMJ Open_.
#> doi:10.1136/bmjopen-2020-039652
#> <https://doi.org/10.1136/bmjopen-2020-039652>.. 
#> To retrieve the short citation use the 'get_citation' function
#> Using Linton N, Kobayashi T, Yang Y, Hayashi K, Akhmetzhanov A, Jung S, Yuan
#> B, Kinoshita R, Nishiura H (2020). "Incubation Period and Other
#> Epidemiological Characteristics of 2019 Novel Coronavirus Infections
#> with Right Truncation: A Statistical Analysis of Publicly Available
#> Case Data." _Journal of Clinical Medicine_. doi:10.3390/jcm9020538
#> <https://doi.org/10.3390/jcm9020538>.. 
#> To retrieve the short citation use the 'get_citation' function
#> Using Lauer S, Grantz K, Bi Q, Jones F, Zheng Q, Meredith H, Azman A, Reich
#> N, Lessler J (2020). "The Incubation Period of Coronavirus Disease 2019
#> (COVID-19) From Publicly Reported Confirmed Cases: Estimation and
#> Application." _Annals of Internal Medicine_. doi:10.7326/M20-0504
#> <https://doi.org/10.7326/M20-0504>.. 
#> To retrieve the short citation use the 'get_citation' function

nest_output
#> # A tibble: 5 × 6
#>   disease  epi_dist          author  get_epidist data              delay_density
#>   <chr>    <chr>             <chr>   <list>      <list>            <list>       
#> 1 COVID-19 incubation period Yang    <epidist>   <tibble [73 × 3]> <fn>         
#> 2 COVID-19 incubation period Bui     <epidist>   <tibble [73 × 3]> <fn>         
#> 3 COVID-19 incubation period McAloon <epidist>   <tibble [73 × 3]> <fn>         
#> 4 COVID-19 incubation period Linton  <epidist>   <tibble [73 × 3]> <fn>         
#> 5 COVID-19 incubation period Lauer   <epidist>   <tibble [73 × 3]> <fn>

nest_output %>% 
  slice(1) %>% 
  pull(delay_density)
#> [[1]]
#> function(x) density(get_epidist, x)
#> <environment: 0x000001320310dad0>

# tried to input delay_density, but failed
nest_output %>% 
  mutate(
    get_cfr = pmap(
      # .l = select(., data, -delay_density), # this works, but with no delays
      .l = select(., data, delay_density),
      .f = cfr_static
    )
  ) %>% 
  unnest(get_cfr)
#> Error in `mutate()`:
#> ℹ In argument: `get_cfr = pmap(.l = select(., data, delay_density), .f =
#>   cfr_static)`.
#> Caused by error in `pmap()`:
#> ℹ In index: 1.
#> Caused by error:
#> ! Obsolete data mask.
#> ✖ Too late to resolve `get_epidist` after the end of `dplyr::mutate()`.
#> ℹ Did you save an object that uses `get_epidist` lazily in a column in the
#>   `dplyr::mutate()` expression ?

Created on 2024-03-08 with reprex v2.1.0

avallecam commented 5 months ago

we can use as.function() to solve this https://github.com/epiverse-trace/simulist/issues/107

pratikunterwegs commented 5 months ago

Sorry @avallecam - I don't think I saw this although the issue is a month old; don't think I got a notification for some reason. Is this still an open issue or is it fixed now?

avallecam commented 5 months ago

Is this still an open issue or is it fixed now?

Yes, it is still open. This depends on the status of the parameter_tbl branch in epiparameter.

I think this type of workflow may also work for the code in https://github.com/epiverse-trace/epiparameter/discussions/269