brad-cannell / bfuncs

A random smattering of Brad's Functions
MIT License
2 stars 0 forks source link

Improve Presenting and Disseminating Results Vignette #23

Open mbcann01 opened 6 years ago

mbcann01 commented 6 years ago

Won’t be able to create a package that will work for every situation. But, can probably create some useful stuff that will work in many situations.

mbcann01 commented 6 years ago

From STEP table 1

# 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_diet_f, any_pt_f, any_sw_f, 
             pharmacist_f, 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 a blank row before each categorical variable
    # later this will create space to slide class levels over
    blank_row <- tibble(
      variable   = !!quo_name(vars[[i]]),
      class      = "",
      no_readmit = "",
      readmit    = ""
    )

    # Append to bottom of table
    table <- bind_rows(table, blank_row, 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)
  }
}

# Clean up
rm(blank_row, df, row, vars, class_var_i, group, i)
# Improve table appearance - automate somehow
table <- table %>% mutate(
  variable = if_else(variable == "age", 
                     "Age, mean (95% CI)", variable),
  variable = if_else(variable == "gender", 
                     "Gender, % (95% CI)", variable),
  variable = if_else(variable == "race_eth", 
                     "Race and ethnicity, % (95% CI)", variable),
  variable = if_else(variable == "insurance", 
                     "Type of insurance, % (95% CI)", variable),
  variable = if_else(variable == "any_trans_limitations", 
                     "Transportation limitations, % (95% CI)", variable),
  variable = if_else(variable == "pt_support_score", 
                     "Social support score, mean (95% CI)", variable),
  variable = if_else(variable == "health_lit_score", 
                     "Health literacy score, mean (95% CI)", variable),
  variable = if_else(variable == "high_risk", 
                     "High risk strata, % (95% CI)", variable),
  variable = if_else(variable == "appts_total", 
                     "Total appointments, mean (95% CI)", variable),
  variable = if_else(variable == "any_diet_f", 
                     "Met with dietitian, % (95% CI)", variable),
  variable = if_else(variable == "any_pt_f", 
                     "Met with physical therapist, % (95% CI)", variable),
  variable = if_else(variable == "any_sw_f", "
                     Met with social worker, % (95% CI)", variable),
  variable = if_else(variable == "pharmacist_f", 
                     "Met with pharmacist, % (95% CI)", variable),
  variable = if_else(variable == "mental_health_concerns", 
                     "Mental health concerns, % (95% CI)", variable),
  variable = if_else(variable == "falls_3_months", 
                     "Fall in past 3 months, % (95% CI)", variable),
  variable = if_else(variable == "falls_12_months", 
                     "Fall in past 12 months, % (95% CI)", variable),
  variable = if_else(variable == "chapters_7cat_short", 
                     "Admission diagnosis, % (95% CI)", variable)
)
# Add blank row after each categoricla variable - for sliding class levels over later
# For some reason, R automatically strips the leading white space. 
# The best work around I can come up with is to add dashes, then find and replaces dashes with white space in Word.
table <- table %>% 
  group_by(variable) %>% 
  mutate(
    row = row_number(),
    class = stringr::str_replace(class, "^", "---")
  ) %>% 
  ungroup() %>% 
  mutate(variable = if_else(row > 1, class, variable)) %>% 
  select(-class, - row)
table_kable <- knitr::kable(table, col.names = c(
  "Characteristic", 
  "No Readmit", 
  "Readmit")
)
table_kable