# Behavioral Risk Factor Surveillance System (BRFSS) {-}

[![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0) <a href="https://github.com/asdfree/brfss/actions"><img src="https://github.com/asdfree/brfss/actions/workflows/r.yml/badge.svg" alt="Github Actions Badge"></a> 

A health behavior telephone interview survey with enough sample size to examine all fifty states.

* One table with one row per telephone respondent.

* A complex survey designed to generalize to the civilian non-institutional adult population of the U.S.

* Released annually since 1984 but all states did not participate until 1994.

* Administered by the [Centers for Disease Control and Prevention](http://www.cdc.gov/).

---

Please skim before you begin:

1. [BRFSS Data User Guide](https://www.cdc.gov/brfss/data_documentation/pdf/UserguideJune2013.pdf)

2. [Wikipedia Entry](https://en.wikipedia.org/wiki/Behavioral_Risk_Factor_Surveillance_System)

3. A haiku regarding this microdata:

```{r}
# a cellphone vibrates
# it's the cdc! asking
# if you ate veggies
```

---

## Download, Import, Preparation {-}

Download and import the national file:
```{r eval = FALSE , results = "hide" }
library(haven)

zip_tf <- tempfile()

zip_url <- "https://www.cdc.gov/brfss/annual_data/2023/files/LLCP2023XPT.zip"
	
download.file( zip_url , zip_tf , mode = 'wb' )

brfss_tbl <- read_xpt( zip_tf )

brfss_df <- data.frame( brfss_tbl )

names( brfss_df ) <- tolower( names( brfss_df ) )

brfss_df[ , 'one' ] <- 1
```

### Save Locally \ {-}

Save the object at any point:

```{r eval = FALSE , results = "hide" }
# brfss_fn <- file.path( path.expand( "~" ) , "BRFSS" , "this_file.rds" )
# saveRDS( brfss_df , file = brfss_fn , compress = FALSE )
```

Load the same object:

```{r eval = FALSE , results = "hide" }
# brfss_df <- readRDS( brfss_fn )
```

### Survey Design Definition {-}
Construct a complex sample survey design:

```{r eval = FALSE , results = "hide" }
library(survey)

options( survey.lonely.psu = "adjust" )

variables_to_keep <-
	c( 'one' , 'x_psu' , 'x_ststr' , 'x_llcpwt' , 'genhlth' , 'medcost1' , 
	'x_state' , 'x_age80' , 'physhlth' , 'menthlth' , 'x_hlthpl1' )
	
brfss_df <- brfss_df[ variables_to_keep ]
	
brfss_national_design <-
	svydesign(
		id = ~ x_psu ,
		strata = ~ x_ststr ,
		data = brfss_df ,
		weight = ~ x_llcpwt ,
		nest = TRUE
	)
```

Since large linearized survey designs execute slowly, a replication design might be preferrable for exploratory analysis. Coefficients (such as means and medians) do not change, standard errors and confidence intervals differ slightly. The initial conversion with `as.svrepdesign` requires an extended period of processing time (perhaps run once overnight), subsequent analyses will finish much faster:

```{r eval = FALSE , results = "hide" }
# brfss_replication_design <-
# 	as.svrepdesign( 
# 		brfss_national_design ,
# 		type = 'bootstrap'
# 	)

# system.time( print( svymean( ~ x_age80 , brfss_national_design ) ) )

# system.time( print( svymean( ~ x_age80 , brfss_replication_design ) ) )
```

**In this example, limit the national design to only Alaska for quicker processing:**

```{r eval = FALSE , results = "hide" }
brfss_design <-
	subset(
		brfss_national_design , 
		x_state %in% 2 
	)
```

### Variable Recoding {-}

Add new columns to the data set:
```{r eval = FALSE , results = "hide" }
brfss_design <- 
	update( 
		brfss_design ,
		
		fair_or_poor_health = ifelse( genhlth %in% 1:5 , as.numeric( genhlth > 3 ) , NA ) ,
		
		no_doc_visit_due_to_cost = 
			factor( 
				medcost1 , 
				levels = c( 1 , 2 , 7 , 9 ) , 
				labels = c( "yes" , "no" , "dk" , "rf" ) 
			) ,
		
		physhlth_days_not_good = 
			ifelse( physhlth <= 30 , physhlth ,
			ifelse( physhlth == 88 , 0 , NA ) ) ,
			
		menthlth_days_not_good = 
			ifelse( menthlth <= 30 , menthlth ,
			ifelse( menthlth == 88 , 0 , NA ) ) ,
			
		
		state_name =
		
			factor(
			
				x_state ,
				
				levels = 
					c(1, 2, 4, 5, 6, 8, 9, 10, 11, 12, 13, 15, 16, 17, 18, 19, 20, 
					21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 
					37, 38, 39, 40, 41, 42, 44, 45, 46, 47, 48, 49, 50, 51, 53, 54, 
					55, 56, 66, 72, 78) ,
					
				labels = 
					c("ALABAMA", "ALASKA", "ARIZONA", "ARKANSAS", "CALIFORNIA", 
					"COLORADO", "CONNECTICUT", "DELAWARE", "DISTRICT OF COLUMBIA", 
					"FLORIDA", "GEORGIA", "HAWAII", "IDAHO", "ILLINOIS", "INDIANA",
					"IOWA", "KANSAS", "KENTUCKY", "LOUISIANA", "MAINE", "MARYLAND",
					"MASSACHUSETTS", "MICHIGAN", "MINNESOTA", "MISSISSIPPI", 
					"MISSOURI", "MONTANA", "NEBRASKA", "NEVADA", "NEW HAMPSHIRE",
					"NEW JERSEY", "NEW MEXICO", "NEW YORK", "NORTH CAROLINA", 
					"NORTH DAKOTA", "OHIO", "OKLAHOMA", "OREGON", "PENNSYLVANIA",
					"RHODE ISLAND", "SOUTH CAROLINA", "SOUTH DAKOTA", "TENNESSEE",
					"TEXAS", "UTAH", "VERMONT", "VIRGINIA", "WASHINGTON",
					"WEST VIRGINIA", "WISCONSIN", "WYOMING", "GUAM", "PUERTO RICO",
					"U.S. VIRGIN ISLANDS")
					
			)
	)
```

---

## Analysis Examples with the `survey` library \ {-}

### Unweighted Counts {-}

Count the unweighted number of records in the survey sample, overall and by groups:
```{r eval = FALSE , results = "hide" }
sum( weights( brfss_design , "sampling" ) != 0 )

svyby( ~ one , ~ state_name , brfss_design , unwtd.count )
```

### Weighted Counts {-}
Count the weighted size of the generalizable population, overall and by groups:
```{r eval = FALSE , results = "hide" }
svytotal( ~ one , brfss_design )

svyby( ~ one , ~ state_name , brfss_design , svytotal )
```

### Descriptive Statistics {-}

Calculate the mean (average) of a linear variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
svymean( ~ x_age80 , brfss_design )

svyby( ~ x_age80 , ~ state_name , brfss_design , svymean )
```

Calculate the distribution of a categorical variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
svymean( ~ no_doc_visit_due_to_cost , brfss_design , na.rm = TRUE )

svyby( ~ no_doc_visit_due_to_cost , ~ state_name , brfss_design , svymean , na.rm = TRUE )
```

Calculate the sum of a linear variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
svytotal( ~ x_age80 , brfss_design )

svyby( ~ x_age80 , ~ state_name , brfss_design , svytotal )
```

Calculate the weighted sum of a categorical variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
svytotal( ~ no_doc_visit_due_to_cost , brfss_design , na.rm = TRUE )

svyby( ~ no_doc_visit_due_to_cost , ~ state_name , brfss_design , svytotal , na.rm = TRUE )
```

Calculate the median (50th percentile) of a linear variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
svyquantile( ~ x_age80 , brfss_design , 0.5 )

svyby( 
	~ x_age80 , 
	~ state_name , 
	brfss_design , 
	svyquantile , 
	0.5 ,
	ci = TRUE 
)
```

Estimate a ratio:
```{r eval = FALSE , results = "hide" }
svyratio( 
	numerator = ~ physhlth_days_not_good , 
	denominator = ~ menthlth_days_not_good , 
	brfss_design ,
	na.rm = TRUE
)
```

### Subsetting {-}

Restrict the survey design to persons without health insurance:
```{r eval = FALSE , results = "hide" }
sub_brfss_design <- subset( brfss_design , x_hlthpl1 == 2 )
```
Calculate the mean (average) of this subset:
```{r eval = FALSE , results = "hide" }
svymean( ~ x_age80 , sub_brfss_design )
```

### Measures of Uncertainty {-}

Extract the coefficient, standard error, confidence interval, and coefficient of variation from any descriptive statistics function result, overall and by groups:
```{r eval = FALSE , results = "hide" }
this_result <- svymean( ~ x_age80 , brfss_design )

coef( this_result )
SE( this_result )
confint( this_result )
cv( this_result )

grouped_result <-
	svyby( 
		~ x_age80 , 
		~ state_name , 
		brfss_design , 
		svymean 
	)
	
coef( grouped_result )
SE( grouped_result )
confint( grouped_result )
cv( grouped_result )
```

Calculate the degrees of freedom of any survey design object:
```{r eval = FALSE , results = "hide" }
degf( brfss_design )
```

Calculate the complex sample survey-adjusted variance of any statistic:
```{r eval = FALSE , results = "hide" }
svyvar( ~ x_age80 , brfss_design )
```

Include the complex sample design effect in the result for a specific statistic:
```{r eval = FALSE , results = "hide" }
# SRS without replacement
svymean( ~ x_age80 , brfss_design , deff = TRUE )

# SRS with replacement
svymean( ~ x_age80 , brfss_design , deff = "replace" )
```

Compute confidence intervals for proportions using methods that may be more accurate near 0 and 1. See `?svyciprop` for alternatives:
```{r eval = FALSE , results = "hide" }
svyciprop( ~ fair_or_poor_health , brfss_design ,
	method = "likelihood" , na.rm = TRUE )
```

### Regression Models and Tests of Association {-}

Perform a design-based t-test:
```{r eval = FALSE , results = "hide" }
svyttest( x_age80 ~ fair_or_poor_health , brfss_design )
```

Perform a chi-squared test of association for survey data:
```{r eval = FALSE , results = "hide" }
svychisq( 
	~ fair_or_poor_health + no_doc_visit_due_to_cost , 
	brfss_design 
)
```

Perform a survey-weighted generalized linear model:
```{r eval = FALSE , results = "hide" }
glm_result <- 
	svyglm( 
		x_age80 ~ fair_or_poor_health + no_doc_visit_due_to_cost , 
		brfss_design 
	)

summary( glm_result )
```

---

## Replication Example {-}

This example matches Alaska's confidence intervals from the [BRFSS Prevalence & Trends Data](https://nccd.cdc.gov/BRFSSPrevalence/rdPage.aspx?rdReport=DPH_BRFSS.ExploreByTopic&irbLocationType=StatesAndMMSA&islClass=CLASS07&islTopic=TOPIC28&islYear=2023&rdRnd=24558):

```{r eval = FALSE , results = "hide" }

result <-
	svymean(
		~ no_doc_visit_due_to_cost ,
		subset(
			brfss_design ,
			no_doc_visit_due_to_cost %in%
				c( 'yes' , 'no' )
		) ,
		na.rm = TRUE
	)

stopifnot( round( coef( result )[1] , 3 ) == 0.111 )
stopifnot( round( confint( result )[ 1 , 1 ] , 3 ) == 0.098 )
stopifnot( round( confint( result )[ 1 , 2 ] , 3 ) == 0.123 )
```

---

## Analysis Examples with `srvyr` \ {-}

The R `srvyr` library calculates summary statistics from survey data, such as the mean, total or quantile using [dplyr](https://github.com/tidyverse/dplyr/)-like syntax. [srvyr](https://github.com/gergness/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](https://cran.r-project.org/web/packages/srvyr/vignettes/srvyr-vs-survey.html) details the available features. As a starting point for BRFSS users, this code replicates previously-presented examples:

```{r eval = FALSE , results = "hide" }
library(srvyr)
brfss_srvyr_design <- as_survey( brfss_design )
```
Calculate the mean (average) of a linear variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
brfss_srvyr_design %>%
	summarize( mean = survey_mean( x_age80 ) )

brfss_srvyr_design %>%
	group_by( state_name ) %>%
	summarize( mean = survey_mean( x_age80 ) )
```


