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

Formalize Table 1 process/functions #26

Open
2 tasks
mbcann01 opened this issue Dec 12, 2017 · 0 comments
Open
2 tasks

Formalize Table 1 process/functions #26

mbcann01 opened this issue Dec 12, 2017 · 0 comments
Labels
documentation Need to add better documentation enhancement New feature to existing function or feature high priority Start here

Comments

@mbcann01
Copy link
Member

mbcann01 commented Dec 12, 2017

You have a fair amount of loose code floating around related to creating tables for presentation/dissemination. Need to formalize and refine the processes and code. A good example comes from STEP:

# Create the table shell
# ---------------------
table <- tibble(
  variable   = "",
  class      = "",
  no_readmit = step_clean_03 %>% bfuncs::get_group_n(readmit_30_day == 0),
  readmit    = step_clean_03 %>% bfuncs::get_group_n(readmit_30_day == 1)
)

# Select data frame
# -----------------
df <- step_clean_03

# Select group variable (columns)
# -------------------------------
group <- quo(readmit_30_day)

# Select variables (in order of appearance)
# -----------------------------------------
vars <- quos(age, gender, race_eth, insurance, any_trans_limitations, pt_support_score, 
             health_lit_score, high_risk, appts_total, any_sw, any_pt, any_diet, pharmacist, 
             mental_health_concerns, falls_3_months, falls_12_months,chapters_7cat_short)


# Build table
# -----------

for (i in seq_along(vars)) {
  
  # Figure out what type of variable it is
  class_var_i <- df %>% 
    pull(!!vars[[i]]) %>% 
    class()
  
  # If it's a categorical (character/factor) variable:
  # Calculate percent and 95% CI
  # Then, add that row to the table
  if (class_var_i == "character" || class_var_i == "factor") {
    
    row <- df %>% 
      filter(!(is.na(!!vars[[i]]))) %>% # filter out missing
      group_by(!!group, !!vars[[i]]) %>% 
      freq_table() %>% 
      format_table() %>% 
      spread(key = !!group, value = percent_row_95) %>% 
      mutate(variable = colnames(.)[1]) %>% 
      rename("class" = !!vars[[i]], "no_readmit" = `FALSE`, "readmit" = `TRUE`) %>% 
      mutate(class = as.character(class)) # Need for bind_rows below

    # Append to bottom of table
    table <- bind_rows(table, row)
    
  # If it's a continuous variable:
  # Calculate mean and 95% CI
  # Then, add that row to the table 
  } else {
    
    row <- df %>% 
      group_by(!!group) %>% 
      bfuncs::mean_table(!!vars[[i]]) %>% 
      bfuncs::format_table() %>% 
      spread(key = !!group, value = mean_95) %>% 
      rename("variable" = var, "no_readmit" = `FALSE`, "readmit" = `TRUE`)

    # Append to bottom of table
    table <- bind_rows(table, row)
  }
}
@mbcann01 mbcann01 self-assigned this Dec 12, 2017
@mbcann01 mbcann01 added documentation Need to add better documentation enhancement New feature to existing function or feature high priority Start here labels Dec 12, 2017
@mbcann01 mbcann01 removed their assignment Sep 30, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Need to add better documentation enhancement New feature to existing function or feature high priority Start here
Projects
None yet
Development

No branches or pull requests

1 participant