# Statistical analysis code for the LEP pre- versus post-upgrade comparison.
# This submission copy preserves the complete analysis in the original LEP.R.

library(readr)
library(janitor)  # for clean_names()
library(dplyr)
library(haven)


# Define full file paths
# NOENGLISH <- read_csv("data/DisparitiesOfFunctio_DATA_LABELS_2026-02-02_1143.csv")

# NOENGLISH <- clean_names(NOENGLISH)
# names(NOENGLISH)

library(readr)
library(janitor)
library(dplyr)
library(gtsummary)
library(stringr)

# Read and clean the PI-approved de-identified dataset.
# Update the filename below if the submitted dataset has a different name.
data <- read_csv("data/deidentified_LEP_dataset.csv") %>%
  clean_names()

# Check the original period labels
unique(data$pre_post_period)

# Keep Non-English/LEP patients from both periods
data_lep <- data %>%
  filter(
    !is.na(preferred_language),
    preferred_language != "English"
  ) %>%
  mutate(
    upgrade_period = case_when(
      str_starts(pre_post_period, "Pre")  ~ "Pre-upgrade",
      str_starts(pre_post_period, "Post") ~ "Post-upgrade",
      TRUE                                ~ NA_character_
    ),
    
    upgrade_period = factor(
      upgrade_period,
      levels = c("Pre-upgrade", "Post-upgrade")
    ),
    
    mobility_change =
      discharge_mobility_qi_score -
      admission_mobility_qi_score
  ) %>%
  filter(!is.na(upgrade_period))

# Confirm sample sizes
table(data_lep$upgrade_period, useNA = "ifany")
data_lep <- data_lep %>%
  mutate(
    gender = case_when(
      gender_7 == "F" ~ "Female",
      gender_7 == "M" ~ "Male",
      gender_7 == "UNK" | is.na(gender_7) ~ "Unknown",
      TRUE ~ gender_7
    ),
    gender = factor(
      gender,
      levels = c("Female", "Male", "Unknown")
    )
  )

#################################################
# TABLE 1 — LEP patients: pre- vs post-upgrade
#################################################

table1_pre_post <- data_lep %>%
  select(
    upgrade_period,
    age_at_admission,
    gender,
    race,
    ethnicity,
    preferred_language,
    admission_mobility_qi_score,
    discharge_mobility_qi_score,
    mobility_change
  ) %>%
  tbl_summary(
    by = upgrade_period,
    statistic = list(
      all_continuous()  ~ "{mean} ({sd})",
      all_categorical() ~ "{n} ({p}%)"
    ),
    digits = all_continuous() ~ 1,
    missing = "ifany",
    label = list(
      age_at_admission ~ "Age at admission (years)",
      gender ~ "Gender",
      race ~ "Race",
      ethnicity ~ "Ethnicity",
      preferred_language ~ "Preferred language",
      admission_mobility_qi_score ~
        "Admission mobility QI score",
      discharge_mobility_qi_score ~
        "Discharge mobility QI score",
      mobility_change ~
        "Mobility change (discharge - admission)"
    )
  ) %>%
  add_p(
    test = list(
      all_continuous()  ~ "t.test",
      all_categorical() ~ "chisq.test"
    ),
    pvalue_fun = label_style_pvalue(digits = 2)
  ) %>%
  modify_header(
    label ~ "**Characteristic**",
    stat_1 ~ "**Pre-upgrade**  \nN = {n}",
    stat_2 ~ "**Post-upgrade**  \nN = {n}",
    p.value ~ "**p-value**"
  ) %>%
  bold_labels()

table1_pre_post

