Youth Risk Behavior Surveillance System (YRBSS)
The high school edition of the Behavioral Risk Factor Surveillance System (BRFSS).
One table with one row per sampled youth respondent.
A complex sample survey designed to generalize to all public and private school students in grades 9-12 in the United States.
Released biennially since 1993.
Administered by the Centers for Disease Control and Prevention.
Please skim before you begin:
A haiku regarding this microdata:
Download, Import, Preparation
Load the SAScii
library to interpret a SAS input program, and also re-arrange the SAS input program:
library(SAScii)
sas_url <-
"https://www.cdc.gov/healthyyouth/data/yrbs/files/2019/2019XXH-SAS-Input-Program.sas"
sas_text <- tolower( readLines( sas_url ) )
# find the (out of numerical order)
# `site` location variable's position
# within the SAS input program
site_location <- which( sas_text == '@1 site $3.' )
# find the start field's position
# within the SAS input program
input_location <- which( sas_text == "input" )
# create a vector from 1 to the length of the text file
sas_length <- seq( length( sas_text ) )
# remove the site_location
sas_length <- sas_length[ -site_location ]
# re-insert the site variable's location
# immediately after the starting position
sas_reorder <-
c(
sas_length[ seq( input_location ) ] ,
site_location ,
sas_length[ seq( input_location + 1 , length( sas_length ) ) ]
)
# re-order the sas text file
sas_text <- sas_text[ sas_reorder ]
sas_tf <- tempfile()
writeLines( sas_text , sas_tf )
Download and import the national file:
dat_tf <- tempfile()
dat_url <-
"https://www.cdc.gov/healthyyouth/data/yrbs/files/2019/XXH2019_YRBS_Data.dat"
download.file( dat_url , dat_tf , mode = 'wb' )
yrbss_df <- read.SAScii( dat_tf , sas_tf )
names( yrbss_df ) <- tolower( names( yrbss_df ) )
yrbss_df[ , 'one' ] <- 1
Save Locally
Save the object at any point:
# yrbss_fn <- file.path( path.expand( "~" ) , "YRBSS" , "this_file.rds" )
# saveRDS( yrbss_df , file = yrbss_fn , compress = FALSE )
Load the same object:
Variable Recoding
Add new columns to the data set:
yrbss_design <-
update(
yrbss_design ,
q2 = q2 ,
never_rarely_wore_seat_belt = as.numeric( qn8 == 1 ) ,
ever_used_marijuana = as.numeric( qn45 == 1 ) ,
tried_to_quit_tobacco_past_year = as.numeric( q39 == 2 ) ,
used_tobacco_past_year = as.numeric( q39 > 1 )
)
Analysis Examples with the survey
library
Unweighted Counts
Count the unweighted number of records in the survey sample, overall and by groups:
Descriptive Statistics
Calculate the mean (average) of a linear variable, overall and by groups:
svymean( ~ bmipct , yrbss_design , na.rm = TRUE )
svyby( ~ bmipct , ~ ever_used_marijuana , yrbss_design , svymean , na.rm = TRUE )
Calculate the distribution of a categorical variable, overall and by groups:
svymean( ~ q2 , yrbss_design , na.rm = TRUE )
svyby( ~ q2 , ~ ever_used_marijuana , yrbss_design , svymean , na.rm = TRUE )
Calculate the sum of a linear variable, overall and by groups:
svytotal( ~ bmipct , yrbss_design , na.rm = TRUE )
svyby( ~ bmipct , ~ ever_used_marijuana , yrbss_design , svytotal , na.rm = TRUE )
Calculate the weighted sum of a categorical variable, overall and by groups:
svytotal( ~ q2 , yrbss_design , na.rm = TRUE )
svyby( ~ q2 , ~ ever_used_marijuana , yrbss_design , svytotal , na.rm = TRUE )
Calculate the median (50th percentile) of a linear variable, overall and by groups:
svyquantile( ~ bmipct , yrbss_design , 0.5 , na.rm = TRUE )
svyby(
~ bmipct ,
~ ever_used_marijuana ,
yrbss_design ,
svyquantile ,
0.5 ,
ci = TRUE , na.rm = TRUE
)
Estimate a ratio:
Subsetting
Restrict the survey design to youths who ever drank alcohol:
Calculate the mean (average) of this subset:
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( ~ bmipct , yrbss_design , na.rm = TRUE )
coef( this_result )
SE( this_result )
confint( this_result )
cv( this_result )
grouped_result <-
svyby(
~ bmipct ,
~ ever_used_marijuana ,
yrbss_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:
Calculate the complex sample survey-adjusted variance of any statistic:
Include the complex sample design effect in the result for a specific statistic:
# SRS without replacement
svymean( ~ bmipct , yrbss_design , na.rm = TRUE , deff = TRUE )
# SRS with replacement
svymean( ~ bmipct , yrbss_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:
Replication Example
This example matches statistics, standard errors, and confidence intervals from the “never/rarely wore seat belt” row of PDF page 29 of this CDC analysis software document:
unwtd_count_result <-
unwtd.count( ~ never_rarely_wore_seat_belt , yrbss_design )
stopifnot( coef( unwtd_count_result ) == 11149 )
wtd_n_result <-
svytotal(
~ one ,
subset(
yrbss_design ,
!is.na( never_rarely_wore_seat_belt )
)
)
stopifnot( round( coef( wtd_n_result ) , 0 ) == 12132 )
share_result <-
svymean(
~ never_rarely_wore_seat_belt ,
yrbss_design ,
na.rm = TRUE
)
stopifnot( round( coef( share_result ) , 4 ) == .0654 )
stopifnot( round( SE( share_result ) , 4 ) == .0065 )
ci_result <-
svyciprop(
~ never_rarely_wore_seat_belt ,
yrbss_design ,
na.rm = TRUE ,
method = "beta"
)
stopifnot( round( confint( ci_result )[1] , 4 ) == 0.0529 )
stopifnot( round( confint( ci_result )[2] , 2 ) == 0.08 )
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 YRBSS users, this code replicates previously-presented examples:
Calculate the mean (average) of a linear variable, overall and by groups: