This comprehensive analysis examines health disparities in the NHANES dataset, focusing on:
# Quick summary of key demographics
summary_data <- NHANES %>%
select(Age, Gender, Race1, BMI) %>%
filter(!is.na(Age), !is.na(Gender), !is.na(Race1), !is.na(BMI)) %>%
group_by(Gender, Race1) %>%
summarize(
Count = n(),
Mean_Age = mean(Age),
Median_Age = median(Age),
Mean_BMI = mean(BMI),
Median_BMI = median(BMI),
.groups = 'drop'
)
print(summary_data)
## # A tibble: 10 × 7
## Gender Race1 Count Mean_Age Median_Age Mean_BMI Median_BMI
## <fct> <fct> <int> <dbl> <dbl> <dbl> <dbl>
## 1 female Black 594 35.9 34 29.8 29.2
## 2 female Hispanic 310 32.8 30 26.3 25.7
## 3 female Mexican 427 28.5 26 26.4 25.9
## 4 female White 3112 41.8 43 26.6 25.3
## 5 female Other 398 34.2 33 24.0 23.2
## 6 male Black 565 33.7 30 26.3 25.4
## 7 male Hispanic 279 32.8 31 26.4 26.9
## 8 male Mexican 531 28.9 26 26.6 27.1
## 9 male White 3038 39.7 40 26.8 26.5
## 10 male Other 380 31.9 29 24.8 24.4
# Frequency of age ranges
age_distribution <- NHANES %>%
group_by(AgeDecade) %>%
summarise(Count = n()) %>%
mutate(Percent = round(Count / sum(Count) * 100, 1))
print(age_distribution)
## # A tibble: 9 × 3
## AgeDecade Count Percent
## <fct> <int> <dbl>
## 1 " 0-9" 1391 13.9
## 2 " 10-19" 1374 13.7
## 3 " 20-29" 1356 13.6
## 4 " 30-39" 1338 13.4
## 5 " 40-49" 1398 14
## 6 " 50-59" 1304 13
## 7 " 60-69" 919 9.2
## 8 " 70+" 587 5.9
## 9 <NA> 333 3.3
# By sex
nhanes_male <- subset(NHANES, Gender == "male")
# By age decade
nhanes_30_39 <- subset(NHANES, trimws(as.character(AgeDecade)) == "30-39")
# By race
nhanes_white <- subset(NHANES, Race1 %in% "White")
# Create custom age groups
NHANES$AgeGroup <- ifelse(NHANES$Age %in% c("20-29", "30-39", "40-49"), "Young", "Older")
NHANES %>%
group_by(Gender) %>%
summarize(
Mean_BMI = mean(BMI, na.rm = TRUE),
Mean_Cholesterol = mean(TotChol, na.rm = TRUE),
count = n()
)
## # A tibble: 2 × 4
## Gender Mean_BMI Mean_Cholesterol count
## <fct> <dbl> <dbl> <int>
## 1 female 26.8 4.96 5020
## 2 male 26.5 4.80 4980
NHANES %>%
group_by(AgeDecade) %>%
summarise(
Mean_BMI = mean(BMI, na.rm = TRUE),
Mean_Cholesterol = mean(TotChol, na.rm = TRUE),
Count = n()
)
## # A tibble: 9 × 4
## AgeDecade Mean_BMI Mean_Cholesterol Count
## <fct> <dbl> <dbl> <int>
## 1 " 0-9" 17.1 4.09 1391
## 2 " 10-19" 23.3 4.08 1374
## 3 " 20-29" 27.5 4.64 1356
## 4 " 30-39" 29.2 4.96 1338
## 5 " 40-49" 28.9 5.27 1398
## 6 " 50-59" 29.2 5.38 1304
## 7 " 60-69" 29.6 5.20 919
## 8 " 70+" 29.2 5.05 587
## 9 <NA> 26.8 4.82 333
# Create detailed summary table
summary_table <- NHANES %>%
group_by(Gender, Race1, AgeDecade) %>%
summarize(
Mean_BMI = mean(BMI, na.rm = TRUE),
SD_BMI = sd(BMI, na.rm = TRUE),
Mean_Cholesterol = mean(TotChol, na.rm = TRUE),
SD_Cholesterol = sd(TotChol, na.rm = TRUE),
count = n(),
.groups = 'drop'
)
print(head(summary_table, 20))
## # A tibble: 20 × 8
## Gender Race1 AgeDecade Mean_BMI SD_BMI Mean_Cholesterol SD_Cholesterol count
## <fct> <fct> <fct> <dbl> <dbl> <dbl> <dbl> <int>
## 1 female Black " 0-9" 17.8 3.76 4.06 0.666 86
## 2 female Black " 10-19" 25.0 7.04 4.08 0.776 86
## 3 female Black " 20-29" 31.3 8.70 4.56 0.840 101
## 4 female Black " 30-39" 33.5 8.96 4.95 0.882 94
## 5 female Black " 40-49" 34.2 9.57 5.13 0.879 69
## 6 female Black " 50-59" 33.2 10.8 5.29 0.956 77
## 7 female Black " 60-69" 32.6 7.65 5.20 1.07 58
## 8 female Black " 70+" 30.9 7.59 5.27 1.02 34
## 9 female Black <NA> 29.2 8.45 5.16 0.956 9
## 10 female Hispa… " 0-9" 17.9 3.83 4.26 0.567 48
## 11 female Hispa… " 10-19" 23.5 5.16 4.08 0.574 54
## 12 female Hispa… " 20-29" 26.0 4.57 4.65 0.806 56
## 13 female Hispa… " 30-39" 29.6 8.18 4.84 0.953 51
## 14 female Hispa… " 40-49" 27.6 4.88 5.12 0.927 48
## 15 female Hispa… " 50-59" 31.1 7.76 5.46 1.24 25
## 16 female Hispa… " 60-69" 31.0 5.48 5.47 1.21 23
## 17 female Hispa… " 70+" 30.3 7.85 5.46 0.909 11
## 18 female Hispa… <NA> 24.0 2.25 5.84 1.01 4
## 19 female Mexic… " 0-9" 18.4 3.86 4.04 0.486 100
## 20 female Mexic… " 10-19" 22.9 5.73 4.08 0.593 90
# Export summary table
write_csv(summary_table, "summarytable.csv")
# Create tidy version for visualization
tidy_summary <- summary_table %>%
pivot_longer(
cols = c(Mean_BMI, SD_BMI, Mean_Cholesterol, SD_Cholesterol),
names_to = "Measure",
values_to = "value"
)
write_csv(tidy_summary, "tidysummary.csv")
# Independent two-sample t-test
t_test_result <- t.test(BMI ~ Gender, data = NHANES)
print(t_test_result)
##
## Welch Two Sample t-test
##
## data: BMI by Gender
## t = 1.4981, df = 9452.6, p-value = 0.1341
## alternative hypothesis: true difference in means between group female and group male is not equal to 0
## 95 percent confidence interval:
## -0.06939849 0.51940903
## sample estimates:
## mean in group female mean in group male
## 26.77208 26.54707
# Visualization
boxplot(BMI ~ Gender, data = NHANES,
main = "BMI Comparison by Gender",
xlab = "Gender", ylab = "BMI",
col = c("lightblue", "lightpink"))
Interpretation: The t-test shows no significant difference in BMI between males and females (p = 0.1341).
# ANOVA for total cholesterol across racial groups
anova_result <- aov(TotChol ~ Race1, data = NHANES)
summary(anova_result)
## Df Sum Sq Mean Sq F value Pr(>F)
## Race1 4 115 28.793 25.17 <2e-16 ***
## Residuals 8469 9687 1.144
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 1526 observations deleted due to missingness
# Summarize average BP by race
bp_summary <- NHANES %>%
filter(!is.na(BPSysAve), !is.na(BPDiaAve), !is.na(Race1)) %>%
group_by(Race1) %>%
summarise(
Systolic = mean(BPSysAve, na.rm = TRUE),
Diastolic = mean(BPDiaAve, na.rm = TRUE)
) %>%
pivot_longer(cols = c(Systolic, Diastolic), names_to = "Type", values_to = "BP")
# Create grouped bar plot
ggplot(bp_summary, aes(x = Race1, y = BP, fill = Type)) +
geom_bar(stat = "identity", position = "dodge") +
labs(
title = "Average Blood Pressure by Race",
x = "Race",
y = "Blood Pressure (mm Hg)",
fill = "Type"
) +
theme_minimal()
# Create hypertension variable
NHANES <- NHANES %>%
mutate(Hypertension = case_when(
BPSysAve >= 130 | BPDiaAve >= 80 ~ 1,
TRUE ~ 0
))
# Convert variables to factors
NHANES <- NHANES %>%
mutate(across(c(Gender, Race1, Diabetes, HealthGen, PhysActive, Hypertension), as.factor))
# Prepare clean dataset for hypertension model
nhanes_htn_clean <- NHANES %>%
select(Hypertension, Age, BMI, PhysActive) %>%
na.omit()
# Fit logistic regression model
model_htn <- glm(Hypertension ~ Age + BMI + PhysActive,
data = nhanes_htn_clean,
family = binomial)
# View model summary
summary(model_htn)
##
## Call:
## glm(formula = Hypertension ~ Age + BMI + PhysActive, family = binomial,
## data = nhanes_htn_clean)
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -4.081656 0.146362 -27.887 <2e-16 ***
## Age 0.042731 0.001490 28.679 <2e-16 ***
## BMI 0.043795 0.003857 11.355 <2e-16 ***
## PhysActiveYes 0.036309 0.053532 0.678 0.498
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 10094.5 on 8249 degrees of freedom
## Residual deviance: 8905.6 on 8246 degrees of freedom
## AIC: 8913.6
##
## Number of Fisher Scoring iterations: 4
# Convert Hypertension to numeric for plotting
nhanes_htn_clean$Hypertension_num <- as.numeric(as.character(nhanes_htn_clean$Hypertension))
ggplot(nhanes_htn_clean, aes(x = BMI, y = Hypertension_num)) +
geom_point(alpha = 0.3) +
geom_smooth(method = "glm",
method.args = list(family = "binomial"),
se = TRUE,
color = "blue") +
labs(title = "Logistic Regression: BMI vs Hypertension",
x = "BMI",
y = "Predicted Probability of Hypertension") +
theme_minimal()
# Predict probabilities
nhanes_htn_clean$predicted_prob <- predict(model_htn, type = "response")
# Classify based on threshold
nhanes_htn_clean$predicted_class <- ifelse(nhanes_htn_clean$predicted_prob > 0.5, "Yes", "No")
# Convert to factors with proper levels
nhanes_htn_clean$predicted_class <- as.factor(nhanes_htn_clean$predicted_class)
nhanes_htn_clean$Hypertension <- factor(nhanes_htn_clean$Hypertension,
levels = c("0", "1"),
labels = c("No", "Yes"))
# Confusion Matrix
conf_matrix_htn <- confusionMatrix(nhanes_htn_clean$predicted_class,
nhanes_htn_clean$Hypertension)
print(conf_matrix_htn)
## Confusion Matrix and Statistics
##
## Reference
## Prediction No Yes
## No 5164 1787
## Yes 602 697
##
## Accuracy : 0.7104
## 95% CI : (0.7005, 0.7202)
## No Information Rate : 0.6989
## P-Value [Acc > NIR] : 0.01146
##
## Kappa : 0.2039
##
## Mcnemar's Test P-Value : < 2e-16
##
## Sensitivity : 0.8956
## Specificity : 0.2806
## Pos Pred Value : 0.7429
## Neg Pred Value : 0.5366
## Prevalence : 0.6989
## Detection Rate : 0.6259
## Detection Prevalence : 0.8425
## Balanced Accuracy : 0.5881
##
## 'Positive' Class : No
##
# ROC Curve and AUC
roc_htn <- roc(nhanes_htn_clean$Hypertension, nhanes_htn_clean$predicted_prob)
plot(roc_htn, main = "ROC Curve - Hypertension Model")
auc_htn <- auc(roc_htn)
text(0.5, 0.3, paste("AUC =", round(auc_htn, 3)), cex = 1.2)
Model Performance Summary:
# Prepare clean dataset for diabetes model
nhanes_diab_clean <- NHANES %>%
select(Gender, Age, Race1, BMI, Diabetes, HealthGen, PhysActive) %>%
na.omit()
# Fit logistic regression model
model_diab <- glm(Diabetes ~ Gender + Age + Race1 + BMI + HealthGen + PhysActive,
data = nhanes_diab_clean,
family = binomial)
# View model summary
summary(model_diab)
##
## Call:
## glm(formula = Diabetes ~ Gender + Age + Race1 + BMI + HealthGen +
## PhysActive, family = binomial, data = nhanes_diab_clean)
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -8.440720 0.371719 -22.707 < 2e-16 ***
## Gendermale 0.341438 0.089248 3.826 0.000130 ***
## Age 0.060357 0.002932 20.582 < 2e-16 ***
## Race1Hispanic -0.046741 0.211997 -0.220 0.825497
## Race1Mexican -0.109064 0.191046 -0.571 0.568083
## Race1White -0.507312 0.131655 -3.853 0.000117 ***
## Race1Other 0.269422 0.196524 1.371 0.170394
## BMI 0.077577 0.006195 12.522 < 2e-16 ***
## HealthGenVgood 0.199263 0.214202 0.930 0.352237
## HealthGenGood 0.833738 0.202068 4.126 3.69e-05 ***
## HealthGenFair 1.519292 0.212947 7.135 9.71e-13 ***
## HealthGenPoor 2.215520 0.259978 8.522 < 2e-16 ***
## PhysActiveYes 0.133200 0.095099 1.401 0.161320
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 4647 on 7470 degrees of freedom
## Residual deviance: 3586 on 7458 degrees of freedom
## AIC: 3612
##
## Number of Fisher Scoring iterations: 6
# Predict probabilities
nhanes_diab_clean$predicted_prob <- predict(model_diab, type = "response")
# Classify based on threshold
nhanes_diab_clean$predicted_class <- ifelse(nhanes_diab_clean$predicted_prob > 0.5, "Yes", "No")
# Confusion Matrix
conf_matrix_diab <- confusionMatrix(as.factor(nhanes_diab_clean$predicted_class),
nhanes_diab_clean$Diabetes)
print(conf_matrix_diab)
## Confusion Matrix and Statistics
##
## Reference
## Prediction No Yes
## No 6707 610
## Yes 64 90
##
## Accuracy : 0.9098
## 95% CI : (0.9031, 0.9162)
## No Information Rate : 0.9063
## P-Value [Acc > NIR] : 0.1556
##
## Kappa : 0.1832
##
## Mcnemar's Test P-Value : <2e-16
##
## Sensitivity : 0.9905
## Specificity : 0.1286
## Pos Pred Value : 0.9166
## Neg Pred Value : 0.5844
## Prevalence : 0.9063
## Detection Rate : 0.8977
## Detection Prevalence : 0.9794
## Balanced Accuracy : 0.5596
##
## 'Positive' Class : No
##
# ROC Curve and AUC
roc_diab <- roc(nhanes_diab_clean$Diabetes, nhanes_diab_clean$predicted_prob)
plot(roc_diab, main = "ROC Curve - Diabetes Model")
auc_diab <- auc(roc_diab)
text(0.5, 0.3, paste("AUC =", round(auc_diab, 3)), cex = 1.2)
Model Performance Summary:
sessionInfo()
## R version 4.5.1 (2025-06-13 ucrt)
## Platform: x86_64-w64-mingw32/x64
## Running under: Windows 10 x64 (build 19045)
##
## Matrix products: default
## LAPACK version 3.12.1
##
## locale:
## [1] LC_COLLATE=English_United States.utf8
## [2] LC_CTYPE=English_United States.utf8
## [3] LC_MONETARY=English_United States.utf8
## [4] LC_NUMERIC=C
## [5] LC_TIME=English_United States.utf8
##
## time zone: America/New_York
## tzcode source: internal
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] pROC_1.19.0.1 caret_7.0-1 lattice_0.22-7 NHANES_2.1.0
## [5] lubridate_1.9.4 forcats_1.0.0 stringr_1.5.2 dplyr_1.1.4
## [9] purrr_1.1.0 readr_2.1.5 tidyr_1.3.1 tibble_3.3.0
## [13] ggplot2_4.0.0 tidyverse_2.0.0
##
## loaded via a namespace (and not attached):
## [1] tidyselect_1.2.1 timeDate_4041.110 farver_2.1.2
## [4] S7_0.2.0 fastmap_1.2.0 digest_0.6.37
## [7] rpart_4.1.24 timechange_0.3.0 lifecycle_1.0.4
## [10] survival_3.8-3 magrittr_2.0.4 compiler_4.5.1
## [13] rlang_1.1.6 sass_0.4.10 tools_4.5.1
## [16] utf8_1.2.6 yaml_2.3.10 data.table_1.17.8
## [19] knitr_1.50 labeling_0.4.3 bit_4.6.0
## [22] plyr_1.8.9 RColorBrewer_1.1-3 withr_3.0.2
## [25] nnet_7.3-20 grid_4.5.1 stats4_4.5.1
## [28] e1071_1.7-16 future_1.67.0 globals_0.18.0
## [31] scales_1.4.0 iterators_1.0.14 MASS_7.3-65
## [34] cli_3.6.5 rmarkdown_2.29 crayon_1.5.3
## [37] generics_0.1.4 rstudioapi_0.17.1 future.apply_1.20.0
## [40] reshape2_1.4.4 tzdb_0.5.0 proxy_0.4-27
## [43] cachem_1.1.0 splines_4.5.1 parallel_4.5.1
## [46] vctrs_0.6.5 hardhat_1.4.2 Matrix_1.7-3
## [49] jsonlite_2.0.0 hms_1.1.3 bit64_4.6.0-1
## [52] listenv_0.9.1 foreach_1.5.2 gower_1.0.2
## [55] jquerylib_0.1.4 recipes_1.3.1 glue_1.8.0
## [58] parallelly_1.45.1 codetools_0.2-20 stringi_1.8.7
## [61] gtable_0.3.6 pillar_1.11.1 htmltools_0.5.8.1
## [64] ipred_0.9-15 lava_1.8.1 R6_2.6.1
## [67] vroom_1.6.5 evaluate_1.0.5 bslib_0.9.0
## [70] class_7.3-23 Rcpp_1.1.0 nlme_3.1-168
## [73] prodlim_2025.04.28 mgcv_1.9-3 xfun_0.53
## [76] pkgconfig_2.0.3 ModelMetrics_1.2.2.2