|
|
|
|
|
title: "Run Multiple Regression Models" |
|
|
author: "Vy Nguyen" |
|
|
date: "1/10/2023" |
|
|
output: html_document |
|
|
|
|
|
|
|
|
|
|
|
We will learn how to run regression models using the chemical dataset. We will specifically learn to |
|
|
1. run a regression model |
|
|
2. run a regression model accounting for NHANES sampling design |
|
|
3. run multiple regression models using tidyverse |
|
|
4. run multiple regression models accounting for NHANES sampling design via tidyverse |
|
|
|
|
|
```{r setup, include=FALSE} |
|
|
knitr::opts_chunk$set(echo = TRUE) |
|
|
``` |
|
|
|
|
|
|
|
|
You only need to run this ONCE! |
|
|
```{r, echo = FALSE} |
|
|
install.packages("survey") |
|
|
install.packages("tidyverse") |
|
|
install.packages("broom") |
|
|
``` |
|
|
|
|
|
|
|
|
Run this so you have access to the functions in these packages. |
|
|
```{r, echo = FALSE} |
|
|
library("survey") |
|
|
library("tidyverse") |
|
|
library("broom") |
|
|
``` |
|
|
|
|
|
|
|
|
Make sure you set your working directory to be the folder containing file for the NHANES data. |
|
|
```{r, echo = FALSE} |
|
|
load("./w - nhanes_1988_2018.RData") |
|
|
``` |
|
|
|
|
|
|
|
|
|
|
|
Please use any dataset that ends with "_clean". |
|
|
Let's merge the demographics, chemicals, and weights datasets together using tidyverse syntax. |
|
|
```{r, echo = FALSE} |
|
|
nhanes_merged <- full_join(demographics_clean, |
|
|
chemicals_clean, |
|
|
by = c("SEQN", |
|
|
"SEQN_new", |
|
|
"SDDSRVYR")) %>% |
|
|
full_join(., |
|
|
weights_clean, |
|
|
by = c("SEQN", |
|
|
"SEQN_new", |
|
|
"SDDSRVYR")) |
|
|
``` |
|
|
|
|
|
|
|
|
Take a look at the dictionary to know the codename and description of the variables |
|
|
```{r, echo = FALSE} |
|
|
View(df_dictionary) |
|
|
``` |
|
|
|
|
|
|
|
|
factor() is a base R function that encode a variable as categorical |
|
|
relevel() is a base R function that set the reference group. |
|
|
|
|
|
We ensured that sex (RIAGENDR) is a categorical variable. |
|
|
We ensured that race/ethnicity (RIDRETH1) is a categorical variable with the reference group as Non-Hispanic Whites (3). |
|
|
```{r, echo = FALSE} |
|
|
nhanes_merged <- nhanes_merged %>% |
|
|
mutate(RIAGENDR = factor(RIAGENDR)) %>% |
|
|
mutate(RIDRETH1 = factor(RIDRETH1) %>% |
|
|
relevel(., ref = 3)) |
|
|
``` |
|
|
|
|
|
|
|
|
|
|
|
For the portion on calculating summary statistics for multiple variables, we'll iterate over the metal biomarkers to get distribution statistics for each metal biomarkers. |
|
|
So let's include the metal biomarkers and their corresponding weight codenames in this NHANES subset to prepare for subsequential analysis. |
|
|
|
|
|
```{r, echo = FALSE} |
|
|
metals_codename <- df_dictionary %>% |
|
|
filter(chemical_family == "Metals") %>% |
|
|
filter(grepl("replicate", variable_description_use) == FALSE) %>% |
|
|
pull(variable_codename_use) |
|
|
|
|
|
|
|
|
weights_codename_for_metals <- paste("WT_" |
|
|
, metals_codename |
|
|
, sep = "") |
|
|
|
|
|
nhanes_subset <- nhanes_merged %>% |
|
|
select("SEQN", |
|
|
"SEQN_new", |
|
|
"SDDSRVYR", |
|
|
"SDMVPSU", |
|
|
"SDMVSTRA", |
|
|
all_of(metals_codename), |
|
|
all_of(weights_codename_for_metals), |
|
|
"RIDAGEYR", |
|
|
"RIAGENDR", |
|
|
"RIDRETH1") |
|
|
|
|
|
``` |
|
|
|
|
|
|
|
|
|
|
|
Let's run a regression model with blood lead as the outcome variable and the predictors as age, sex, and race/ethnicity. |
|
|
|
|
|
```{r, echo = FALSE} |
|
|
|
|
|
dataset_lead <- nhanes_subset %>% |
|
|
select("SEQN", |
|
|
"SEQN_new", |
|
|
"SDDSRVYR", |
|
|
"SDMVPSU", |
|
|
"SDMVSTRA", |
|
|
"WT_LBXBPB", |
|
|
"LBXBPB", |
|
|
"RIDAGEYR", |
|
|
"RIAGENDR", |
|
|
"RIDRETH1") %>% |
|
|
na.omit(.) |
|
|
|
|
|
model_glm_lead <- glm(log10(LBXBPB) ~ RIDAGEYR + RIAGENDR + RIDRETH1, |
|
|
data = dataset_lead) |
|
|
|
|
|
|
|
|
tidy(model_glm_lead) %>% |
|
|
mutate(percent_diff = (10^estimate - 1)*100) |
|
|
|
|
|
|
|
|
glance(model_glm_lead) |
|
|
|
|
|
``` |
|
|
|
|
|
|
|
|
|
|
|
The survey weights for a given two-year cycle in the set of cycles 3-10 (2003-2018) is representative of a national sample (i.e. the survey weights in a given cycle should sum to the population size of the US). |
|
|
The survey weights for cycles 1 and 2 (1999-2002) TOGETHER is representative of a national sample. |
|
|
|
|
|
When we combine survey cycles, let's suppose for two cycles (cycle 3 and 4), then the survey weights are going to be representative of two national samples, so any estimates are going to be representative of two US population. This doesn't make sense! So we multiply the survey weights by 1/2 (i.e. 1 over the number of studied cycles), and then the adjusted survey weights are representative of ONLY ONE national sample. |
|
|
|
|
|
More examples can be found under the tab "When and How to Construct Weights When Combining Survey Cycles" at https://wwwn.cdc.gov/nchs/nhanes/tutorials/weighting.aspx |
|
|
|
|
|
Sanity check: sum the adjusted survey weights and it should be close to the US population size. |
|
|
|
|
|
Let's run a regression model with blood lead as the outcome variable and the predictors as age, sex, and race/ethnicity, while adjusting for the sampling design. |
|
|
|
|
|
```{r, echo = FALSE} |
|
|
|
|
|
|
|
|
unique_cycles_lead <- dataset_lead %>% |
|
|
pull(SDDSRVYR) %>% |
|
|
unique(.) |
|
|
|
|
|
|
|
|
num_cycles_lead <- length(unique_cycles_lead) |
|
|
|
|
|
|
|
|
dataset_lead <- dataset_lead %>% |
|
|
mutate(adjusted_weights = ifelse(SDDSRVYR %in% c(1,2), |
|
|
(2/num_cycles_lead)*WT_LBXBPB, |
|
|
(1/num_cycles_lead)*WT_LBXBPB)) |
|
|
|
|
|
|
|
|
dataset_lead %>% |
|
|
select(SDDSRVYR, |
|
|
WT_LBXBPB, |
|
|
adjusted_weights) %>% |
|
|
unique(.) %>% |
|
|
mutate(multiplier = adjusted_weights/WT_LBXBPB) %>% |
|
|
View(.) |
|
|
|
|
|
|
|
|
dsn_lead <- svydesign(ids = ~SDMVPSU, |
|
|
strata = ~SDMVSTRA, |
|
|
weights = ~adjusted_weights, |
|
|
nest = TRUE, |
|
|
data = dataset_lead) |
|
|
|
|
|
|
|
|
model_svyglm_lead <- svyglm(log10(LBXBPB) ~ RIDAGEYR + RIAGENDR + RIDRETH1, |
|
|
design = dsn_lead) |
|
|
|
|
|
|
|
|
tidy(model_svyglm_lead) %>% |
|
|
mutate(percent_diff = (10^estimate - 1)*100) |
|
|
|
|
|
|
|
|
glance(model_svyglm_lead) |
|
|
``` |
|
|
|
|
|
|
|
|
|
|
|
We will iterate over each metal biomarker, run a regression model for each metal biomarker, extract the regression statistics from each model, and stitch the dataset of statistics together. |
|
|
|
|
|
To iterate over each metal biomarkers, we will use map() on the codenames of the metal biomarkers. |
|
|
|
|
|
To calculate the statistics for each metal biomarker, we will customize a function (e.g. run_associations_glm) to run a regression model and extract the regression statistics. The model will be log10(chemical concentration) ~ age + sex + race/ethnicity. |
|
|
|
|
|
To stitch the dataset of statistics together, we will use bind_rows(). |
|
|
|
|
|
```{r, echo = FALSE} |
|
|
|
|
|
run_associations_glm <- function(x, |
|
|
df_nhanes, |
|
|
boolean_statistics_type) |
|
|
{ |
|
|
|
|
|
|
|
|
print(x) |
|
|
|
|
|
|
|
|
formula_regression <- paste("log10(", |
|
|
x, |
|
|
")", |
|
|
" ~ RIDAGEYR + RIAGENDR + RIDRETH1", |
|
|
sep = "") |
|
|
|
|
|
|
|
|
string_chem_no_equal_zero <- paste(x , |
|
|
"!= 0", |
|
|
sep = "") |
|
|
|
|
|
|
|
|
subset_x <- df_nhanes %>% |
|
|
select("SEQN", |
|
|
"SEQN_new", |
|
|
"SDDSRVYR", |
|
|
all_of(x), |
|
|
"RIDAGEYR", |
|
|
"RIAGENDR", |
|
|
"RIDRETH1") %>% |
|
|
na.omit(.) %>% |
|
|
filter(eval(parse(text = string_chem_no_equal_zero))) |
|
|
|
|
|
|
|
|
model_x <- glm(as.formula(formula_regression), |
|
|
data = subset_x) |
|
|
|
|
|
|
|
|
|
|
|
string_stats_expression <- paste(boolean_statistics_type, |
|
|
"(model_x)", |
|
|
sep = "") |
|
|
|
|
|
|
|
|
df_stats <- eval(parse(text = string_stats_expression)) %>% |
|
|
mutate(variable_codename_use = x) %>% |
|
|
mutate(formula = formula_regression) %>% |
|
|
relocate(variable_codename_use) |
|
|
|
|
|
return(df_stats) |
|
|
} |
|
|
|
|
|
|
|
|
df_glm_metals_tidy <- metals_codename %>% |
|
|
map(. |
|
|
, run_associations_glm |
|
|
, nhanes_subset |
|
|
, "tidy") %>% |
|
|
bind_rows(.) %>% |
|
|
mutate(percent_diff = (10^estimate - 1)*100) |
|
|
|
|
|
|
|
|
df_glm_metals_glance <- metals_codename %>% |
|
|
map(. |
|
|
, run_associations_glm |
|
|
, nhanes_subset |
|
|
, "glance") %>% |
|
|
bind_rows(.) |
|
|
``` |
|
|
|
|
|
|
|
|
|
|
|
AGain, we will iterate over each metal biomarker, run a regression model for each metal biomarker, extract the regression statistics from each model, and stitch the dataset of statistics together. |
|
|
|
|
|
Now the difference is that when we customize the function for each metal biomarker, we need to adjust the survey weights, so that our estimates will be representative of ONLY ONE national sample. |
|
|
|
|
|
```{r, echo = FALSE} |
|
|
|
|
|
run_associations_svyglm <- function(x, |
|
|
df_nhanes, |
|
|
boolean_statistics_type) |
|
|
{ |
|
|
|
|
|
print(x) |
|
|
|
|
|
|
|
|
weight_codename <- paste("WT_", |
|
|
x, |
|
|
sep = "") |
|
|
|
|
|
|
|
|
formula_regression <- paste("log10(", |
|
|
x, |
|
|
")", |
|
|
" ~ RIDAGEYR + RIAGENDR + RIDRETH1", |
|
|
sep = "") |
|
|
|
|
|
|
|
|
string_chem_no_equal_zero <- paste(x , |
|
|
"!= 0", |
|
|
sep = "") |
|
|
|
|
|
|
|
|
subset_x <- df_nhanes %>% |
|
|
select("SEQN", |
|
|
"SEQN_new", |
|
|
"SDDSRVYR", |
|
|
"SDMVPSU", |
|
|
"SDMVSTRA", |
|
|
all_of(x), |
|
|
all_of(weight_codename), |
|
|
"RIDAGEYR", |
|
|
"RIAGENDR", |
|
|
"RIDRETH1") %>% |
|
|
na.omit(.) %>% |
|
|
filter(eval(parse(text = string_chem_no_equal_zero))) |
|
|
|
|
|
|
|
|
unadjusted_weights <- subset_x %>% |
|
|
pull(all_of(weight_codename)) |
|
|
|
|
|
|
|
|
unique_cycles_x <- subset_x %>% |
|
|
pull(SDDSRVYR) %>% |
|
|
unique(.) |
|
|
|
|
|
|
|
|
|
|
|
num_cycles <- length(unique_cycles_x) |
|
|
|
|
|
|
|
|
|
|
|
indicator_cycles <- ifelse(1 %in% unique_cycles_x & 2 %in% unique_cycles_x, |
|
|
"yes", |
|
|
"no") |
|
|
|
|
|
|
|
|
if(indicator_cycles == "yes") |
|
|
{ |
|
|
adjusted_weights <- ifelse(subset_x$SDDSRVYR %in% c(1,2), |
|
|
(2/num_cycles)*unadjusted_weights, |
|
|
(1/num_cycles)*unadjusted_weights) |
|
|
} else { |
|
|
adjusted_weights <- (1/num_cycles)*unadjusted_weights |
|
|
} |
|
|
|
|
|
|
|
|
subset_x <- subset_x %>% |
|
|
mutate(adjusted_weights = adjusted_weights) %>% |
|
|
filter(adjusted_weights != 0) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dsn_x <- svydesign(ids = ~SDMVPSU, |
|
|
strata = ~SDMVSTRA, |
|
|
weights = ~adjusted_weights, |
|
|
nest = TRUE, |
|
|
data = subset_x) |
|
|
|
|
|
|
|
|
model_x <- svyglm(as.formula(formula_regression), |
|
|
design = dsn_x) |
|
|
|
|
|
|
|
|
if(boolean_statistics_type == "tidy") |
|
|
{ |
|
|
df_stats <- tidy(model_x) %>% |
|
|
mutate(variable_codename_use = x) %>% |
|
|
mutate(formula = formula_regression) %>% |
|
|
relocate(variable_codename_use) |
|
|
|
|
|
|
|
|
|
|
|
} else if(boolean_statistics_type == "glance") { |
|
|
|
|
|
df_stats <- data.frame("nobs" = length(model_x$y), |
|
|
"null.deviance" = model_x$null.deviance, |
|
|
"df.null" = model_x$df.null, |
|
|
"logLik" = logLik(model_x)[1], |
|
|
"df" = length(model_svyglm_lead$coefficients) + 1, |
|
|
"AIC" = model_x$aic, |
|
|
"BIC" = -2*logLik + log(nobs)*df, |
|
|
"deviance" = model_x$deviance, |
|
|
"df.residual" = model_x$df.residual) %>% |
|
|
mutate(variable_codename_use = x) %>% |
|
|
mutate(formula = formula_regression) %>% |
|
|
relocate(variable_codename_use) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
return(df_stats) |
|
|
} |
|
|
|
|
|
df_svyglm_metals_tidy <- metals_codename %>% |
|
|
map(., |
|
|
run_associations_svyglm, |
|
|
nhanes_subset, |
|
|
"tidy") %>% |
|
|
bind_rows(.) %>% |
|
|
mutate(percent_diff = (10^estimate - 1)*100) |
|
|
|
|
|
df_svyglm_metals_glance <- metals_codename %>% |
|
|
map(., |
|
|
run_associations_svyglm, |
|
|
nhanes_subset, |
|
|
"glance") %>% |
|
|
bind_rows(.) |
|
|
``` |
|
|
|
|
|
|