Skip to content
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

add_ar and add_cfr not working #33

Open
PBBlomquist opened this issue Mar 9, 2025 · 0 comments
Open

add_ar and add_cfr not working #33

PBBlomquist opened this issue Mar 9, 2025 · 0 comments
Assignees

Comments

@PBBlomquist
Copy link

PBBlomquist commented Mar 9, 2025

Working on the r4epis sitrep package and need these two functions in epitabulate but they're not working.

add_ar

The function builds on a tbl_summary output, which works fine:

Image

But then when you add on the add_ar function it looks like this:

Image

See here:

library(dplyr)
library(gtsummary)
library(epitabulate)

# Create dummy data
linelist_cleaned <- data.frame(
  age_group = sample(c("0-4", "5-9", "10-14", "15-19"), 20, replace = TRUE)
) 

population_data_age <- data.frame(
  age_group = c("0-4", "5-9", "10-14", "15-19"),
  population = c(5000, 6000, 5500, 5200)  # Dummy population counts
)

# Test the code
linelist_cleaned %>%
  mutate(case_total = TRUE) %>% 
  select(case_total, age_group) %>%
  gtsummary::tbl_summary(
    include = age_group, 
    label = list(age_group ~ "Age Group")
  ) %>%
  epitabulate::add_ar(
    ## specify which variable counts cases (dummy)
    case_var = "case_total",
    ## get the population counts as a vector
    population = pull(population_data_age, population),
    drop_tblsummary_stat = TRUE)

My solution was to use attack_rate instead:

linelist_cleaned |> 
  count(age_group) |> 
  left_join(population_data_age) |> 
  mutate(attack_rate_result = pmap(list(n, population), ~ attack_rate(.x, .y, multiplier = 10000))) |> 
  select(-n, -population) |> 
  unnest(attack_rate_result) 

add_cfr()

This function also builds on a tbl_summary() output, which itself is fine, but then add_cfr() makes the output blank. You can see here:

library(dplyr)
library(gtsummary)
library(epitabulate)

# Dummy data
linelist_cleaned <- data.frame(
  patient_facility_type = sample(c("Inpatient", "Outpatient"), 20, replace = TRUE),
  DIED = sample(c(TRUE, FALSE, NA), 20, replace = TRUE),
  case_def = sample(c("Confirmed", "Probable", "Suspected"), 20, replace = TRUE)
)

# Test the code
linelist_cleaned %>%
  filter(patient_facility_type == "Inpatient") %>%
  select(DIED, case_def) %>%
  gtsummary::tbl_summary(
    include = case_def,
    statistic = case_def ~ "{n}",
    missing = "no",
    label = case_def ~ "Case definition"
  ) %>%
  modify_header(stat_0 = "Cases (N)") |> 
  # Use wrapper function to calculate cfr
  add_cfr(deaths_var = "DIED") 
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