In [14]:
# YRBS survey analysis 
# Lots of help, inspiration, and borrowed (copy and pasted) code from asdfree.com:
#    https://github.com/ajdamico/asdfree/blob/master/Youth%20Risk%20Behavior%20Surveillance%20System/replicate%20cdc%20software%20for%20analysis%20of%20yrbs%20data%20publication.R#L73-L91
In [16]:
# Dependencies
library(jsonlite)
library(survey)
library(RCurl)
In [2]:
# Config variables
fullDatasetBaseUrl <- "https://owh.demo.socrata.com/resource/j6wc-x2ek.json?$limit=100000000000"
In [3]:
# Relevant filters from full dataset
filterParams <- "&year=2013&sitetype=National"

# Fetch and store filtered sample
sample <- fromJSON(paste(fullDatasetBaseUrl,filterParams, sep = ''))
nrow(sample)
Out[3]:
13583
In [12]:
sample <- subset(sample, !is.na(q8))
sample <- transform(sample, weight = as.numeric(weight))
sample <- transform(sample, q8 = as.numeric(q8))

yrbsSurvey <- 
    svydesign( 
        ~psu , 
        strata = ~stratum, 
        data = sample, 
        weights = ~weight,
        nest = TRUE
)
In [10]:
( helmet <- svymean( ~as.numeric( qn8 == 1 ) , yrbsSurvey , na.rm = TRUE ) )
Out[10]:
                        mean     SE
as.numeric(qn8 == 1) 0.87873 0.0129
In [ ]: