File size: 15,426 Bytes
6a1738b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 |
---
title: "Run Multiple Regression Models"
author: "Vy Nguyen"
date: "1/10/2023"
output: html_document
---
## Goal
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)
```
## Install packages to run analysis
You only need to run this ONCE!
```{r, echo = FALSE}
install.packages("survey") # Needed to run a regression model that accounts for the NHANES sampling design
install.packages("tidyverse") # Needed to pipe functions together
install.packages("broom") # Needed to easily extract regression statistics
```
## Load packages
Run this so you have access to the functions in these packages.
```{r, echo = FALSE}
library("survey")
library("tidyverse")
library("broom")
```
## Loading NHANES datasets
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")
```
## Merge NHANES datasets
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"))
```
## Data dictionary
Take a look at the dictionary to know the codename and description of the variables
```{r, echo = FALSE}
View(df_dictionary)
```
## Ensure that sex and race/ethnicity variables are categorical
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))
```
## Choose only variables that you will use
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")
```
## A generalized linear regression model
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)
# Extract the regression statistics for the predictors
tidy(model_glm_lead) %>%
mutate(percent_diff = (10^estimate - 1)*100)
# Extract the statistics for the model
glance(model_glm_lead)
```
## A generalized linear regression model accounting for NHANES sampling design
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}
# Determine the cycles with measurements for blood lead
unique_cycles_lead <- dataset_lead %>%
pull(SDDSRVYR) %>%
unique(.)
# Determine the total number of cycles with measurements for lead
num_cycles_lead <- length(unique_cycles_lead)
# Calculate the adjusted survey weights
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))
# Checking our calculations
dataset_lead %>%
select(SDDSRVYR,
WT_LBXBPB,
adjusted_weights) %>%
unique(.) %>%
mutate(multiplier = adjusted_weights/WT_LBXBPB) %>%
View(.)
# Account for the sampling design
dsn_lead <- svydesign(ids = ~SDMVPSU,
strata = ~SDMVSTRA,
weights = ~adjusted_weights,
nest = TRUE,
data = dataset_lead)
# Run the regression model
model_svyglm_lead <- svyglm(log10(LBXBPB) ~ RIDAGEYR + RIAGENDR + RIDRETH1,
design = dsn_lead)
# Extract the regression statistics for the predictors
tidy(model_svyglm_lead) %>%
mutate(percent_diff = (10^estimate - 1)*100)
# Extract the statistics for the model
glance(model_svyglm_lead)
```
## Running generalized linear regression models over all metal biomarkers
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 out the codename of a given chemical biomarker
# This helps with debugging to detect which chemical biomarker is causing problems.
print(x)
# Define a string for the code to run the regression model
formula_regression <- paste("log10(",
x,
")",
" ~ RIDAGEYR + RIAGENDR + RIDRETH1",
sep = "")
# Define a string to exclude chemical measurements that are 0
string_chem_no_equal_zero <- paste(x ,
"!= 0",
sep = "")
# Define a subset to include any participants with measurements for the chemical biomarker
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))) # Exclude all measurements that are 0
# Run the regression model
model_x <- glm(as.formula(formula_regression),
data = subset_x)
# Define a string to extract the regression statistics
# It could be either tidy(model_x) or glance(model_x)
string_stats_expression <- paste(boolean_statistics_type,
"(model_x)",
sep = "")
# Tabulate the regression statistics
df_stats <- eval(parse(text = string_stats_expression)) %>%
mutate(variable_codename_use = x) %>% # Include the codename to know which statistics belongs to which chemical when we stitch the datasets together
mutate(formula = formula_regression) %>% # Include the regression formula
relocate(variable_codename_use) # Make the column of chemical codenames be the 1st column
return(df_stats)
}
# Run a regression model on each metal biomarker and extract the regression statistics for all metal biomarkers
df_glm_metals_tidy <- metals_codename %>%
map(.
, run_associations_glm
, nhanes_subset
, "tidy") %>%
bind_rows(.) %>%
mutate(percent_diff = (10^estimate - 1)*100)
# Run a regression model on each metal biomarker and extract the regression statistics for all metal biomarkers
df_glm_metals_glance <- metals_codename %>%
map(.
, run_associations_glm
, nhanes_subset
, "glance") %>%
bind_rows(.)
```
## Running generalized linear regression models over all metal biomarkers & accounting for NHANES sampling design
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 out the codename of a given chemical biomarker
print(x)
# Define the weight codename for a given chemical biomarker
weight_codename <- paste("WT_",
x,
sep = "")
# Define a string for the code to run the regression model
formula_regression <- paste("log10(",
x,
")",
" ~ RIDAGEYR + RIAGENDR + RIDRETH1",
sep = "")
# Define a string to exclude chemical measurements that are 0
string_chem_no_equal_zero <- paste(x ,
"!= 0",
sep = "")
# Define a subset to include any participants with measurements for the chemical biomarker
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))) # Exclude all measurements that are 0
# Extract the unadjusted survey weights from the subset
unadjusted_weights <- subset_x %>%
pull(all_of(weight_codename))
# Determine the cycles that the metal biomarker was measured
unique_cycles_x <- subset_x %>%
pull(SDDSRVYR) %>%
unique(.)
# print(unique_cycles_x)
# Determine the total number of cycles
num_cycles <- length(unique_cycles_x)
# print(num_cycles)
# Determine if the chemical biomarker had measurements in both cycle 1 and cycle 2
indicator_cycles <- ifelse(1 %in% unique_cycles_x & 2 %in% unique_cycles_x,
"yes",
"no")
# Calculate the adjusted weights
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
}
# Include the adjusted weights in the subset and exclude any weights that are 0 or else the code to calculate the statistics may not run
subset_x <- subset_x %>%
mutate(adjusted_weights = adjusted_weights) %>%
filter(adjusted_weights != 0)
# df_check <- subset_x %>%
# select(SDDSRVYR,
# adjusted_weights) %>%
# cbind(.
# , unadjusted_weights) %>%
# unique() %>%
# mutate(multiplier = adjusted_weights/unadjusted_weights)
# View(df_check)
#
# print(unique(df_check$multiplier))
# Define the survey object to account for the sampling design
dsn_x <- svydesign(ids = ~SDMVPSU,
strata = ~SDMVSTRA,
weights = ~adjusted_weights,
nest = TRUE,
data = subset_x)
# Run the regression model
model_x <- svyglm(as.formula(formula_regression),
design = dsn_x)
# Extract the regression statistics for the predictors
if(boolean_statistics_type == "tidy")
{
df_stats <- tidy(model_x) %>%
mutate(variable_codename_use = x) %>%
mutate(formula = formula_regression) %>%
relocate(variable_codename_use)
# Manually extract and calculate the model statistics
# glance() did not work on a model object that account for the sampling design
} 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(.)
```
|