-
Notifications
You must be signed in to change notification settings - Fork 0
Add NHANES covariate sampling method #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
5ca77fa
Add NHANES covariate sampling method
roninsightrx c14d866
Add offline NHANES cache: download_nhanes_cache() and cache_dir param
roninsightrx 95403f9
Auto-download NHANES cache on package load
roninsightrx 68761c0
Redesign NHANES interface: merged cache and covariates argument
roninsightrx 9902d2c
Add NHANES covariate reference table to sample_covariates_nhanes() docs
roninsightrx 1e7483a
Add dictionary argument to sample_covariates_nhanes()
roninsightrx cfd92f7
update docs
roninsightrx 45aa906
Fix failing tests: tempdir scoping, duplicate-SEQN warning, onLoad mo…
roninsightrx 8bf53da
Fix dictionary test: add BMXHT to mock_nhanes
roninsightrx 0e34db6
Add seed argument to all sample_covariates_* functions
roninsightrx 455efd6
Add rm.na argument to drop NA rows before sampling
roninsightrx da690d2
rm NAs + add docs
roninsightrx a8a66b6
update default
roninsightrx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ Imports: | |
| tidyr, | ||
| tidyselect | ||
| Suggests: | ||
| nhanesA, | ||
| testthat (>= 3.2.0), | ||
| withr | ||
| Remotes: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| #' Download and cache NHANES data locally | ||
| #' | ||
| #' Downloads all tables within the specified NHANES data groups for one or | ||
| #' more survey years, merges them into a single data frame per year, and | ||
| #' saves it as an RDS file. The resulting cache is used automatically by | ||
| #' [sample_covariates_nhanes()]. | ||
| #' | ||
| #' @param groups character vector of NHANES data groups to download. Valid | ||
| #' values: `"DEMO"` (Demographics), `"LAB"` (Laboratory), `"EXAM"` | ||
| #' (Examination), `"Q"` (Questionnaire), `"DIET"` (Dietary). | ||
| #' Defaults to `c("DEMO", "LAB", "EXAM")`. | ||
| #' @param years character vector of NHANES survey cycles, e.g. | ||
| #' `c("2015-2016", "2017-2018")`. Defaults to `"2017-2018"`. | ||
| #' @param path directory where merged RDS files will be saved. Created | ||
| #' automatically if it does not exist. Defaults to the package-level cache | ||
| #' directory returned by `nhanes_default_cache_dir()`. | ||
| #' @param overwrite logical. If `FALSE` (default), a year that already has a | ||
| #' merged RDS in `path` is skipped. Set to `TRUE` to re-download and | ||
| #' overwrite. | ||
| #' @param ... additional arguments (currently unused) | ||
| #' | ||
| #' @details | ||
| #' Each survey year is saved as a single file `nhanes_<year>.rds` | ||
| #' (e.g. `nhanes_2017-2018.rds`) containing all variables from all downloaded | ||
| #' tables, merged on the SEQN respondent sequence number. Tables with | ||
| #' multiple rows per subject (e.g. dietary recall) are skipped automatically. | ||
| #' | ||
| #' Requires the `nhanesA` package. | ||
| #' | ||
| #' @returns the `path` directory, invisibly. | ||
| #' | ||
| #' @export | ||
| download_nhanes_cache <- function( | ||
| groups = c("DEMO", "LAB", "EXAM"), | ||
| years = "2017-2018", | ||
| path = nhanes_default_cache_dir(), | ||
| overwrite = FALSE, | ||
| ... | ||
| ) { | ||
| if (!requireNamespace("nhanesA", quietly = TRUE)) { | ||
| stop( | ||
| "Package 'nhanesA' is required to download NHANES data. ", | ||
| "Install it with: install.packages('nhanesA')", | ||
| call. = FALSE | ||
| ) | ||
| } | ||
|
|
||
| if (!dir.exists(path)) { | ||
| dir.create(path, recursive = TRUE) | ||
| } | ||
|
|
||
| for (year in years) { | ||
| nhanes_year_suffix(year) # validates year; errors on unsupported values | ||
| out_file <- file.path(path, paste0("nhanes_", year, ".rds")) | ||
|
|
||
| if (file.exists(out_file) && !overwrite) { | ||
| message("Skipping ", year, " (cache exists; use overwrite = TRUE to re-download)") | ||
| next | ||
| } | ||
|
|
||
| year_end <- as.integer(sub(".*-", "", year)) | ||
| table_list <- list() | ||
|
|
||
| for (group in groups) { | ||
| message("Fetching table list for group ", group, ", year ", year, " ...") | ||
| tbl_info <- nhanesA::nhanesTables(group, year_end) | ||
| # nhanesTables() returns a data.frame; table names are in Data.File.Name | ||
| tbl_names <- tbl_info[["Data.File.Name"]] | ||
|
|
||
| for (tbl_name in tbl_names) { | ||
| message(" Downloading ", tbl_name, " ...") | ||
| tbl_data <- tryCatch( | ||
| nhanesA::nhanes(tbl_name), | ||
| error = function(e) { | ||
| message(" Skipping ", tbl_name, ": ", conditionMessage(e)) | ||
| NULL | ||
| } | ||
| ) | ||
| if (is.null(tbl_data) || !"SEQN" %in% names(tbl_data)) next | ||
| if (anyDuplicated(tbl_data[["SEQN"]]) > 0) { | ||
| message(" Skipping ", tbl_name, " (multiple rows per subject)") | ||
| next | ||
| } | ||
| table_list[[tbl_name]] <- tbl_data | ||
| } | ||
| } | ||
|
|
||
| if (length(table_list) == 0) { | ||
| warning("No tables downloaded for year ", year, "; skipping.") | ||
| next | ||
| } | ||
|
|
||
| message("Merging ", length(table_list), " tables for ", year, " ...") | ||
| merged <- Reduce( | ||
| function(a, b) dplyr::full_join(a, b, by = "SEQN"), | ||
| table_list | ||
| ) | ||
|
|
||
| saveRDS(merged, out_file) | ||
| message("Saved merged NHANES data (", nrow(merged), " subjects, ", | ||
| ncol(merged), " variables) to ", out_file) | ||
| } | ||
|
|
||
| invisible(path) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,19 @@ | ||
| #' Sample covariates using a variety of methods | ||
| #' | ||
| #' @param method sampling method, one of `mvtnorm`, `bootstrap`, or `mice`. | ||
| #' E.g. `list(AGE = c(60, 80), WT = c(70, 100))`. | ||
| #' | ||
| #' | ||
| #' @param method sampling method, one of `mvtnorm`, `bootstrap`, `mice`, or | ||
| #' `nhanes`. E.g. `list(AGE = c(60, 80), WT = c(70, 100))`. | ||
| #' @param seed integer random seed passed to [set.seed()] for reproducibility. | ||
| #' Default `NULL` does not set a seed. | ||
| #' @param ... arguments passed to lower-level function(s). | ||
| #' | ||
| #' | ||
| #' @returns data.frame with covariates in each column | ||
| #' | ||
| #' @export | ||
| sample_covariates <- function( | ||
| method = c("mvtnorm", "mice", "bootstrap"), | ||
| method = c("mvtnorm", "mice", "bootstrap", "nhanes"), | ||
| seed = NULL, | ||
| ... | ||
| ) { | ||
| method <- rlang::arg_match(method) | ||
| do.call(paste0("sample_covariates_", method), args = list(...)) | ||
| do.call(paste0("sample_covariates_", method), args = list(seed = seed, ...)) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.