library(tidyverse)
library(survey)
library(broom)
library(dplyr)
library(ggplot2)
# Load BRFSS data (pre-processed to RDS)
BRFSS2011 <- readRDS("C:/Users/63650/Downloads/BRFSS2011.RDS")
BRFSS2012 <- readRDS("C:/Users/63650/Downloads/BRFSS2012.RDS")
BRFSS2013 <- readRDS("C:/Users/63650/Downloads/BRFSS2013.RDS")
BRFSS2014 <- readRDS("C:/Users/63650/Downloads/BRFSS2014.RDS")
BRFSS2015 <- readRDS("C:/Users/63650/Downloads/BRFSS2015.RDS")
BRFSS2016 <- readRDS("C:/Users/63650/Downloads/BRFSS2016.RDS")
BRFSS2017 <- readRDS("C:/Users/63650/Downloads/BRFSS2017.RDS")
BRFSS2018 <- readRDS("C:/Users/63650/Downloads/BRFSS2018.RDS")
BRFSS2019 <- readRDS("C:/Users/63650/Downloads/BRFSS2019.RDS")
BRFSS2020 <- readRDS("C:/Users/63650/Downloads/BRFSS2020.RDS")
BRFSS2021 <- readRDS("C:/Users/63650/Downloads/BRFSS2021.RDS")
BRFSS2022 <- readRDS("C:/Users/63650/Downloads/BRFSS2022.RDS")
BRFSS2023 <- readRDS("C:/Users/63650/Downloads/BRFSS2023.RDS")
# -------------------------
# 2011 — SEX
# -------------------------
tx_sex_2011 <- BRFSS2011 %>%
filter(x.state == 48) %>%
select(x.psu, x.ststr, x.llcpwt, diabete3, sex) %>%
mutate(
diabete3 = ifelse(diabete3 %in% c(7, 9), NA, diabete3),
diabetes_dx = case_when(diabete3 == 1 ~ 1, diabete3 == 3 ~ 0, TRUE ~ NA_real_),
sex = ifelse(sex %in% c(7, 9), NA, sex)
) %>%
filter(!is.na(diabetes_dx), !is.na(sex)) %>%
mutate(sex = factor(sex)) # 1=Male, 2=Female; baseline is 1 by default
des_sex_2011 <- svydesign(ids=~x.psu, strata=~x.ststr, weights=~x.llcpwt, data=tx_sex_2011, nest=TRUE)
out_sex_2011 <- broom::tidy(svyglm(diabetes_dx ~ sex, design=des_sex_2011, family=quasibinomial()), conf.int=FALSE) %>%
filter(term != "(Intercept)") %>% mutate(year=2011, predictor="sex")
# -------------------------
# 2012 — SEX
# -------------------------
tx_sex_2012 <- BRFSS2012 %>%
filter(x.state == 48) %>%
select(x.psu, x.ststr, x.llcpwt, diabete3, sex) %>%
mutate(
diabete3 = ifelse(diabete3 %in% c(7, 9), NA, diabete3),
diabetes_dx = case_when(diabete3 == 1 ~ 1, diabete3 == 3 ~ 0, TRUE ~ NA_real_),
sex = ifelse(sex %in% c(7, 9), NA, sex)
) %>%
filter(!is.na(diabetes_dx), !is.na(sex)) %>%
mutate(sex = factor(sex))
des_sex_2012 <- svydesign(ids=~x.psu, strata=~x.ststr, weights=~x.llcpwt, data=tx_sex_2012, nest=TRUE)
out_sex_2012 <- broom::tidy(svyglm(diabetes_dx ~ sex, design=des_sex_2012, family=quasibinomial()), conf.int=FALSE) %>%
filter(term != "(Intercept)") %>% mutate(year=2012, predictor="sex")
# -------------------------
# 2013 — SEX
# -------------------------
tx_sex_2013 <- BRFSS2013 %>%
filter(x.state == 48) %>%
select(x.psu, x.ststr, x.llcpwt, diabete3, sex) %>%
mutate(
diabete3 = ifelse(diabete3 %in% c(7, 9), NA, diabete3),
diabetes_dx = case_when(diabete3 == 1 ~ 1, diabete3 == 3 ~ 0, TRUE ~ NA_real_),
sex = ifelse(sex %in% c(7, 9), NA, sex)
) %>%
filter(!is.na(diabetes_dx), !is.na(sex)) %>%
mutate(sex = factor(sex))
des_sex_2013 <- svydesign(ids=~x.psu, strata=~x.ststr, weights=~x.llcpwt, data=tx_sex_2013, nest=TRUE)
out_sex_2013 <- broom::tidy(svyglm(diabetes_dx ~ sex, design=des_sex_2013, family=quasibinomial()), conf.int=FALSE) %>%
filter(term != "(Intercept)") %>% mutate(year=2013, predictor="sex")
# -------------------------
# 2014 — SEX
# -------------------------
tx_sex_2014 <- BRFSS2014 %>%
filter(x.state == 48) %>%
select(x.psu, x.ststr, x.llcpwt, diabete3, sex) %>%
mutate(
diabete3 = ifelse(diabete3 %in% c(7, 9), NA, diabete3),
diabetes_dx = case_when(diabete3 == 1 ~ 1, diabete3 == 3 ~ 0, TRUE ~ NA_real_),
sex = ifelse(sex %in% c(7, 9), NA, sex)
) %>%
filter(!is.na(diabetes_dx), !is.na(sex)) %>%
mutate(sex = factor(sex))
des_sex_2014 <- svydesign(ids=~x.psu, strata=~x.ststr, weights=~x.llcpwt, data=tx_sex_2014, nest=TRUE)
out_sex_2014 <- broom::tidy(svyglm(diabetes_dx ~ sex, design=des_sex_2014, family=quasibinomial()), conf.int=FALSE) %>%
filter(term != "(Intercept)") %>% mutate(year=2014, predictor="sex")
# -------------------------
# 2015 — SEX
# -------------------------
tx_sex_2015 <- BRFSS2015 %>%
filter(x.state == 48) %>%
select(x.psu, x.ststr, x.llcpwt, diabete3, sex) %>%
mutate(
diabete3 = ifelse(diabete3 %in% c(7, 9), NA, diabete3),
diabetes_dx = case_when(diabete3 == 1 ~ 1, diabete3 == 3 ~ 0, TRUE ~ NA_real_),
sex = ifelse(sex %in% c(7, 9), NA, sex)
) %>%
filter(!is.na(diabetes_dx), !is.na(sex)) %>%
mutate(sex = factor(sex))
des_sex_2015 <- svydesign(ids=~x.psu, strata=~x.ststr, weights=~x.llcpwt, data=tx_sex_2015, nest=TRUE)
out_sex_2015 <- broom::tidy(svyglm(diabetes_dx ~ sex, design=des_sex_2015, family=quasibinomial()), conf.int=FALSE) %>%
filter(term != "(Intercept)") %>% mutate(year=2015, predictor="sex")
# -------------------------
# 2016 — SEX
# -------------------------
tx_sex_2016 <- BRFSS2016 %>%
filter(x.state == 48) %>%
select(x.psu, x.ststr, x.llcpwt, diabete3, sex) %>%
mutate(
diabete3 = ifelse(diabete3 %in% c(7, 9), NA, diabete3),
diabetes_dx = case_when(diabete3 == 1 ~ 1, diabete3 == 3 ~ 0, TRUE ~ NA_real_),
sex = ifelse(sex %in% c(7, 9), NA, sex)
) %>%
filter(!is.na(diabetes_dx), !is.na(sex)) %>%
mutate(sex = factor(sex))
des_sex_2016 <- svydesign(ids=~x.psu, strata=~x.ststr, weights=~x.llcpwt, data=tx_sex_2016, nest=TRUE)
out_sex_2016 <- broom::tidy(svyglm(diabetes_dx ~ sex, design=des_sex_2016, family=quasibinomial()), conf.int=FALSE) %>%
filter(term != "(Intercept)") %>% mutate(year=2016, predictor="sex")
# -------------------------
# 2017 — SEX
# -------------------------
tx_sex_2017 <- BRFSS2017 %>%
filter(x.state == 48) %>%
select(x.psu, x.ststr, x.llcpwt, diabete3, sex) %>%
mutate(
diabete3 = ifelse(diabete3 %in% c(7, 9), NA, diabete3),
diabetes_dx = case_when(diabete3 == 1 ~ 1, diabete3 == 3 ~ 0, TRUE ~ NA_real_),
sex = ifelse(sex %in% c(7, 9), NA, sex)
) %>%
filter(!is.na(diabetes_dx), !is.na(sex)) %>%
mutate(sex = factor(sex))
des_sex_2017 <- svydesign(ids=~x.psu, strata=~x.ststr, weights=~x.llcpwt, data=tx_sex_2017, nest=TRUE)
out_sex_2017 <- broom::tidy(svyglm(diabetes_dx ~ sex, design=des_sex_2017, family=quasibinomial()), conf.int=FALSE) %>%
filter(term != "(Intercept)") %>% mutate(year=2017, predictor="sex")
# -------------------------
# 2018 — SEX
# -------------------------
tx_sex_2018 <- BRFSS2018 %>%
filter(x.state == 48) %>%
select(x.psu, x.ststr, x.llcpwt, diabete3, sex1) %>%
rename(sex = sex1) %>% # harmonize name
mutate(
diabete3 = ifelse(diabete3 %in% c(7, 9), NA, diabete3),
diabetes_dx = case_when(
diabete3 == 1 ~ 1,
diabete3 == 3 ~ 0,
TRUE ~ NA_real_
),
sex = ifelse(sex %in% c(7, 9), NA, sex)
) %>%
filter(!is.na(diabetes_dx), !is.na(sex)) %>%
mutate(sex = factor(sex))
des_sex_2018 <- svydesign(
ids = ~x.psu,
strata = ~x.ststr,
weights = ~x.llcpwt,
data = tx_sex_2018,
nest = TRUE
)
out_sex_2018 <- broom::tidy(
svyglm(diabetes_dx ~ sex, design = des_sex_2018, family = quasibinomial()),
conf.int = FALSE
) %>%
filter(term != "(Intercept)") %>%
mutate(year = 2018, predictor = "sex")
# -------------------------
# NOTE:
# Beginning in 2019, BRFSS renames the diabetes variable from DIABETE3 to DIABETE4.
# To preserve consistency across years, DIABETE4 is renamed to DIABETE3
# before any recoding or regression is performed.
# 2019 — SEX
# -------------------------
tx_sex_2019 <- BRFSS2019 %>%
filter(x.state == 48) %>%
select(x.psu, x.ststr, x.llcpwt, diabete4, sexvar) %>%
rename(diabete3 = diabete4) %>%
rename(sex = sexvar) %>% # harmonize name
mutate(
diabete3 = ifelse(diabete3 %in% c(7, 9), NA, diabete3),
diabetes_dx = case_when(diabete3 == 1 ~ 1, diabete3 == 3 ~ 0, TRUE ~ NA_real_),
sex = ifelse(sex %in% c(7, 9), NA, sex)
) %>%
filter(!is.na(diabetes_dx), !is.na(sex)) %>%
mutate(sex = factor(sex))
des_sex_2019 <- svydesign(ids=~x.psu, strata=~x.ststr, weights=~x.llcpwt,
data=tx_sex_2019, nest=TRUE)
out_sex_2019 <- broom::tidy(
svyglm(diabetes_dx ~ sex, design=des_sex_2019, family=quasibinomial()),
conf.int=FALSE
) %>%
filter(term != "(Intercept)") %>%
mutate(year=2019, predictor="sex")
# -------------------------
# NOTE:
# BRFSS 2020 Texas analytic filtering can create singleton strata (a stratum with only one PSU at stage 1),
# which causes svydesign/svyglm to error: "Stratum (...) has only one PSU at stage 1".
# For 2020 SEX, the singleton strata observed are 481012, 481052, 481062, 481092, and 481172,
# each containing exactly one observation after filtering.
# To proceed, these observations are dropped from the 2020 analytic dataset ONLY.
# 2020 — SEX
# -------------------------
tx_sex_2020 <- BRFSS2020 %>%
filter(x.state == 48) %>%
select(x.psu, x.ststr, x.llcpwt, diabete4, sexvar) %>%
rename(diabete3 = diabete4) %>%
rename(sex = sexvar) %>% # harmonize name
mutate(
diabete3 = ifelse(diabete3 %in% c(7, 9), NA, diabete3),
diabetes_dx = case_when(diabete3 == 1 ~ 1, diabete3 == 3 ~ 0, TRUE ~ NA_real_),
sex = ifelse(sex %in% c(7, 9), NA, sex)
) %>%
filter(!is.na(diabetes_dx), !is.na(sex)) %>%
mutate(sex = factor(sex)) %>%
filter(!x.ststr %in% c(481012, 481052, 481062, 481092, 481172))
des_sex_2020 <- svydesign(
ids = ~x.psu, strata = ~x.ststr, weights = ~x.llcpwt,
data = tx_sex_2020, nest = TRUE
)
out_sex_2020 <- broom::tidy(
svyglm(diabetes_dx ~ sex, design = des_sex_2020, family = quasibinomial()),
conf.int = FALSE
) %>%
filter(term != "(Intercept)") %>%
mutate(year = 2020, predictor = "sex")
# -------------------------
# NOTE:
# BRFSS 2021 TX contains strata with only one PSU at stage 1 after analytic filtering,
# which causes svydesign/svyglm to error: "Stratum (...) has only one PSU at stage 1".
# These strata (482099, 482149, 482179, 482189) each contain exactly one observation.
# To proceed, these 4 observations are dropped from the 2021 SEX analytic dataset ONLY:
# 482099, 482149, 482179, 482189.
# 2021 — SEX
# -------------------------
tx_sex_2021 <- BRFSS2021 %>%
filter(x.state == 48) %>%
select(x.psu, x.ststr, x.llcpwt, diabete4, sexvar) %>%
rename(diabete3 = diabete4) %>%
rename(sex = sexvar) %>% # harmonize name
mutate(
diabete3 = ifelse(diabete3 %in% c(7, 9), NA, diabete3),
diabetes_dx = case_when(diabete3 == 1 ~ 1, diabete3 == 3 ~ 0, TRUE ~ NA_real_),
sex = ifelse(sex %in% c(7, 9), NA, sex)
) %>%
filter(!is.na(diabetes_dx), !is.na(sex)) %>%
mutate(sex = factor(sex))
tx_sex_2021 <- tx_sex_2021 %>%
filter(!x.ststr %in% c(482099, 482149, 482179, 482189))
des_sex_2021 <- svydesign(
ids = ~x.psu, strata = ~x.ststr, weights = ~x.llcpwt,
data = tx_sex_2021, nest = TRUE
)
out_sex_2021 <- broom::tidy(
svyglm(diabetes_dx ~ sex, design = des_sex_2021, family = quasibinomial()),
conf.int = FALSE
) %>%
filter(term != "(Intercept)") %>%
mutate(year = 2021, predictor = "sex")
# -------------------------
# BRFSS TX can contain singleton strata after analytic filtering
# which causes svydesign/svyglm to error: "Stratum (...) has only one PSU at stage 1".
# To prevent failures, singleton strata are detected and dropped automatically for 2022 SEX.
# Specifically, any stratum with fewer than 2 distinct PSUs is removed before svydesign.
# 2022 — SEX
# -------------------------
tx_sex_2022 <- BRFSS2022 %>%
filter(x.state == 48) %>%
select(x.psu, x.ststr, x.llcpwt, diabete4, sexvar) %>%
rename(diabete3 = diabete4) %>%
rename(sex = sexvar) %>% # harmonize name
mutate(
diabete3 = ifelse(diabete3 %in% c(7, 9), NA, diabete3),
diabetes_dx = case_when(diabete3 == 1 ~ 1, diabete3 == 3 ~ 0, TRUE ~ NA_real_),
sex = ifelse(sex %in% c(7, 9), NA, sex)
) %>%
filter(!is.na(diabetes_dx), !is.na(sex)) %>%
mutate(sex = factor(sex))
tx_sex_2022 <- tx_sex_2022 %>%
group_by(x.ststr) %>%
mutate(
n_obs = n(),
n_psu = n_distinct(x.psu)
) %>%
ungroup() %>%
filter(n_obs >= 10, n_psu >= 2) %>%
select(-n_obs, -n_psu)
des_sex_2022 <- svydesign(
ids = ~x.psu,
strata = ~x.ststr,
weights = ~x.llcpwt,
data = tx_sex_2022,
nest = TRUE
)
out_sex_2022 <- broom::tidy(
svyglm(diabetes_dx ~ sex, design = des_sex_2022, family = quasibinomial()),
conf.int = FALSE
) %>%
filter(term != "(Intercept)") %>%
mutate(year = 2022, predictor = "sex")
# -------------------------
# 2023 — SEX
# -------------------------
tx_sex_2023 <- BRFSS2023 %>%
filter(x.state == 48) %>%
select(x.psu, x.ststr, x.llcpwt, diabete4, sexvar) %>%
rename(diabete3 = diabete4) %>%
rename(sex = sexvar) %>% # harmonize name
mutate(
diabete3 = ifelse(diabete3 %in% c(7, 9), NA, diabete3),
diabetes_dx = case_when(
diabete3 == 1 ~ 1,
diabete3 == 3 ~ 0,
TRUE ~ NA_real_
),
sex = ifelse(sex %in% c(7, 9), NA, sex)
) %>%
filter(!is.na(diabetes_dx), !is.na(sex)) %>%
mutate(sex = factor(sex))
# NOTE:
# Remove strata that cause svydesign/svyglm failures.
# Any stratum with fewer than 2 PSUs or fewer than 10 observations is dropped.
tx_sex_2023 <- tx_sex_2023 %>%
group_by(x.ststr) %>%
mutate(
n_obs = n(),
n_psu = n_distinct(x.psu)
) %>%
ungroup() %>%
filter(n_obs >= 10, n_psu >= 2) %>%
select(-n_obs, -n_psu)
des_sex_2023 <- svydesign(
ids = ~x.psu,
strata = ~x.ststr,
weights = ~x.llcpwt,
data = tx_sex_2023,
nest = TRUE
)
out_sex_2023 <- broom::tidy(
svyglm(diabetes_dx ~ sex, design = des_sex_2023, family = quasibinomial()),
conf.int = FALSE
) %>%
filter(term != "(Intercept)") %>%
mutate(year = 2023, predictor = "sex")
# -------------------------
# Combine + print coeff and p-value by year for SEX
# -------------------------
sex_yearly <- bind_rows(
out_sex_2011, out_sex_2012, out_sex_2013, out_sex_2014, out_sex_2015, out_sex_2016, out_sex_2017,
out_sex_2018, out_sex_2019, out_sex_2020, out_sex_2021, out_sex_2022, out_sex_2023
) %>%
select(year, predictor, term, estimate, p.value)
sex_yearly
# Pool 2012–2014 (pre-ACA)
pool_sex_2012_2014 <- bind_rows(
tx_sex_2012 %>% select(x.psu,x.ststr,x.llcpwt,diabetes_dx,sex) %>%
mutate(x.psu=as.numeric(x.psu), x.ststr=as.numeric(x.ststr), x.llcpwt=as.numeric(x.llcpwt),
diabetes_dx=as.numeric(diabetes_dx), sex=as.character(sex)),
tx_sex_2013 %>% select(x.psu,x.ststr,x.llcpwt,diabetes_dx,sex) %>%
mutate(x.psu=as.numeric(x.psu), x.ststr=as.numeric(x.ststr), x.llcpwt=as.numeric(x.llcpwt),
diabetes_dx=as.numeric(diabetes_dx), sex=as.character(sex)),
tx_sex_2014 %>% select(x.psu,x.ststr,x.llcpwt,diabetes_dx,sex) %>%
mutate(x.psu=as.numeric(x.psu), x.ststr=as.numeric(x.ststr), x.llcpwt=as.numeric(x.llcpwt),
diabetes_dx=as.numeric(diabetes_dx), sex=as.character(sex))
) %>% mutate(sex = factor(sex, levels = c("1","2"))) # 1=Male (baseline), 2=Female
des_sex_2012_2014 <- svydesign(ids=~x.psu, strata=~x.ststr, weights=~x.llcpwt, data=pool_sex_2012_2014, nest=TRUE)
out_sex_2012_2014 <- broom::tidy(svyglm(diabetes_dx ~ sex, design=des_sex_2012_2014, family=quasibinomial()), conf.int=FALSE)
strong_sex_2012_2014 <- out_sex_2012_2014 %>% filter(term!="(Intercept)") %>% slice_min(p.value, n=1, with_ties=FALSE)
strong_sex_2012_2014
# Pool 2014–2016 (early ACA)
pool_sex_2014_2016 <- bind_rows(
tx_sex_2014 %>% select(x.psu,x.ststr,x.llcpwt,diabetes_dx,sex) %>%
mutate(x.psu=as.numeric(x.psu), x.ststr=as.numeric(x.ststr), x.llcpwt=as.numeric(x.llcpwt),
diabetes_dx=as.numeric(diabetes_dx), sex=as.character(sex)),
tx_sex_2015 %>% select(x.psu,x.ststr,x.llcpwt,diabetes_dx,sex) %>%
mutate(x.psu=as.numeric(x.psu), x.ststr=as.numeric(x.ststr), x.llcpwt=as.numeric(x.llcpwt),
diabetes_dx=as.numeric(diabetes_dx), sex=as.character(sex)),
tx_sex_2016 %>% select(x.psu,x.ststr,x.llcpwt,diabetes_dx,sex) %>%
mutate(x.psu=as.numeric(x.psu), x.ststr=as.numeric(x.ststr), x.llcpwt=as.numeric(x.llcpwt),
diabetes_dx=as.numeric(diabetes_dx), sex=as.character(sex))
) %>% mutate(sex = factor(sex, levels = c("1","2"))) # 1=Male (baseline), 2=Female
des_sex_2014_2016 <- svydesign(ids=~x.psu, strata=~x.ststr, weights=~x.llcpwt, data=pool_sex_2014_2016, nest=TRUE)
out_sex_2014_2016 <- broom::tidy(svyglm(diabetes_dx ~ sex, design=des_sex_2014_2016, family=quasibinomial()), conf.int=FALSE)
strong_sex_2014_2016 <- out_sex_2014_2016 %>% filter(term!="(Intercept)") %>% slice_min(p.value, n=1, with_ties=FALSE)
strong_sex_2014_2016
# Pool 2020–2023 (COVID era)
pool_sex_2020_2023 <- bind_rows(
tx_sex_2020 %>% select(x.psu,x.ststr,x.llcpwt,diabetes_dx,sex) %>%
mutate(x.psu=as.numeric(x.psu), x.ststr=as.numeric(x.ststr), x.llcpwt=as.numeric(x.llcpwt),
diabetes_dx=as.numeric(diabetes_dx), sex=as.character(sex)),
tx_sex_2021 %>% select(x.psu,x.ststr,x.llcpwt,diabetes_dx,sex) %>%
mutate(x.psu=as.numeric(x.psu), x.ststr=as.numeric(x.ststr), x.llcpwt=as.numeric(x.llcpwt),
diabetes_dx=as.numeric(diabetes_dx), sex=as.character(sex)),
tx_sex_2022 %>% select(x.psu,x.ststr,x.llcpwt,diabetes_dx,sex) %>%
mutate(x.psu=as.numeric(x.psu), x.ststr=as.numeric(x.ststr), x.llcpwt=as.numeric(x.llcpwt),
diabetes_dx=as.numeric(diabetes_dx), sex=as.character(sex)),
tx_sex_2023 %>% select(x.psu,x.ststr,x.llcpwt,diabetes_dx,sex) %>%
mutate(x.psu=as.numeric(x.psu), x.ststr=as.numeric(x.ststr), x.llcpwt=as.numeric(x.llcpwt),
diabetes_dx=as.numeric(diabetes_dx), sex=as.character(sex))
) %>% mutate(sex = factor(sex, levels = c("1","2"))) # 1=Male (baseline), 2=Female
des_sex_2020_2023 <- svydesign(ids=~x.psu, strata=~x.ststr, weights=~x.llcpwt, data=pool_sex_2020_2023, nest=TRUE)
out_sex_2020_2023 <- broom::tidy(svyglm(diabetes_dx ~ sex, design=des_sex_2020_2023, family=quasibinomial()), conf.int=FALSE)
strong_sex_2020_2023 <- out_sex_2020_2023 %>% filter(term!="(Intercept)") %>% slice_min(p.value, n=1, with_ties=FALSE)
strong_sex_2020_2023
# Combine all years
sex_all <- bind_rows(
out_sex_2011 %>% mutate(year = 2011),
out_sex_2012 %>% mutate(year = 2012),
out_sex_2013 %>% mutate(year = 2013),
out_sex_2014 %>% mutate(year = 2014),
out_sex_2015 %>% mutate(year = 2015),
out_sex_2016 %>% mutate(year = 2016),
out_sex_2017 %>% mutate(year = 2017),
out_sex_2018 %>% mutate(year = 2018),
out_sex_2019 %>% mutate(year = 2019),
out_sex_2020 %>% mutate(year = 2020),
out_sex_2021 %>% mutate(year = 2021),
out_sex_2022 %>% mutate(year = 2022),
out_sex_2023 %>% mutate(year = 2023)
)
# Keep only non-intercept coefficients, then pick the single smallest p-value per year (sex has 1 coef/year)
sex_strongest <- sex_all %>%
filter(term != "(Intercept)") %>%
group_by(year) %>%
slice_min(order_by = p.value, n = 1, with_ties = FALSE) %>%
ungroup()
# Build sex_period_table from pooled strongest objects (from subsets_sex block)
sex_period_table <- bind_rows(
strong_sex_2012_2014 %>% mutate(period = "2012–2014"),
strong_sex_2014_2016 %>% mutate(period = "2014–2016"),
strong_sex_2020_2023 %>% mutate(period = "2020–2023")
)
# Pooled-period points (from sex_period_table)
sex_pooled_plot <- sex_period_table %>%
mutate(
year = c(2013, 2015, 2021.5), # midpoints: 2012–14, 2014–16, 2020–23
label = term
)
# Plot
ggplot(sex_strongest, aes(x = year, y = estimate)) +
geom_line() +
geom_point(size = 2.5) +
# Error bars for yearly points
geom_errorbar(
aes(
ymin = estimate - 1.96 * std.error,
ymax = estimate + 1.96 * std.error
),
width = 0.2,
linewidth = 0.6
) +
geom_text(
aes(label = term),
vjust = -1.2,
size = 2.8,
check_overlap = TRUE
) +
geom_vline(xintercept = 2014, linetype = "dashed", color = "#1f77b4", linewidth = 0.8) +
geom_vline(xintercept = 2020, linetype = "dashed", color = "#d62728", linewidth = 0.8) +
annotate(
"text",
x = 2014,
y = max(sex_strongest$estimate, na.rm = TRUE),
label = "ACA",
color = "#1f77b4",
angle = 90,
hjust = -1.0,
vjust = -0.6,
size = 4
) +
annotate(
"text",
x = 2020,
y = max(sex_strongest$estimate, na.rm = TRUE),
label = "COVID",
color = "#d62728",
angle = 90,
hjust = -0.3,
vjust = -0.6,
size = 4
) +
scale_x_continuous(breaks = 2011:2023) +
labs(
x = "Year",
y = "Log-odds coefficient (Female vs Male)",
title = "Most Statistically Significant Sex Coefficient by Year (Texas BRFSS)",
subtitle = "Dashed lines mark 2014 (ACA-era onset) and 2020 (COVID-era onset)"
) +
theme_minimal() +
theme(
axis.text.x = element_text(size = 12, face = "bold", angle = 45, hjust = 1),
axis.text.y = element_text(size = 12, face = "bold"),
axis.title.x = element_text(size = 13, face = "bold"),
axis.title.y = element_text(size = 13, face = "bold"),
plot.title = element_text(face = "bold")
) +
# Pooled-era strongest coefficients
geom_point(
data = sex_pooled_plot,
aes(x = year, y = estimate),
color = "#2E8B57",
size = 2.5
) +
geom_errorbar(
data = sex_pooled_plot,
aes(
x = year,
ymin = estimate - 1.96 * std.error,
ymax = estimate + 1.96 * std.error
),
width = 0.4,
linewidth = 0.8,
color = "#2E8B57"
) +
geom_text(
data = sex_pooled_plot,
aes(x = year, y = estimate, label = label, vjust = ifelse(year == 2021.5, -1.2, 1.4)),
color = "#2E8B57",
size = 3
) +
# Green brackets showing pooled-year ranges
annotate(
"segment",
x = c(2012, 2014, 2020),
xend = c(2014, 2016, 2023),
y = min(sex_strongest$estimate, na.rm = TRUE) - 0.5,
yend = min(sex_strongest$estimate, na.rm = TRUE) - 0.5,
linewidth = 1,
color = "#2E8B57"
) +
annotate(
"segment",
x = c(2012, 2014, 2020),
xend = c(2012, 2014, 2020),
y = min(sex_strongest$estimate, na.rm = TRUE) - 0.6,
yend = min(sex_strongest$estimate, na.rm = TRUE) - 0.4,
linewidth = 1,
color = "#2E8B57"
) +
annotate(
"segment",
x = c(2014, 2016, 2023),
xend = c(2014, 2016, 2023),
y = min(sex_strongest$estimate, na.rm = TRUE) - 0.6,
yend = min(sex_strongest$estimate, na.rm = TRUE) - 0.4,
linewidth = 1,
color = "#2E8B57"
)