Skip to contents

Counts detections in each window with calc_detection_windows(), then sets each current window against its previous window and names the direction of the change with detection_direction().

Usage

calc_detection_comparison(
  detections,
  as_of = Sys.Date(),
  windows = detection_default_windows(),
  pairs = detection_default_pairs(),
  level = NULL,
  threshold = 15,
  metrics = c("detections", "reported_cases", "positive_es", "provinces", "clusters"),
  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().

pairs

A data frame with the columns current and previous, each naming a window in windows. Default detection_default_pairs().

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.

threshold

Size of change, in percent, at or below which a move is called flat. Default 15.

metrics

The counts to compare, from "detections", "reported_cases", "positive_es", "provinces" and "clusters". Default all five.

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, window pair and metric: with level, the columns level, guid and name; then current_window, previous_window, metric, current, previous, difference, pct_change and direction.

Details

The percentage change is the difference divided by the previous count, times 100, so it is in per cent and can be negative or above 100. When the previous count is zero it is Inf if the current count is above zero, and zero otherwise.

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_comparison(detections, as_of = "2024-07-01")
#> # A tibble: 10 × 8
#>    current_window previous_window metric  current previous difference pct_change
#>    <chr>          <chr>           <chr>     <int>    <int>      <int>      <dbl>
#>  1 last_6         preceding_6     detect…       2        0          2        Inf
#>  2 last_6         preceding_6     report…       1        0          1        Inf
#>  3 last_6         preceding_6     positi…       1        0          1        Inf
#>  4 last_6         preceding_6     provin…       1        0          1        Inf
#>  5 last_6         preceding_6     cluste…       2        0          2        Inf
#>  6 last_12        preceding_12    detect…       2        1          1        100
#>  7 last_12        preceding_12    report…       1        1          0          0
#>  8 last_12        preceding_12    positi…       1        0          1        Inf
#>  9 last_12        preceding_12    provin…       1        1          0          0
#> 10 last_12        preceding_12    cluste…       2        0          2        Inf
#> # ℹ 1 more variable: direction <chr>