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

Improve Presenting and Disseminating Results Vignette #23

Open
2 of 4 tasks
mbcann01 opened this issue Nov 28, 2017 · 1 comment
Open
2 of 4 tasks

Improve Presenting and Disseminating Results Vignette #23

mbcann01 opened this issue Nov 28, 2017 · 1 comment
Labels
documentation Need to add better documentation high priority Start here

Comments

@mbcann01
Copy link
Member

mbcann01 commented Nov 28, 2017

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.

  • STEP has an example of “sliding” the categories under the row header. Get it to work.
  • STEP table_1_word_step_only has some examples of including some of my new helper functions.
  • Take a look at some of the various tables in WHI SF as well
  • Add a mini table of contents under the "creating tables of summary statistics section"
@mbcann01 mbcann01 added the documentation Need to add better documentation label Nov 28, 2017
@mbcann01 mbcann01 self-assigned this Nov 28, 2017
@mbcann01
Copy link
Member Author

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

@mbcann01 mbcann01 added the high priority Start here label Jan 18, 2018
@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 high priority Start here
Projects
None yet
Development

No branches or pull requests

1 participant