nhanesA was developed to enable fully customizable retrieval of data from the National Health and Nutrition Examination Survey (NHANES). The survey is conducted by the National Center for Health Statistics (NCHS), and data are publicly available at: https://www.cdc.gov/nchs/nhanes.htm. NHANES data are reported in well over one thousand peer-reviewed journal publications every year.
Since 1999, the NHANES survey has been conducted continuously, and the surveys during that period are referred to as "continuous NHANES" to distinguish from several prior surveys. Continuous NHANES surveys are grouped in two-year intervals, with the first interval being 1999-2000.
Most NHANES data are in the form of tables in SAS 'XPT' format. The survey is grouped into five data categories that are publicly available, as well as an additional category (Limited access data) that requires written justification and prior approval before access. Package nhanesA is intended mostly for use with the publicly available data, but some information pertaining to the limited access data can also be retrieved.
The five publicly available data categories are: - Demographics (DEMO) - Dietary (DIET) - Examination (EXAM) - Laboratory (LAB) - Questionnaire (Q)
The abbreviated forms in parentheses may be substituted for the long form in nhanesA commands.
For limited access data, the available tables and variable names can be listed, but the data cannot be downloaded directly. To indicate limited access data in nhanesA functions, use: - Limited (LTD)
To quickly get familiar with NHANES data, it is helpful to display a listing of tables. Use nhanesTables to get information on tables that are available for a given category for a given year.
library(nhanesA) nhanesTables('EXAM', 2005)
Note that the two-year survey intervals begin with the odd year. For convenience, only a single 4-digit year is entered such that nhanesTables('EXAM', 2005) and nhanesTables('EXAM', 2006) yield identical output.
After viewing the output, we decide we are interested in table 'BMX_D' that contains body measures data. To better determine if that table is of interest, we can display detailed information on the table contents using nhanesTableVars.
nhanesTableVars('EXAM', 'BMX_D')
We see that there are r nrow(nhanesTableVars('EXAM', 'BMX_D')) columns in table BMX_D. The column SEQN is the respondent sequence number and is included in every NHANES table. Effectively, SEQN is a subject identifier that is used to join information across tables.
We now import BMX_D along with the demographics table DEMO_D.
bmx_d <- nhanes('BMX_D') demo_d <- nhanes('DEMO_D')
We merge the tables and display several variables:
bmx_demo <- merge(demo_d, bmx_d) options(digits=4) select_cols <- c('RIAGENDR', 'BMXHT', 'BMXWT', 'BMXLEG', 'BMXCALF', 'BMXTHICR') print(bmx_demo[5:8,select_cols], row.names=FALSE)
NHANES uses coded values for many fields. In the preceding example, gender is coded as 1 or 2. To determine what the values mean, we can list the code translations for the gender field RIAGENDR in table DEMO_D
nhanesTranslate('DEMO_D', 'RIAGENDR')
If desired, we can use nhanesTranslate to apply the code translation to demo_d directly by assigning data=demo_d.
demo_d <- nhanesTranslate('DEMO_D', 'RIAGENDR', data=demo_d) bmx_demo <- merge(demo_d, bmx_d)
The RIAGENDR field is now recoded as Male, Female instead of 1,2.
print(bmx_demo[5:8,select_cols], row.names=FALSE)
An NHANES table may have dozens of columns with coded values. Translating all possible columns is a three step process. 1: Download the table 2: Save a list of table variables 3: Pass the table and variable list to nhanesTranslate
bpx_d <- nhanes('BPX_D') head(bpx_d[,6:11]) bpx_d_vars <- nhanesTableVars('EXAM', 'BPX_D', namesonly=TRUE) #Alternatively may use bpx_d_vars = names(bpx_d) bpx_d <- suppressWarnings(nhanesTranslate('BPX_D', bpx_d_vars, data=bpx_d)) head(bpx_d[,6:11])
Some discretion should be applied when translating coded columns as code translations can be quite long. To improve readability the translation string is restricted to a default length of 32 but can be set as high as 128. Also, columns that have at least two categories (e.g. Male, Female) will be translated, but mincategories can be set to 1 to perform the translation even if only a single category is present.
The primary goal of nhanesA is to enable fully customizable processing of select NHANES tables. However, it is quite easy to download entire surveys using nhanesA functions. Say we want to download every questionnaire in the 2007-2008 survey. We first get a list of the table names by using nhanesTables with namesonly = TRUE. The tables can then be downloaded using nhanes with lapply.
q2007names <- nhanesTables('Q', 2007, namesonly=TRUE) q2007tables <- lapply(q2007names, nhanes) names(q2007tables) <- q2007names
Dual X-Ray Absorptiometry (DXA) data were acquired from 1999-2006. More information may be found at https://wwwn.cdc.gov/nchs/nhanes/dxa/dxa.aspx. By default the DXA data are imported into the R environment, however, because the tables are quite large it may be desirable to save the data to a local file then import to R as needed. When nhanesTranslate is applied to DXA data, only the 2005-2006 translation tables are used as those are the only DXA codes that are currently available in html format.
#Import into R dxx_b <- nhanesDXA(2001) #Save to file nhanesDXA(2001, destfile="dxx_b.xpt") #Import supplemental data dxx_c_s <- nhanesDXA(2003, suppl=TRUE) #Apply code translations dxalist <- c('DXAEXSTS', 'DXITOT', 'DXIHE') dxx_b <- nhanesTranslate(colnames=dxalist, data=dxx_b, dxa=TRUE)
If you are interested in working with accelerometer data from 2003-2006 then please see packages nhanesaccel https://r-forge.r-project.org/R/?group_id=1733 and accelerometry https://cran.r-project.org/package=accelerometry.
The NHANES repository is extensive, thus it is helpful to perform a targeted search to identify relevant tables and variables. Comprehensive lists of NHANES variables are maintained for each data group. For example, the demographics variables are available at https://wwwn.cdc.gov/nchs/nhanes/search/variablelist.aspx?Component=Demographics. The nhanesSearch function allows the investigator to input search terms, match against the comprehensive variable descriptions, and retrieve the list of matching variables. Matching search terms (variable descriptions must contain one of the terms) and exclusive search terms (variable descriptions must NOT contain any exclusive terms) may be provided. The search can be restricted to a specific survey range as well as specific data groups.
# 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")
nhanesSearch is a versatile search function as it imports the comprehensive variable lists to a data frame. That allows for detailed conditional extraction of the variables. However, each call to nhanesSearch takes up to a minute or more to process. Faster processing can be achieved when we know the name of a specific variable of interest and we look only for exact matches to the variable name. Function nhanesSearchVarName matches a given variable name in the html directly, then only the matching elements are converted to a data frame. Consequently, a call to nhanesSearchVarName executes much faster than nhanesSearch; typically under 30s. nhanesSearchVarName is useful for finding all data tables that contain a given variable.
#nhanesSearchVarName use examples nhanesSearchVarName('BPXPULS') nhanesSearchVarName('CSQ260i', includerdc=TRUE, nchar=38, namesonly=FALSE)
In order to group data across surveys, it is useful to list all available tables that follow a given naming pattern. Function nhanesSearchTableNames is used for such pattern matching. For example, if we want to work with all available body measures data we can retrieve the full list of available tables with nhanesSearchTableNames('BMX'). The search is conducted over the comprehensive table list, which is much smaller than the comprehensive variable list, such that a call to nhanesSearchTableNames takes only a few seconds.
# nhanesSearchTableNames use examples nhanesSearchTableNames('BMX') nhanesSearchTableNames('HPVS', includerdc=TRUE, nchar=42, details=TRUE)
Sincerely,
Christopher J. Endres
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.