National Survey of Family Growth (NSFG)
The National Survey of Family Growth (NSFG) is the principal survey to measure reproductive behavior in the United States population.
Multiple tables with one row per respondent for the female and male tables, then a separate table with one row per pregnancy.
A complex sample survey designed to generalize to the 15-44 year old population of the United States, by gender.
Released every couple of years since 1973.
Administered by the Centers for Disease Control and Prevention.
Simplified Download and Importation
The R lodown
package easily downloads and imports all available NSFG microdata by simply specifying "nsfg"
with an output_dir =
parameter in the lodown()
function. Depending on your internet connection and computer processing speed, you might prefer to run this step overnight.
library(lodown)
lodown( "nsfg" , output_dir = file.path( path.expand( "~" ) , "NSFG" ) )
lodown
also provides a catalog of available microdata extracts with the get_catalog()
function. After requesting the NSFG catalog, you could pass a subsetted catalog through the lodown()
function in order to download and import specific extracts (rather than all available extracts).
library(lodown)
# examine all available NSFG microdata files
nsfg_cat <-
get_catalog( "nsfg" ,
output_dir = file.path( path.expand( "~" ) , "NSFG" ) )
# 2013-2015 only
nsfg_cat <- subset( nsfg_cat , grepl( "2013_2015" , full_url ) )
# download the microdata to your local computer
nsfg_cat <- lodown( "nsfg" , nsfg_cat )
Analysis Examples with the survey
library
Construct a complex sample survey design:
options( survey.lonely.psu = "adjust" )
library(survey)
nsfg_df <- readRDS( file.path( path.expand( "~" ) , "NSFG" , "2013_2015_FemRespData.rds" ) )
nsfg_design <-
svydesign(
id = ~ secu ,
strata = ~ sest ,
data = nsfg_df ,
weights = ~ wgt2013_2015 ,
nest = TRUE
)
Variable Recoding
Add new columns to the data set:
nsfg_design <-
update(
nsfg_design ,
one = 1 ,
birth_control_pill = as.numeric( constat1 == 6 ) ,
age_categories =
factor( findInterval( ager , c( 15 , 20 , 25 , 30 , 35 , 40 ) ) ,
labels = c( '15-19' , '20-24' , '25-29' , '30-34' , '35-39' , '40-44' ) ) ,
marstat =
factor( marstat , levels = c( 1:6 , 8:9 ) ,
labels = c(
"Married to a person of the opposite sex" ,
"Not married but living together with a partner of the opposite sex" ,
"Widowed" ,
"Divorced or annulled" ,
"Separated, because you and your spouse are not getting along" ,
"Never been married" ,
"Refused" ,
"Don't know" )
)
)
Unweighted Counts
Count the unweighted number of records in the survey sample, overall and by groups:
sum( weights( nsfg_design , "sampling" ) != 0 )
svyby( ~ one , ~ age_categories , nsfg_design , unwtd.count )
Weighted Counts
Count the weighted size of the generalizable population, overall and by groups:
svytotal( ~ one , nsfg_design )
svyby( ~ one , ~ age_categories , nsfg_design , svytotal )
Descriptive Statistics
Calculate the mean (average) of a linear variable, overall and by groups:
svymean( ~ npregs_s , nsfg_design , na.rm = TRUE )
svyby( ~ npregs_s , ~ age_categories , nsfg_design , svymean , na.rm = TRUE )
Calculate the distribution of a categorical variable, overall and by groups:
svymean( ~ marstat , nsfg_design )
svyby( ~ marstat , ~ age_categories , nsfg_design , svymean )
Calculate the sum of a linear variable, overall and by groups:
svytotal( ~ npregs_s , nsfg_design , na.rm = TRUE )
svyby( ~ npregs_s , ~ age_categories , nsfg_design , svytotal , na.rm = TRUE )
Calculate the weighted sum of a categorical variable, overall and by groups:
svytotal( ~ marstat , nsfg_design )
svyby( ~ marstat , ~ age_categories , nsfg_design , svytotal )
Calculate the median (50th percentile) of a linear variable, overall and by groups:
svyquantile( ~ npregs_s , nsfg_design , 0.5 , na.rm = TRUE )
svyby(
~ npregs_s ,
~ age_categories ,
nsfg_design ,
svyquantile ,
0.5 ,
ci = TRUE ,
keep.var = TRUE ,
na.rm = TRUE
)
Estimate a ratio:
svyratio(
numerator = ~ npregs_s ,
denominator = ~ nbabes_s ,
nsfg_design ,
na.rm = TRUE
)
Subsetting
Restrict the survey design to ever cohabited:
sub_nsfg_design <- subset( nsfg_design , timescoh > 0 )
Calculate the mean (average) of this subset:
svymean( ~ npregs_s , sub_nsfg_design , na.rm = TRUE )
Measures of Uncertainty
Extract the coefficient, standard error, confidence interval, and coefficient of variation from any descriptive statistics function result, overall and by groups:
this_result <- svymean( ~ npregs_s , nsfg_design , na.rm = TRUE )
coef( this_result )
SE( this_result )
confint( this_result )
cv( this_result )
grouped_result <-
svyby(
~ npregs_s ,
~ age_categories ,
nsfg_design ,
svymean ,
na.rm = TRUE
)
coef( grouped_result )
SE( grouped_result )
confint( grouped_result )
cv( grouped_result )
Calculate the degrees of freedom of any survey design object:
degf( nsfg_design )
Calculate the complex sample survey-adjusted variance of any statistic:
svyvar( ~ npregs_s , nsfg_design , na.rm = TRUE )
Include the complex sample design effect in the result for a specific statistic:
# SRS without replacement
svymean( ~ npregs_s , nsfg_design , na.rm = TRUE , deff = TRUE )
# SRS with replacement
svymean( ~ npregs_s , nsfg_design , na.rm = TRUE , deff = "replace" )
Compute confidence intervals for proportions using methods that may be more accurate near 0 and 1. See ?svyciprop
for alternatives:
svyciprop( ~ birth_control_pill , nsfg_design ,
method = "likelihood" , na.rm = TRUE )
Regression Models and Tests of Association
Perform a design-based t-test:
svyttest( npregs_s ~ birth_control_pill , nsfg_design )
Perform a chi-squared test of association for survey data:
svychisq(
~ birth_control_pill + marstat ,
nsfg_design
)
Perform a survey-weighted generalized linear model:
glm_result <-
svyglm(
npregs_s ~ birth_control_pill + marstat ,
nsfg_design
)
summary( glm_result )
Analysis Examples with srvyr
The R srvyr
library calculates summary statistics from survey data, such as the mean, total or quantile using dplyr-like syntax. srvyr allows for the use of many verbs, such as summarize
, group_by
, and mutate
, the convenience of pipe-able functions, the tidyverse
style of non-standard evaluation and more consistent return types than the survey
package. This vignette details the available features. As a starting point for NSFG users, this code replicates previously-presented examples:
library(srvyr)
nsfg_srvyr_design <- as_survey( nsfg_design )
Calculate the mean (average) of a linear variable, overall and by groups:
nsfg_srvyr_design %>%
summarize( mean = survey_mean( npregs_s , na.rm = TRUE ) )
nsfg_srvyr_design %>%
group_by( age_categories ) %>%
summarize( mean = survey_mean( npregs_s , na.rm = TRUE ) )
Replication Example
The example below matches statistics and standard errors from this table pulled from the CDC variance estimation examples:
Match the sum of the weights:
stopifnot( round( coef( svytotal( ~ one , nsfg_design ) ) , 0 ) == 61491766 )
Compute the statistics and standard errors for percentage of women currently using the pill by age:
row_percents <- c( 16.1723 , 27.8173 , 18.7197 , 13.4367 , 9.0851 , 7.7224 )
std_err_row_percents <- c( 1.5414 , 2.4162 , 1.9890 , 1.5074 , 1.5222 , 1.6053 )
results <- svyby( ~ birth_control_pill , ~ age_categories , nsfg_design , svymean )
stopifnot( all( round( coef( results ) * 100 , 4 ) == row_percents ) )
stopifnot( all( round( SE( results ) * 100 , 4 ) == std_err_row_percents ) )