Importing the
Data
The original data is read in and copied to a new data frame. Data
type is then confirmed for all character, factor, and date
variables.
# Read in the data
Original_data <- read_excel("Original_data.xlsx")
# Pre-process the data to handle '#NULL!' values by replacing them with NA
Original_data <- Original_data %>%
mutate(across(where(is.character), ~na_if(.x, "#NULL!")))
# Now ensure that all the data types are correct
data <- Original_data %>%
mutate(
# Converting character columns
studyid = as.character(studyid),
site = as.character(site),
CODE = as.character(CODE),
CODETYPE = as.character(CODETYPE),
ENCTYPE = as.character(ENCTYPE),
ENCOUNTER_SUBTYPE = as.character(ENCOUNTER_SUBTYPE),
DEPT = as.character(DEPT),
# Converting categorical columns to factors
transgender = factor(transgender),
procedure = factor(procedure),
sex_natal = factor(sex_natal),
RACE1 = factor(RACE1),
RACE2 = factor(RACE2),
HISPANIC = factor(HISPANIC),
INS_MEDICAID = factor(INS_MEDICAID),
# Converting date columns
index_date = ymd(index_date),
birth_date = ymd(birth_date),
TRANS_CODE_ADATE = ymd(TRANS_CODE_ADATE),
TRANS_CODE_PROCDATE = ymd(TRANS_CODE_PROCDATE),
ENR_START = ymd(ENR_START),
ENR_END = ymd(ENR_END),
)
Cleaning the
Data
Existing categorical variables including KP site, transgender status,
natal sex, race/ethnicity, and insurance are re-coded for ease of
readability.
data <- data %>%
mutate(
site = case_when(
site == "GA" ~ "Georgia",
site == "NC" ~ "Northern California",
site == "SC" ~ "Southern California",
TRUE ~ as.character(site) # Keeps the original value if none of the above conditions are met
),
transgender = if_else(transgender == 1, "Yes", "No"),
sex_natal = case_when(
sex_natal == "M" ~ "Male",
sex_natal == "F" ~ "Female",
TRUE ~ as.character(sex_natal)
),
RACE1 = case_when(
RACE1 == "AS" ~ "Asian",
RACE1 == "BA" ~ "Black/African American",
RACE1 == "HP" ~ "Hispanic/Latino- Native Hawaiian or Pacific Islander",
RACE1 == "IN" ~ "American Indian or Alaskan Native",
RACE1 == "MU" ~ "Multi-racial",
RACE1 == "UN" ~ "Unknown",
RACE1 == "WH" ~ "White",
TRUE ~ as.character(RACE1)
),
RACE2 = case_when(
RACE2 == "AS" ~ "Asian",
RACE2 == "BA" ~ "Black/African American",
RACE2 == "IN" ~ "American Indian or Alaskan Native", # not in original data dictionary
RACE2 == "UN" ~ "Unknown",
RACE2 == "WH" ~ "White",
RACE2 == "OT" ~ "Other",
TRUE ~ as.character(RACE2)
),
HISPANIC = case_when(
HISPANIC == "N" ~ "No",
HISPANIC == "Y" ~ "Yes",
HISPANIC == "U" ~ "Unknown",
TRUE ~ as.character(HISPANIC)
),
INS_MEDICAID = case_when(
INS_MEDICAID == "Y" ~ "Medicaid",
is.na(INS_MEDICAID) ~ "Commercial",
TRUE ~ as.character(INS_MEDICAID)
)
)
# check coding
table(data$site, Original_data$site)
table(data$transgender, Original_data$transgender)
table(data$RACE1, Original_data$RACE1)
table(data$RACE2, Original_data$RACE2)
table(data$HISPANIC, Original_data$HISPANIC)
table(data$INS_MEDICAID, Original_data$INS_MEDICAID)
New variables are then created based on existing variables.
Transgender type (TM vs TF) is created based on natal sex. A binary GAS
variable is created based on the procedure variable. Age at index is
transformed into a new categorical variable. Finally, a categorical
poverty variable is created. …
confirm poverty variable is coded correctly
MG comment - I would combine race and
ethnicity into a single variable: 1. Non-Hispanic White; 2. Non-Hispanic
Black; 3. Non-Hispanic Asian/Hawaiian/Pacific Islander; 4. Hispanic; 5.
Other/unknown
This code also makes a race_ethnicity variable using original RACE1
and HISPANIC variables. (HISPANIC originally coded as Yes, No, and
Unknown. All with Yes are coded as Hispanic in new race/ethnicity
variable. Those coded as No or Unknown are now coded as Non-Hispanic X
based on race). The following RACE1 categories are consolidated into
“Other/Unknown”
- Hispanic/Latino- Native Hawaiian or Pacific Islander
- American Indian or Alaskan Native
- Multi-racial
- Unknown
# trans_type, age_at_index_group, poverty, poverty_cat
data <- data %>%
mutate(
trans_type = case_when(
sex_natal == "Male" ~ "Transfeminine",
sex_natal == "Female" ~ "Transmasculine",
TRUE ~ NA_character_
),
GAS_any = case_when(
procedure == 1 ~ "Yes",
procedure == 0 ~ "No",
TRUE ~ NA_character_
),
age_at_index_group = case_when(
age_at_index < 18 ~ "<18",
age_at_index >= 18 & age_at_index <= 34 ~ "18-34",
age_at_index >= 35 & age_at_index <= 64 ~ "35-64",
age_at_index >= 65 ~ "65+",
TRUE ~ NA_character_
),
poverty = pov_lt_50 + pov_50_74 + pov_75_99,
poverty_cat = case_when(
poverty < 0.1 ~ "<10%",
poverty >= 0.1 & poverty < 0.2 ~ "10-20%",
poverty >= 0.2 ~ ">20%",
TRUE ~ NA_character_
),
race_ethnicity = case_when(
HISPANIC == "Yes" ~ "Hispanic",
HISPANIC != "Yes" & RACE1 == "White" ~ "Non-Hispanic White",
HISPANIC != "Yes" & RACE1 == "Black/African American" ~ "Non-Hispanic Black",
HISPANIC != "Yes" & RACE1 == "Asian" ~ "Non-Hispanic Asian",
HISPANIC != "Yes" & RACE1 == "Hispanic/Latino- Native Hawaiian or Pacific Islander" ~ "Other/Unknown",
HISPANIC != "Yes" & RACE1 == "American Indian or Alaskan Native" ~ "Other/Unknown",
HISPANIC != "Yes" & RACE1 == "Multi-racial" ~ "Other/Unknown",
HISPANIC != "Yes" & RACE1 == "Unknown" ~ "Other/Unknown",
TRUE ~ NA_character_
)
)
# Set race_ethnicity to "Other/Unknown" if empty
data$race_ethnicity <- ifelse(is.na(data$race_ethnicity), "Other/Unknown", data$race_ethnicity)
# Check coding
# table(data$trans_type, data$sex_natal)
# table(data$procedure, data$GAS_any)
# table(data$poverty_cat)
# table(data$race_ethnicity, data$RACE1)
Confirm coding of new race/ethnicity variable by printing a cross tab
with the original RACE1 and HISPANIC variables.
### Check coding of new race/ethnicity variable
data %>%
select(RACE1, HISPANIC, race_ethnicity) %>%
mutate(
RACE1 = factor(RACE1, levels = c("White", "Black/African American", "Asian", "Hispanic/Latino- Native Hawaiian or Pacific Islander", "American Indian or Alaskan Native", "Multi-racial", "Unknown")),
HISPANIC = factor(HISPANIC, levels = c("Yes", "No", "Unknown")),
race_ethnicity = factor(race_ethnicity, levels = c("Hispanic", "Non-Hispanic White", "Non-Hispanic Black", "Non-Hispanic Asian", "Other/Unknown"))
) %>%
tbl_summary(by = race_ethnicity,
missing_text = "Missing") %>%
modify_spanning_header(c("stat_1", "stat_2", "stat_3", "stat_4", "stat_5") ~ "**New Race/Ethnicity Variable**") %>%
bold_labels()
| Characteristic |
New Race/Ethnicity Variable
|
| Hispanic, N = 3,503 |
Non-Hispanic White, N = 8,507 |
Non-Hispanic Black, N = 1,066 |
Non-Hispanic Asian, N = 1,506 |
Other/Unknown, N = 982 |
| RACE1 |
|
|
|
|
|
| White |
1,664 (48%) |
8,507 (100%) |
0 (0%) |
0 (0%) |
0 (0%) |
| Black/African American |
64 (1.8%) |
0 (0%) |
1,066 (100%) |
0 (0%) |
0 (0%) |
| Asian |
100 (2.9%) |
0 (0%) |
0 (0%) |
1,506 (100%) |
0 (0%) |
| Hispanic/Latino- Native Hawaiian or Pacific Islander |
13 (0.4%) |
0 (0%) |
0 (0%) |
0 (0%) |
122 (12%) |
| American Indian or Alaskan Native |
43 (1.2%) |
0 (0%) |
0 (0%) |
0 (0%) |
105 (11%) |
| Multi-racial |
14 (0.4%) |
0 (0%) |
0 (0%) |
0 (0%) |
1 (0.1%) |
| Unknown |
1,605 (46%) |
0 (0%) |
0 (0%) |
0 (0%) |
752 (77%) |
| Missing |
0 |
0 |
0 |
0 |
2 |
| HISPANIC |
|
|
|
|
|
| Yes |
3,503 (100%) |
0 (0%) |
0 (0%) |
0 (0%) |
0 (0%) |
| No |
0 (0%) |
1,784 (21%) |
258 (24%) |
376 (25%) |
94 (9.6%) |
| Unknown |
0 (0%) |
6,723 (79%) |
808 (76%) |
1,130 (75%) |
886 (90%) |
| Missing |
0 |
0 |
0 |
0 |
2 |
data %>%
select(race_ethnicity) %>%
tbl_summary(missing_text = "Missing")
| Characteristic |
N = 15,564 |
| race_ethnicity |
|
| Hispanic |
3,503 (23%) |
| Non-Hispanic Asian |
1,506 (9.7%) |
| Non-Hispanic Black |
1,066 (6.8%) |
| Non-Hispanic White |
8,507 (55%) |
| Other/Unknown |
982 (6.3%) |
Procedure Coding
A code lookup sheet is read in and used to create code description
and GAS type variables based on code. GAS type includes top, bottom, and
other. An additional GAS_surgery variable is
created to specifically identify gender-affirming surgeries (includes
top, bottom, bony facial surgeries; excludes soft tissue face/body
procedures (ie. blepharoplasties, rhytidectomies,
liposuction/lipectomies), fillers, and hair removal) Finally, a
binary GAS_bottom variable is created.
# Create list of all unique values for the CODE variable
code_list <- data.frame(unique(data$CODE))
# Read in procedure lookup table
code_lookup <- read_excel("Code_Details.xlsx")
# view(code_lookup)
# Ensure that the 'CODE' column in both data frames are of the same type
data$CODE <- as.character(data$CODE)
code_lookup$CODE <- as.character(code_lookup$CODE)
# Join the lookup table with main data frame using a left join
data <- data %>%
left_join(code_lookup, by = "CODE")
# New GAS_bottom variable
data <- data %>%
mutate(
GAS_bottom = case_when(
GAS_type == "Bottom" ~ "Yes",
!is.na(GAS_type) & GAS_type != "Bottom" ~ "No",
is.na(GAS_type) ~ NA_character_
)
)
# Set GAS_surgery to "No" if empty
unique(data$GAS_surgery)
data$GAS_surgery <- ifelse(is.na(data$GAS_surgery), "No", data$GAS_surgery)
unique(data$GAS_surgery)
table(data$GAS_surgery)
Creating additional
cohorts (TM/TF only, those who receive GAS/P)
MG comment - I am not convinced the
GAS categories of top/bottom/other are justifiable. To overcome this
limitation, I would suggest two options: 1. Focus on top GAS in the
TM cohort only (as we discussed before Christmas). 2. Use a binary
variable “any GAS vs. no GAS” (as you did in the regression
models)
Create new data frame with TF cohort only. Create another new data
frame with TM cohort only and new “top” variable indicating receipt of
top surgery. Finally, create a data frame for those who receive any
procedure.
data_TF <- data[data$trans_type == "Transfeminine", ] # new TF data frame
data_TM <- data[data$trans_type == "Transmasculine", ] # new TM data frame
data_GAS <- data[data$GAS_any == "Yes", ] # new cohort for those who received any GAS/procedure
data_TM <- data_TM %>% # add binary top variable for TM data frame
mutate(
top = case_when(
GAS_type == "Top" ~ "Yes",
!is.na(GAS_type) & GAS_type != "Top" ~ "No",
is.na(GAS_type) ~ NA_character_
)
)
# Set top to "No" if empty
unique(data_TM$top)
data_TM$top <- ifelse(is.na(data_TM$top), "No", data_TM$top)
unique(data_TM$top)
# Add label for new variable
label(data_TM$top) <- "Received Top Surgery"
#Check coding and label:
# table(data_TM$GAS_type, data_TM$top)
# cbind(label(data_TM))