Skip to contents

Counts the detections whose virus date falls in each window, overall or by administrative unit. A detection falls in a window when its virus date lies between as_of - from and as_of - to days, both ends included. A detection with no virus date falls in no window. Every unit present in detections gets a row for every window, with zero where nothing was detected.

Usage

calc_detection_windows(
  detections,
  as_of = Sys.Date(),
  windows = detection_default_windows(),
  level = NULL,
  env_sources = "Environmental",
  cols = list()
)

Arguments

detections

A data frame with one row per detection, such as the output of detection_slice().

as_of

The reporting date, as a Date or a string that as.Date() reads. Defaults to Sys.Date(). The windows are counted back from it.

windows

A data frame with the columns window, from and to, where from and to are whole days before as_of and from is at least to. Default detection_default_windows().

level

Administrative level to count by: NULL (the default) for one row per window over all detections, or "adm0" (country), "adm1" (province) or "adm2" (district). Detections with no identifier at that level are left out.

env_sources

Source values that mark an environmental sample. Default "Environmental".

cols

Named list overriding the default column names, keyed by table, for example list(detections = list(adm1 = "province")). This function reads the detections roles virus_date, source, adm1 (the province counted in provinces) and cluster, by default the clean_virus() names virus_date, source, adm1 and virus_cluster, and with level the GUID and name roles of that level (adm0_guid and adm0, adm1_guid and adm1, or adm2_guid and adm2).

Value

A tibble with one row per unit and window, in the order of windows: with level, the columns level, guid and name; then window, from, to, window_start, window_end and the five counts.

Details

Five counts are returned for each window:

  • detections, every detection in the window;

  • reported_cases, detections whose source is not in env_sources, so a missing source counts as a case;

  • positive_es, detections whose source is in env_sources;

  • provinces, the number of distinct non-missing provinces;

  • clusters, the number of distinct clusters, leaving out missing and empty cluster names.

Examples

detections <- tibble::tibble(
  virus_date = as.Date(c("2024-06-01", "2024-01-15", "2023-05-01")),
  source = c("AFP", "Environmental", "AFP"),
  adm1 = c("Kandahar", "Kandahar", "Helmand"),
  virus_cluster = c("C1", "C2", NA)
)
calc_detection_windows(detections, as_of = "2024-07-01")
#> # A tibble: 4 × 10
#>   window        from    to window_start window_end detections reported_cases
#>   <chr>        <int> <int> <date>       <date>          <int>          <int>
#> 1 last_6         218     0 2023-11-26   2024-07-01          2              1
#> 2 preceding_6    402   219 2023-05-26   2023-11-25          0              0
#> 3 last_12        400     0 2023-05-28   2024-07-01          2              1
#> 4 preceding_12   766   401 2022-05-27   2023-05-27          1              1
#> # ℹ 3 more variables: positive_es <int>, provinces <int>, clusters <int>