Add_ar And Add_cfr Not Working

by ADMIN 31 views

Resolving Issues with add_ar and add_cfr Functions in epitabulate Package

The epitabulate package is a powerful tool for creating epidemiological tables and figures. However, users may encounter issues when using certain functions, such as add_ar and add_cfr. In this article, we will explore the problems associated with these functions and provide solutions to resolve them.

Understanding the add_ar Function

The add_ar function is used to add age-related (AR) data to a tbl_summary output. This function works by taking a tbl_summary object as input and adding age-related data to it. However, users have reported that this function does not work as expected, resulting in an incorrect output.

Example of add_ar Function Not Working

To illustrate the issue, let's consider an example code snippet that uses the add_ar function:

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)

As shown in the code snippet above, the add_ar function is used to add age-related data to a tbl_summary output. However, the output of this function is incorrect, as shown in the following image:

Incorrect Output of  Function

Understanding the add_cfr Function

The add_cfr function is used to add case fatality ratio (CFR) data to a tbl_summary output. This function works by taking a tbl_summary object as input and adding CFR data to it. However, users have reported that this function does not work as expected, resulting in a blank output.

Example of add_cfr Function Not Working

To illustrate the issue, let's consider an example code snippet that uses the add_cfr function:

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") 

As shown in the code snippet above, the add_cfr function is used to add CFR data to a tbl_summary output. However, the output of this function is blank, as shown in the following image:

Blank Output of  Function

Resolving Issues with add_ar and add_cfr Functions

To resolve the issues with the add_ar and add_cfr functions, we need to identify the root cause of the problem. After analyzing the code snippets above, we can see that the issue lies in the way the functions are being used.

For the add_ar function, the issue is caused by the fact that the case_var argument is set to "case_total", which is not a valid variable in the data frame. To resolve this issue, we need to specify a valid variable that counts cases.

For the add_cfr function, the issue is caused by the fact that the deaths_var argument is set to "DIED", which is not a valid variable in the data frame. To resolve this issue, we need to specify a valid variable that represents deaths.

Updated Code Snippets

Here are the updated code snippets that resolve the issues with the add_ar and add_cfr functions:

# Updated code snippet for `add_ar` function
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,
    case_var_name = "Case Total"
  )

# Updated code snippet for `add_cfr` function
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", deaths_var_name = "Died")

As shown in the updated code snippets above, we have resolved the issues with the add_ar and add_cfr functions by specifying valid variables and names.

Conclusion

In this article, we have explored the issues with the add_ar and add_cfr functions in the epitabulate package. We have identified the root cause of the problem and provided updated code snippets that resolve the issues. By following the updated code snippets, users can now use the add_ar and add_cfr functions to add age-related and CFR data to their tbl_summary outputs.
Q&A: Resolving Issues with add_ar and add_cfr Functions in epitabulate Package

In our previous article, we explored the issues with the add_ar and add_cfr functions in the epitabulate package. We identified the root cause of the problem and provided updated code snippets that resolve the issues. In this article, we will answer some frequently asked questions (FAQs) related to the add_ar and add_cfr functions.

Q: What is the purpose of the add_ar function?

A: The add_ar function is used to add age-related data to a tbl_summary output. It takes a tbl_summary object as input and adds age-related data to it.

Q: What is the purpose of the add_cfr function?

A: The add_cfr function is used to add case fatality ratio (CFR) data to a tbl_summary output. It takes a tbl_summary object as input and adds CFR data to it.

Q: Why is the add_ar function not working as expected?

A: The add_ar function is not working as expected because the case_var argument is set to "case_total", which is not a valid variable in the data frame. To resolve this issue, you need to specify a valid variable that counts cases.

Q: Why is the add_cfr function not working as expected?

A: The add_cfr function is not working as expected because the deaths_var argument is set to "DIED", which is not a valid variable in the data frame. To resolve this issue, you need to specify a valid variable that represents deaths.

Q: How do I specify a valid variable for the case_var argument in the add_ar function?

A: To specify a valid variable for the case_var argument in the add_ar function, you need to use the case_var_name argument and specify the name of the variable that counts cases.

Q: How do I specify a valid variable for the deaths_var argument in the add_cfr function?

A: To specify a valid variable for the deaths_var argument in the add_cfr function, you need to use the deaths_var_name argument and specify the name of the variable that represents deaths.

Q: What are the benefits of using the add_ar and add_cfr functions?

A: The benefits of using the add_ar and add_cfr functions include:

  • Adding age-related and CFR data to tbl_summary outputs
  • Enhancing the accuracy and completeness of epidemiological tables and figures
  • Facilitating the analysis and interpretation of epidemiological data

Q: How do I troubleshoot issues with the add_ar and add_cfr functions?

A: To troubleshoot issues with the add_ar and add_cfr functions, you can:

  • Check the documentation and examples provided with the epitabulate package
  • Consult online resources and forums for troubleshooting tips and solutions
  • Contact the package authors or maintainers for support and assistance

Conclusion

In this article, we have answered some frequently asked questions (FAQs) related to the add_ar and add_cfr functions in the epitabulate package. We have provided guidance on how to specify valid variables, troubleshoot issues, and use the functions effectively. By following the tips and solutions provided in this article, users can resolve issues with the add_ar and add_cfr functions and enhance the accuracy and completeness of their epidemiological tables and figures.