### Install nhanesA from Development Repository Source: https://cran.r-project.org/web/packages/nhanesA/readme/README.html Install the development version of nhanesA from GitHub using the devtools package. Ensure devtools is installed first. ```r install.packages("devtools") ``` ```r devtools::install_github("cjendres1/nhanes") ``` -------------------------------- ### Run Docker-enhanced nhanesA Container Source: https://cran.r-project.org/web/packages/nhanesA/readme/README.html This command starts a Docker container that includes a SQL database with NHANES data for faster local access. Replace , , and with your desired values. The image will be downloaded on the first run. ```bash docker run --rm -d -p 8787:8787 -e 'CONTAINER_USER_USERNAME=' -e 'CONTAINER_USER_PASSWORD=' deepayansarkar/nhanes-postgresql: ``` -------------------------------- ### Install nhanesA from CRAN Source: https://cran.r-project.org/web/packages/nhanesA/readme/README.html Use this command to install the nhanesA package from the Comprehensive R Archive Network (CRAN). ```r install.packages("nhanesA") ``` -------------------------------- ### Load NHANES Demographic Data Source: https://cran.r-project.org/web/packages/nhanesA/vignettes/UsingSurveyWeights.rmd Loads the DEMO_J dataset from NHANES using the nhanesA package. Ensure the 'nhanesA' library is installed and loaded. ```r library("nhanesA") demoj = nhanes("DEMO_J") dim(demoj) ``` -------------------------------- ### Get NHANES Table Variables Source: https://cran.r-project.org/web/packages/nhanesA/refman/nhanesA.html Retrieves variable names or details for specified NHANES tables. Use to inspect table contents before data retrieval. ```R lab_cbc = nhanesTableVars('LAB', 'CBC_E') dim(lab_cbc) exam_ohx = nhanesTableVars('EXAM', 'OHX_E', details=TRUE, nchar=50) dim(exam_ohx) demo = nhanesTableVars('DEMO', 'DEMO_F', namesonly = TRUE) length(demo) ``` -------------------------------- ### Get NHANES Table Variable Names Source: https://cran.r-project.org/web/packages/nhanesA/vignettes/Introducing_nhanesA.Rmd Retrieve the names of variables for a specific NHANES table. This can be used to select variables for custom translation. ```r bpx_d_vars <- nhanesTableVars('EXAM', 'BPX_D', namesonly=TRUE) #Alternatively may use bpx_d_vars = names(bpx_d) ``` -------------------------------- ### nhanesOptions Source: https://cran.r-project.org/web/packages/nhanesA/refman/nhanesA.html Set and retrieve global options controlling the behaviour of certain functions in the package. Options can be set using 'nhanesOptions(key1 = value1, key2 = value2)'. Options can be retrieved (one at a time) using 'nhanesOptions("key")'. When called with no arguments, all currently set options are returned as a list. ```APIDOC ## nhanesOptions ### Description Set and retrieve global options controlling the behaviour of certain functions in the package. ### Usage ```R nhanesOptions(...) ``` ### Arguments `...` | either one or more named arguments giving options to be set (in the form `key = value`), or a single unnamed character string to retrieve a setting. ### Details The 'nhanesOptions()' function can be used in two forms, to set or get options. Options can be set using 'nhanesOptions(key1 = value1, key2 = value2)'. Options can be retrieved (one at a time) using 'nhanesOptions("key")'. When called with no arguments, all currently set options are returned as a list. Options currently used in the package are 'use.db' (logical flag controlling whether a database should be used if available), and 'log.access', a logical flag that logs any attempted URL access by printing the URL). ### Value When retrieving an option, the value of the option, or `NULL` if the option has not been set. When setting one or more options, a list (invisibly) containing the previous values (possibly `NULL`) of the options being set. ### Examples ```R nhanesOptions(foo = "bar") nhanesOptions() print(nhanesOptions(foo = NULL)) ``` ``` -------------------------------- ### Manage nhanesA Package Options Source: https://cran.r-project.org/web/packages/nhanesA/refman/nhanesA.html Set or retrieve global options for the nhanesA package. Use named arguments to set options like 'use.db' or 'log.access', or pass a character string to retrieve a specific option. Calling with no arguments returns all set options. ```r nhanesOptions(foo = "bar") ``` ```r nhanesOptions() ``` ```r print(nhanesOptions(foo = NULL)) ``` -------------------------------- ### nhanesCodebookFromURL Source: https://cran.r-project.org/web/packages/nhanesA/refman/nhanesA.html Downloads and parses an NHANES doc file from a URL and returns it as a list. ```APIDOC ## nhanesCodebookFromURL ### Description Download and parse an NHANES doc file from a URL ### Usage ```R nhanesCodebookFromURL(url) ``` ### Arguments - `url` (character): URL to be downloaded ### Details Downloads and parses an NHANES doc file from a URL and returns it as a list ### Value list with one element for each variable ``` -------------------------------- ### Download NHANES manifests Source: https://cran.r-project.org/web/packages/nhanesA/refman/nhanesA.html Downloads and parses NHANES manifests for data tables or variables. Use the cache option to speed up repeated downloads within a session. ```R nhanesManifest( which = c("public", "limitedaccess", "variables"), sizes = FALSE, dxa = FALSE, component = NULL, verbose = getOption("verbose"), use_cache = TRUE, max_age = 24 * 60 * 60 ) ``` -------------------------------- ### Get NHANES Table Attributes (Deprecated) Source: https://cran.r-project.org/web/packages/nhanesA/refman/nhanesA.html Returns attributes such as number of rows, columns, and memory size for an NHANES data table. This function is deprecated and `nhanesTableSummary` should be used instead. ```R nhanesAttr(nh_table) ``` -------------------------------- ### Get NHANES Table Attributes Source: https://cran.r-project.org/web/packages/nhanesA/refman/nhanesA.html Use nhanesAttr to check the size and characteristics of a data table before importing it into R. The function downloads, inspects, and then deletes the table. It respects the NHANES_TABLE_BASE environment variable for alternative base URLs. ```R ## Not run: bpx_e = nhanesAttr('BPX_E') ## Not run: length(bpx_e) ## Not run: folate_f = nhanesAttr('FOLATE_F') ## Not run: length(folate_f) ``` -------------------------------- ### Simulate NHANES Table Variables Source: https://cran.r-project.org/web/packages/nhanesA/vignettes/Introducing_nhanesA.Rmd This code simulates the output of `nhanesTableVars` for demonstration purposes, showing column names and descriptions. ```r df <- data.frame(matrix(1,nrow=27,ncol=2)) names(df) <- c('Variable.Name', 'Variable.Description') df[1,] <- list('BMDSTATS', 'Body Measures Component status Code') df[2,] <- list('BMIARMC', 'Arm Circumference Comment') df[3,] <- list('BMIARML', 'Upper Arm Length Comment') df[4,] <- list('BMICALF', ' Maximal Calf Comment') df[5,] <- list('BMIHEAD', 'Head Circumference Comment') df[6,] <- list('BMIHT', 'Standing Height Comment') df[7,] <- list('BMILEG', 'Upper Leg Length Comment') df[8,] <- list('BMIRECUM', 'Recumbent Length Comment') df[9,] <- list('BMISUB', 'Subscapular Skinfold Comment') df[10,] <- list('BMITHICR', 'Thigh Circumference Comment') df[11,] <- list('BMITRI', 'Triceps Skinfold Comment') df[12,] <- list('BMIWAIST', 'Waist Circumference Comment') df[13,] <- list('BMIWT', 'Weight Comment') df[14,] <- list('BMXARMC', 'Arm Circumference (cm)') df[15,] <- list('BMXARML', 'Upper Arm Length (cm)') df[16,] <- list('BMXBMI', 'Body Mass Index (kg/m**2)') df[17,] <- list('BMXCALF', 'Maximal Calf Circumference (cm)') df[18,] <- list('BMXHEAD', 'Head Circumference (cm)') df[19,] <- list('BMXHT', 'Standing Height (cm)') df[20,] <- list('BMXLEG', 'Upper Leg Length (cm)') df[21,] <- list('BMXRECUM', 'Recumbent Length (cm)') df[22,] <- list('BMXSUB', 'Subscapular Skinfold (mm)') df[23,] <- list('BMXTHICR', 'Thigh Circumference (cm)') df[24,] <- list('BMXTRI', 'Triceps Skinfold (mm)') df[25,] <- list('BMXWAIST', 'Waist Circumference (cm)') df[26,] <- list('BMXWT', 'Weight (kg)') df[27,] <- list('SEQN', 'Respondent sequence number.') df ``` -------------------------------- ### Advanced Table Search with Options Source: https://cran.r-project.org/web/packages/nhanesA/vignettes/Introducing_nhanesA.Rmd Demonstrates nhanesSearchTableNames with additional parameters like includerdc, nchar, and details. Use this to refine searches and retrieve more specific information, including Restricted Data Center (RDC) tables. ```r nhanesSearchTableNames('HPVS', includerdc=TRUE, nchar=42, details=TRUE) ``` -------------------------------- ### List NHANES Tables Source: https://cran.r-project.org/web/packages/nhanesA/vignettes/Introducing_nhanesA.Rmd Use nhanesTables to get information on tables available for a specific data category and year. The two-year survey intervals begin with the odd year, and entering a single year will yield identical output for both years in the interval. ```r library(nhanesA) nhanesTables('EXAM', 2005) ``` -------------------------------- ### nhanes Source: https://cran.r-project.org/web/packages/nhanesA/refman/nhanesA.html Downloads a specified NHANES data table from the website and returns it as an R data frame. ```APIDOC ## nhanes ### Description Download an NHANES table and return as a data frame. Use to download NHANES data tables that are in SAS format. ### Usage nhanes( nh_table, includelabels = FALSE, translated = TRUE, cleanse_numeric = FALSE, nchar = 128, adjust_timeout = TRUE ) ### Arguments - `nh_table` (character) - The name of the specific table to retrieve. - `includelabels` (logical) - If TRUE, then include SAS labels as variable attribute (default = FALSE). - `translated` (logical) - Whether the variables are translated (default = TRUE). - `cleanse_numeric` (logical) - If TRUE, some special codes in numeric variables, such as ‘Refused’ and ‘Don't know’ will be converted to `NA` (default = FALSE). - `nchar` (numeric) - Maximum length of translated string (default = 128). Ignored if translated=FALSE. - `adjust_timeout` (logical or numeric) - Adjusts the default `download.file` timeout. If TRUE, it's adjusted based on file size. If numeric, it's a multiplicative factor. ### Details Downloads a table from the NHANES website in its entirety. If the environment variable `NHANES_TABLE_BASE` was set, its value is used as the base URL instead of https://wwwn.cdc.gov. NHANES tables are stored in SAS '.XPT' format but are imported as a data frame. This function cannot be used to import limited access data. ### Value The table is returned as a data frame. ### Examples ```R bpx_e = nhanes('BPX_E') dim(bpx_e) folate_f = nhanes('FOLATE_F', includelabels = TRUE) dim(folate_f) ``` ``` -------------------------------- ### Download NHANES Table from URL Source: https://cran.r-project.org/web/packages/nhanesA/refman/nhanesA.html Downloads an NHANES table directly from a URL and returns it as a data frame. Allows options for translation, numeric cleansing, character length, and timeout adjustment. ```R nhanesFromURL( url, translated = TRUE, cleanse_numeric = TRUE, nchar = 128, adjust_timeout = TRUE ) ``` -------------------------------- ### nhanesManifest Source: https://cran.r-project.org/web/packages/nhanesA/refman/nhanesA.html Downloads and parses NHANES manifests for data tables and variables, returning them as data frames. Supports caching and filtering by component. ```APIDOC ## nhanesManifest ### Description Downloads and parses NHANES manifests for data tables and variables, and returns them as data frames. The NHANES website maintains several listings (manifests) of tables and associated variables, which can be downloaded using these functions. The list of tables for which data is available publicly can be found at https://wwwn.cdc.gov/Nchs/Nhanes/search/DataPage.aspx, with further restriction to specific components possible by specifying an additional query parameter as below. This is the _public_ manifest. Limited access tables (also referred to as RDC only tables) are listed at https://wwwn.cdc.gov/Nchs/Nhanes/search/DataPage.aspx?Component=LimitedAccess. This is the _limited access_ manifest. Available variables are listed according to component at https://wwwn.cdc.gov/nchs/nhanes/search/variablelist.aspx?Component=Demographics, etc. These are the _variable_ manifests. ### Usage ```R nhanesManifest( which = c("public", "limitedaccess", "variables"), sizes = FALSE, dxa = FALSE, component = NULL, verbose = getOption("verbose"), use_cache = TRUE, max_age = 24 * 60 * 60 ) ``` ### Arguments #### Query Parameters - **which** (character vector) - Either "public" or "limitedaccess" to get a manifest of available tables, or "variables" to get a manifest of available variables. - **sizes** (logical) - Optional - whether to compute data file sizes (as reported by the server) and include them in the result. - **dxa** (logical) - Optional - whether to include information on DXA tables. These tables contain imputed imputed Dual Energy X-ray Absorptiometry measurements, and are listed separately, not in the main listing. - **component** (character string) - Optional - An optional character string specifying the component for which the public data manifest is to be downloaded. Valid values are "demographics", "dietary", "examination", "laboratory", and "questionnaire". Partial matching is allowed, and case is ignored. Specifying a component for the public manifest will return a subset of the tables, but has the advantage that the result will include a description of each table. - **verbose** (logical) - Optional - Logical flag indicating whether information on progress should be reported. - **use_cache** (logical) - Optional - Logical flag indicating whether a cached version (from a previous download in the same session) should be used. - **max_age** (numeric) - Optional - Maximum allowed age of the cache in seconds (defaults to 24 hours). Cached versions that are older are ignored, even if available. ### Value A data frame, with columns that depend on `which`. For a manifest of tables, columns are "Table", "DocURL", "DataURL", "Years", "Date.Published". If `component` is specified, an additional column "Description" giving a description of the table will be included. If `sizes = TRUE`, an additional column "DataSize" giving the data file sizes in bytes (as reported by the server) is included. For limited access tables, the "DataURL" and "DataSize" columns are omitted. For a manifest of variables, columns are "VarName", "VarDesc", "Table", "TableDesc", "BeginYear", "EndYear", "Component", and "UseConstraints". ``` -------------------------------- ### Download NHANES data from URL Source: https://cran.r-project.org/web/packages/nhanesA/refman/nhanesA.html Downloads an XPT file from a specified URL and returns it as a data frame. Optionally translates codes and cleans numeric variables. May fail if the CDC website is unavailable. ```R try({ vix_e = nhanesFromURL("https://wwwn.cdc.gov/Nchs/Data/Nhanes/Public/2007/DataFiles/VIX_E.xpt") bpx_e = nhanesFromURL("/Nchs/Data/Nhanes/Public/2007/DataFiles/BPX_E.xpt", translated = FALSE) dim(bpx_e) }) ``` -------------------------------- ### Parse NHANES Codebook from URL Source: https://cran.r-project.org/web/packages/nhanesA/refman/nhanesA.html Downloads and parses an NHANES doc file directly from a provided URL, returning the information as a list. ```R nhanesCodebookFromURL(url) ``` -------------------------------- ### List NNYFS Tables and Import Data Source: https://cran.r-project.org/web/packages/nhanesA/vignettes/Introducing_nhanesA.Rmd Use 'Y' or 'y' as the year to list NNYFS (NHANES National Youth Fitness Survey) tables. Data import and variable translation function as usual. ```r #List NNYFS EXAM tables nhanesTables('EXAM', 'Y') #Table import and variable translation operate as usual y_cvx <- nhanes('Y_CVX') nhanesTranslate('Y_CVX','CVXPARC') ``` -------------------------------- ### nhanesFromURL Source: https://cran.r-project.org/web/packages/nhanesA/refman/nhanesA.html Downloads an NHANES table from a URL and returns it as a data frame. Allows for translation and numeric cleansing. ```APIDOC ## nhanesFromURL ### Description Downloads an NHANES table from a URL and returns it as a data frame. ### Usage ```R nhanesFromURL( url, translated = TRUE, cleanse_numeric = TRUE, nchar = 128, adjust_timeout = TRUE ) ``` ### Arguments - `url` (character): The URL of the NHANES table to download. - `translated` (logical, optional): If TRUE, the data will be translated (default=TRUE). - `cleanse_numeric` (logical, optional): If TRUE, numeric data will be cleansed (default=TRUE). - `nchar` (numeric, optional): The number of characters to read for character variables (default=128). - `adjust_timeout` (logical or numeric, optional): A logical flag indicating whether the default `download.file` timeout option should be adjusted. Can also be a positive numeric value used as a multiplicative factor. ``` -------------------------------- ### Simulate NHANES Codebook Output Source: https://cran.r-project.org/web/packages/nhanesA/vignettes/Introducing_nhanesA.Rmd This code simulates the structure and content of a codebook generated by `nhanesCodebook` for a categorical variable. ```r df <- data.frame(matrix(1,nrow=3, ncol=5)) names(df) <- c("Code.or.Value", "Value.Description", "Count", "Cumulative", "Skip to Item") df[1,] <- list(1, 'Male', 5080, 5080, NA) df[2,] <- list(2, 'Female', 5268, 10348, NA) df[3,] <- list('.', 'Missing', 0, 10348, NA) codelist <- list("RIAGENDR", "Gender", "Gender of the sample person", "Both males and females 0 YEARS -\r 150 YEARS", df) names(codelist) <- c('Variable Name', 'SAS Label', 'English Text', 'Target', 'RIAGENDR') codlist ``` -------------------------------- ### List Pre-Pandemic Tables by Category Source: https://cran.r-project.org/web/packages/nhanesA/vignettes/Introducing_nhanesA.Rmd List all pre-pandemic NHANES tables belonging to a specific category, such as 'EXAM'. ```r #List pre-pandemic EXAM tables nhanesTables('EXAM', 'P') ``` -------------------------------- ### Display NHANES Codebook for a Variable Source: https://cran.r-project.org/web/packages/nhanesA/refman/nhanesA.html Retrieves the full NHANES codebook for a specified variable within a table. Handles potential website unavailability with a try-catch block. Respects the NHANES_TABLE_BASE environment variable for documentation URLs. ```R ## May fail if CDC website is unavailable try({ nhanesCodebook('AUX_D', 'AUQ020D') |> print() nhanesCodebook('BPX_J', 'BPACSZ') |> print() bpx_code = nhanesCodebook('BPX_J') length(bpx_code) }) ``` -------------------------------- ### Import DXA Data Source: https://cran.r-project.org/web/packages/nhanesA/vignettes/Introducing_nhanesA.Rmd Import Dual Energy X-Ray Absorptiometry (DXA) data for a specified year. DXA data may require special handling due to its size and missing values. ```r #Import into R dxx_b <- nhanesDXA(2001) ``` -------------------------------- ### Download Entire Survey Data Source: https://cran.r-project.org/web/packages/nhanesA/vignettes/Introducing_nhanesA.Rmd Download all tables for a specific survey cycle and year. This is useful for obtaining a complete dataset for analysis. ```r q2007names <- nhanesTables('Q', 2007, namesonly=TRUE) q2007tables <- lapply(q2007names, nhanes) names(q2007tables) <- q2007names ``` -------------------------------- ### Save DXA Data to File Source: https://cran.r-project.org/web/packages/nhanesA/vignettes/Introducing_nhanesA.Rmd Save DXA data for a specified year to a local file. This is recommended for large DXA tables to manage memory usage. ```r #Save to file nhanesDXA(2001, destfile="dxx_b.xpt") ``` -------------------------------- ### Search Table Names by Pattern Source: https://cran.r-project.org/web/packages/nhanesA/vignettes/Introducing_nhanesA.Rmd Use nhanesSearchTableNames to find tables matching a naming pattern. This is useful for grouping data across surveys. The search is efficient as it operates on a comprehensive table list. ```r nhanesSearchTableNames('BMX') ``` -------------------------------- ### Create Survey Design Object Source: https://cran.r-project.org/web/packages/nhanesA/vignettes/UsingSurveyWeights.rmd Creates a survey design object using the 'survey' package, incorporating PSU, strata, and weights from the merged NHANES data. This object is crucial for accurate survey data analysis. Ensure 'survey' library is loaded. ```r library("survey") nhanesDesign <- svydesign(id = ~SDMVPSU, # Primary Sampling Units (PSU) strata = ~SDMVSTRA, # Stratification used in the survey weights = ~WTMEC2YR, # Survey weights nest = TRUE, # Whether PSUs are nested within strata data = data) ``` -------------------------------- ### Retrieve NHANES Manifest Data Source: https://cran.r-project.org/web/packages/nhanesA/refman/nhanesA.html Fetches the NHANES manifest, optionally specifying component and whether to include sizes. May fail if the CDC website is malformatted. ```r try({ manifest <- nhanesManifest(sizes = FALSE) dim(manifest) }) ``` ```r try({ varmf <- nhanesManifest("variables", component = "lab") head(varmf) }) ``` -------------------------------- ### Import and Process Pre-Pandemic Data Source: https://cran.r-project.org/web/packages/nhanesA/vignettes/Introducing_nhanesA.Rmd Demonstrates importing a pre-pandemic NHANES table, translating variables, and displaying its codebook. These operations function similarly to standard NHANES data. ```r #Table import, variable translation, and codebook display operate as usual p_dxxfem <- nhanes('P_DXXFEM') nhanesTranslate('P_BMX', 'BMDSTATS') nhanesCodebook('P_INS', 'LBDINSI') ``` -------------------------------- ### nhanesSearchTableNames Source: https://cran.r-project.org/web/packages/nhanesA/refman/nhanesA.html Returns a list of table names that match a specified pattern. The search can be restricted to specific survey years. ```APIDOC ## nhanesSearchTableNames ### Description Search for matching table names. Returns a list of table names that match a specified pattern. ### Usage ```R nhanesSearchTableNames( pattern = NULL, ystart = NULL, ystop = NULL, includerdc = FALSE, includewithdrawn = FALSE, nchar = 128, details = FALSE ) ``` ### Arguments `pattern` | Pattern of table names to match `ystart` | Four digit year of first survey included in search, where ystart >= 1999. `ystop` | Four digit year of final survey included in search, where ystop >= ystart. `includerdc` | If TRUE then RDC only tables are included (default=FALSE). `includewithdrawn` | IF TRUE then withdrawn tables are included (default=FALSE). `nchar` | Truncates the variable description to a max length of nchar. `details` | If TRUE then complete table information from the comprehensive data list is returned (default=FALSE). ``` -------------------------------- ### nhanesAttr Source: https://cran.r-project.org/web/packages/nhanesA/refman/nhanesA.html Checks the size and other characteristics of a data table before importing into R. It downloads the specified table, determines its characteristics, and then deletes the table. It can use a local mirror if the NHANES_TABLE_BASE environment variable is set. ```APIDOC ## nhanesAttr ### Description Checks the size and other characteristics of a data table before importing into R. Downloads a table from the NHANES website as is, then determines characteristics, and deletes the table. Can use a local mirror if `NHANES_TABLE_BASE` environment variable is set. ### Value Returns a list with the following attributes: - `nrow`: number of rows - `ncol`: number of columns - `names`: name of each column - `unique`: true if all SEQN values are unique - `na`: number of 'NA' cells in the table - `size`: total size of table in bytes - `types`: data types of each column ### Examples ```R # Not run: bpx_e = nhanesAttr('BPX_E') # Not run: length(bpx_e) # Not run: folate_f = nhanesAttr('FOLATE_F') # Not run: length(folate_f) ``` ``` -------------------------------- ### List Variables for Pre-Pandemic Table Source: https://cran.r-project.org/web/packages/nhanesA/vignettes/Introducing_nhanesA.Rmd Retrieve the variable names for a specific pre-pandemic NHANES table. ```r #List table variables nhanesTableVars('EXAM', 'P_AUX', namesonly=TRUE) ``` -------------------------------- ### Prepare Data for Coefficient Visualization Source: https://cran.r-project.org/web/packages/nhanesA/vignettes/UsingSurveyWeights.rmd Formats coefficient and standard error data into data frames suitable for plotting with ggplot2, distinguishing between weighted and unweighted estimates. ```r unweighted_df <- data.frame(Variable = names(unweighted_coefs), Estimate = unweighted_coefs, SE = unweighted_se, Type = "Unweighted") weighted_df <- data.frame(Variable = names(unweighted_coefs), Estimate = weighted_coefs, SE = weighted_se, Type = "Weighted") plot_data <- rbind(unweighted_df, weighted_df) ``` -------------------------------- ### nhanesTables Source: https://cran.r-project.org/web/packages/nhanesA/refman/nhanesA.html Retrieves a list of tables and a description of their contents from the NHANES website. This provides a convenient way to browse the available tables. ```APIDOC ## nhanesTables ### Description Returns a list of table names for the specified survey group. ### Usage ``` nhanesTables( data_group, year, nchar = 128, details = FALSE, namesonly = FALSE, includerdc = FALSE ) ``` ### Arguments `data_group` | The type of survey (DEMOGRAPHICS, DIETARY, EXAMINATION, LABORATORY, QUESTIONNAIRE). Abbreviated terms may also be used: (DEMO, DIET, EXAM, LAB, Q). `year` | The year in yyyy format where 1999 <= yyyy. `nchar` | Truncates the table description to a max length of nchar. `details` | If TRUE then a more detailed description of the tables is returned (default=FALSE). `namesonly` | If TRUE then only the table names are returned (default=FALSE). `includerdc` | If TRUE then RDC only tables are included in list (default=FALSE). ### Details Function nhanesTables retrieves a list of tables and a description of their contents from the NHANES website. This provides a convenient way to browse the available tables. NULL is returned when an HTML read error is encountered. ### Value Returns a data frame that contains table attributes. If namesonly=TRUE, then a character vector of table names is returned. ### Examples ```R exam = nhanesTables('EXAM', 2007) dim(exam) lab = nhanesTables('LAB', 2009, details=TRUE, includerdc=TRUE) dim(lab) q = nhanesTables('Q', 2005, namesonly=TRUE) length(q) diet = nhanesTables('DIET', 'P') dim(diet) exam = nhanesTables('EXAM', 'Y') dim(exam) ``` ``` -------------------------------- ### Download NHANES Table with Translations Disabled Source: https://cran.r-project.org/web/packages/nhanesA/vignettes/Introducing_nhanesA.Rmd Download a specific NHANES table without applying default code translations. This is the first step in a manual translation process. ```r bpx_d <- nhanes('BPX_D', translate=FALSE) head(bpx_d[,6:11]) ``` -------------------------------- ### Simulate Merged NHANES Data Display Source: https://cran.r-project.org/web/packages/nhanesA/vignettes/Introducing_nhanesA.Rmd This code simulates the output of merging and selecting NHANES data, displaying a subset of rows and specified columns. ```r df <- data.frame(matrix(1,nrow=4, ncol=6)) names(df) <- c('RIAGENDR', 'BMXHT', 'BMXWT', 'BMXLEG', 'BMXCALF', 'BMXTHICR') df[1,] <- list('Female', 156.0, 75.2, 38.0, 36.6, 53.7) df[2,] <- list('Male', 167.6, 69.5, 40.4, 35.6, 48.0) df[3,] <- list('Female', 163.7, 45.0, 39.2, 31.7, 41.3) df[4,] <- list('Male', 182.4, 101.9, 41.5, 42.6, 50.5) print(df,row.names=FALSE) ``` -------------------------------- ### Display Translated Data Source: https://cran.r-project.org/web/packages/nhanesA/vignettes/Introducing_nhanesA.Rmd View the first few rows of a data frame after custom code translations have been applied. ```r head(bpx_d[,6:11]) ``` -------------------------------- ### Table Variables Source: https://cran.r-project.org/web/packages/nhanesA/refman/nhanesA.html Displays a list of variables in the specified NHANES table, enabling quick display of table variables and their definitions. ```APIDOC ## nhanesTableVars ### Description Displays a list of variables in the specified NHANES table. Enables quick display of table variables and their definitions. ### Usage ```R nhanesTableVars( data_group, nh_table, details = FALSE, nchar = 128, namesonly = FALSE ) ``` ### Arguments - `data_group` (character string) - The type of survey (DEMOGRAPHICS, DIETARY, EXAMINATION, LABORATORY, QUESTIONNAIRE). Abbreviated terms may also be used: (DEMO, DIET, EXAM, LAB, Q). - `nh_table` (character string) - The name of the specific table to retrieve. - `details` (logical, optional) - If TRUE then all columns in the variable description are returned (default=FALSE). - `nchar` (numeric, optional) - The number of characters in the Variable Description to print. Default length is 128, which is set to enhance readability cause variable descriptions can be very long. - `namesonly` (logical, optional) - If TRUE then only the variable names are returned (default=FALSE). ### Details NHANES tables may contain more than 100 variables. Function nhanesTableVars provides a concise display of variables for a specified table, which helps to ascertain quickly if the table is of interest. NULL is returned when an HTML read error is encountered. ### Value Returns a data frame that describes variable attributes for the specified table. If namesonly=TRUE, then a character vector of the variable names is returned. ``` -------------------------------- ### Download NHANES Table Source: https://cran.r-project.org/web/packages/nhanesA/refman/nhanesA.html Downloads NHANES data tables in SAS format and returns them as a data frame. Handles SAS labels and translated variables. Can optionally cleanse numeric variables. ```R bpx_e = nhanes('BPX_E') dim(bpx_e) folate_f = nhanes('FOLATE_F', includelabels = TRUE) dim(folate_f) ``` -------------------------------- ### Import NHANES DXA Data Source: https://cran.r-project.org/web/packages/nhanesA/refman/nhanesA.html Imports Dual Energy X-ray Absorptiometry (DXA) data for a specified year (1999-2006). Can optionally retrieve supplemental data and save it to a destination file. Handles potential website unavailability with a try-catch block. ```R ## May fail if CDC website is unavailable try({ dxa_b <- nhanesDXA(2001) dxa_c_s <- nhanesDXA(2003, suppl = TRUE) }) ## To save to a local file ## dxa <- nhanesDXA(1999, destfile = "dxx.xpt") ``` -------------------------------- ### nhanesCodebook Source: https://cran.r-project.org/web/packages/nhanesA/refman/nhanesA.html Returns the full NHANES codebook for a selected variable, including Variable Name, SAS Label, English Text, Target, and Value distribution. It can use a local mirror if the NHANES_TABLE_BASE environment variable is set. ```APIDOC ## nhanesCodebook ### Description Returns full NHANES codebook including Variable Name, SAS Label, English Text, Target, and Value distribution. ### Usage ```R nhanesCodebook(nh_table, colname = NULL, dxa = FALSE) ``` ### Arguments - `nh_table` (character): The name of the NHANES table that contains the desired variable. - `colname` (character, optional): The name of the table column (variable). - `dxa` (logical, optional): If TRUE then the 2005-2006 DXA codebook will be used (default=FALSE). ### Details Each NHANES variable has a codebook that provides a basic description as well as the distribution or range of values. This function returns the full codebook information for the selected variable. If the environment variable `NHANES_TABLE_BASE` was set during startup, the value of this variable is used as the base URL instead of https://wwwn.cdc.gov. ### Value The codebook is returned as a list object. Returns NULL upon error. ### Examples ```R # May fail if CDC website is unavailable try({ nhanesCodebook('AUX_D', 'AUQ020D') |> print() nhanesCodebook('BPX_J', 'BPACSZ') |> print() bpx_code = nhanesCodebook('BPX_J') length(bpx_code) }) ``` ``` -------------------------------- ### Table Summary Source: https://cran.r-project.org/web/packages/nhanesA/refman/nhanesA.html Computes a per-variable summary of a NHANES table either using the actual data or its corresponding codebook. ```APIDOC ## nhanesTableSummary ### Description Computes a per-variable summary of a NHANES table either using the actual data or its corresponding codebook. ### Usage ```R nhanesTableSummary(nh_table, use = c("data", "codebook", "both"), ...) ``` ### Arguments - `nh_table` (character string) - The name of a valid NHANES table. - `use` (character string, optional) - Whether to create a summary from the data itself (`"data"`) or the codebook (`"codebook"`). If `use = "both"`, both are computed and merged. Defaults to `c("data", "codebook", "both")`. - `...` - Additional arguments, usually passed on to either `nhanes` or `nhanesCodebook` as appropriate. Alternatively, the `src` argument can be used to pass on an already available data frame or codebook, but this must be consistent with the `use` argument. ### Details This function computes useful summaries of each variable included in the specified NHANES table. The resulting data frame contains one row for each variable in the table, and includes summary measures that indicate the total number of observations, the number of missing observations, whether the variable is most likely numeric or categorical, whether the variable is related to skipping other variables, etc. The precise details depend on the `use` argument and are subject to change. ### Value A data frame with one row per variable, with columns depending on the value of the `use` argument. ### Examples ```R ## May fail if CDC website is unavailable try(nhanesTableSummary('DEMO_D', use = "data")) try(nhanesTableSummary('DEMO_D', use = "codebook")) ``` ``` -------------------------------- ### Import NHANES Tables Source: https://cran.r-project.org/web/packages/nhanesA/vignettes/Introducing_nhanesA.Rmd Use the `nhanes` function to import specified NHANES tables into R data frames. This is the primary step for data analysis. ```r bmx_d <- nhanes('BMX_D') demo_d <- nhanes('DEMO_D') ``` -------------------------------- ### Search for Pre-Pandemic NHANES Tables Source: https://cran.r-project.org/web/packages/nhanesA/vignettes/Introducing_nhanesA.Rmd List table names that match a pattern, specifically for pre-pandemic data indicated by the 'P_' prefix. ```r #List all pre-pandemic tables nhanesSearchTableNames('^P_') ``` -------------------------------- ### Browse NHANES Website Source: https://cran.r-project.org/web/packages/nhanesA/refman/nhanesA.html Opens a web browser to the specified NHANES site. Can direct to a specific year, survey, or table. Defaults to the main data sets page if no arguments are provided. ```R browseNHANES(browse = FALSE) # Defaults to the main data sets page ``` ```R browseNHANES(2005) # The main page for the specified survey year ``` ```R browseNHANES(2009, 'EXAM') # Page for the specified year and survey group ``` ```R browseNHANES(nh_table = 'VIX_D') # Page for a specific table ``` ```R browseNHANES(nh_table = 'DXA') # DXA main page ``` -------------------------------- ### Extract and Compare Regression Coefficients Source: https://cran.r-project.org/web/packages/nhanesA/vignettes/UsingSurveyWeights.rmd Extracts coefficients and standard errors from both weighted and unweighted models and creates a comparison data frame. Ensure models are fitted first. ```r # For the unweighted model unweighted_summary <- summary(unweighted_model) unweighted_coefs <- unweighted_summary$coefficients[, "Estimate"] unweighted_se <- unweighted_summary$coefficients[, "Std. Error"] # For the weighted model weighted_summary <- summary(weighted_model) weighted_coefs <- as.numeric(weighted_summary$coefficients[, "Estimate"]) weighted_se <- as.numeric(weighted_summary$coefficients[, "Std. Error"]) comparison <- data.frame( Variable = names(unweighted_coefs), Unweighted = unweighted_coefs, Weighted = as.numeric(weighted_coefs) ) print(comparison) ``` -------------------------------- ### Import Supplemental DXA Data Source: https://cran.r-project.org/web/packages/nhanesA/vignettes/Introducing_nhanesA.Rmd Import supplemental DXA data for a specified year. Supplemental data may contain additional or different measurements. ```r #Import supplemental data dxx_c_s <- nhanesDXA(2003, suppl=TRUE) ``` -------------------------------- ### Search Variable Name Source: https://cran.r-project.org/web/packages/nhanesA/refman/nhanesA.html Returns a list of table names that contain the specified variable. ```APIDOC ## nhanesSearchVarName ### Description Returns a list of table names that contain the specified variable. ### Usage ```R nhanesSearchVarName( varname = NULL, ystart = NULL, ystop = NULL, includerdc = FALSE, nchar = 128, namesonly = TRUE ) ``` ### Arguments - `varname` (character string) - Name of variable to match. - `ystart` (numeric, optional) - Four digit year of first survey included in search, where ystart >= 1999. - `ystop` (numeric, optional) - Four digit year of final survey included in search, where ystop >= ystart. - `includerdc` (logical, optional) - If TRUE then RDC only tables are included in list (default=FALSE). - `nchar` (numeric, optional) - Truncates the variable description to a max length of nchar (default=128). - `namesonly` (logical, optional) - If TRUE then only the table names are returned (default=TRUE). ### Details The NHANES Comprehensive Variable List is scanned to find all data tables that contain the given variable name. Only a single variable name may be entered, and only exact matches will be found. ### Value By default, a character vector of table names that include the specified variable is returned. If namesonly=FALSE, then a data frame of table attributes is returned. ### Examples ```R bmxleg = nhanesSearchVarName('BMXLEG') length(bmxleg) bmxhead = nhanesSearchVarName('BMXHEAD', ystart=2003) length(bmxhead) ``` ``` -------------------------------- ### Fit Weighted and Unweighted Regression Models Source: https://cran.r-project.org/web/packages/nhanesA/vignettes/UsingSurveyWeights.rmd Fits both standard and survey-weighted generalized linear models. Requires 'datasub' for unweighted and 'dfsub' for weighted design. ```r unweighted_model <- glm(BPXDI1 ~ RIDAGEYR + RIDRETH1, data = datasub, family = gaussian()) weighted_model <- svyglm(BPXDI1 ~ RIDAGEYR + RIDRETH1, design = dfsub, family = gaussian()) ``` -------------------------------- ### List NHANES Tables Source: https://cran.r-project.org/web/packages/nhanesA/refman/nhanesA.html Lists available NHANES tables for a given survey group and year. Useful for browsing data availability. ```R exam = nhanesTables('EXAM', 2007) dim(exam) ``` ```R lab = nhanesTables('LAB', 2009, details=TRUE, includerdc=TRUE) dim(lab) ``` ```R q = nhanesTables('Q', 2005, namesonly=TRUE) length(q) ``` ```R diet = nhanesTables('DIET', 'P') dim(diet) ``` ```R exam = nhanesTables('EXAM', 'Y') dim(exam) ``` -------------------------------- ### Search Table Names Source: https://cran.r-project.org/web/packages/nhanesA/refman/nhanesA.html Searches the Doc File field in the NHANES Comprehensive Data List for tables that match a given name pattern. Only a single pattern may be entered. ```APIDOC ## nhanesSearchTableNames ### Description Searches the Doc File field in the NHANES Comprehensive Data List (see https://wwwn.cdc.gov/nchs/nhanes/search/DataPage.aspx) for tables that match a given name pattern. Only a single pattern may be entered. ### Arguments - `pattern` (character string) - The pattern to search for in table names. - `includerdc` (logical, optional) - If TRUE, include RDC-only tables (default=FALSE). - `details` (logical, optional) - If TRUE, return a data frame of table attributes (default=FALSE). ### Value Returns a character vector of table names that match the given pattern. If details=TRUE, then a data frame of table attributes is returned. NULL is returned when an HTML read error is encountered. ### Examples ```R bmx = nhanesSearchTableNames('BMX') length(bmx) hepbd = nhanesSearchTableNames('HEPBD') length(hepbd) hpvs = nhanesSearchTableNames('HPVS', includerdc=TRUE, details=TRUE) dim(hpvs) ``` ``` -------------------------------- ### Display NHANES Variable Codebook Source: https://cran.r-project.org/web/packages/nhanesA/vignettes/Introducing_nhanesA.Rmd Use `nhanesCodebook` to retrieve and display the codebook for a specific variable within an NHANES table. This provides variable descriptions and value distributions. ```r nhanesCodebook('DEMO_D', 'RIAGENDR') ``` -------------------------------- ### Search NHANES Variables by Description Source: https://cran.r-project.org/web/packages/nhanesA/vignettes/Introducing_nhanesA.Rmd Use nhanesSearch to find variables based on their descriptions. You can specify search terms, exclude terms, restrict the survey range, and control the output format (e.g., return only table names). ```r # nhanesSearch use examples # # Search on the word bladder, restrict to the 2001-2008 surveys, # print out 50 characters of the variable description nhanesSearch("bladder", ystart=2001, ystop=2008, nchar=50) # # Search on "urin" (will match urine, urinary, etc), from 1999-2010, return table names only nhanesSearch("urin", ignore.case=TRUE, ystop=2010, namesonly=TRUE) # # Search on "urin", exclude "During", search surveys from 1999-2010, return table names only nhanesSearch("urin", exclude_terms="during", ignore.case=TRUE, ystop=2010, namesonly=TRUE) # # Restrict search to 'EXAM' and 'LAB' data groups. Explicitly list matching and exclude terms, leave ignore.case set to default value of FALSE. Search surveys from 2009 to present. nhanesSearch(c("urin", "Urin"), exclude_terms=c("During", "eaten during", "do during"), data_group=c('EXAM', 'LAB'), ystart=2009) # # Search on "tooth" or "teeth", all years nhanesSearch(c("tooth", "teeth"), ignore.case=TRUE) # # Search for variables where the variable description begins with "Tooth" nhanesSearch("^Tooth") ``` -------------------------------- ### List Variables in NHANES Table Source: https://cran.r-project.org/web/packages/nhanesA/vignettes/Introducing_nhanesA.Rmd Use `nhanesTableVars` to display detailed information about the columns within a specified NHANES table. This helps in understanding the table's content before importing. ```r nhanesTableVars('EXAM', 'BMX_D') ``` -------------------------------- ### List of Body Measures Tables Source: https://cran.r-project.org/web/packages/nhanesA/vignettes/Introducing_nhanesA.Rmd This code displays a predefined list of body measures tables, likely generated from a previous search or for demonstration purposes. ```r bpxtables <- c("BMX_D", "BMX", "BMX_E", "BMX_C", "BMX_B", "BMX_F", "BMX_H", "BMX_G", "BMX_I", "BMX_J", "P_BMX") bpxtables ```