Medical Expenditure Panel Survey (MEPS)
The Household Component captures person-level spending across service categories, coverage types.
The consolidated file contains one row per individual within each sampled household, other tables contain one record per event (like prescription fills, hospitalizations), per job, per insurance policy.
A complex sample survey designed to generalize to the U.S. civilian non-institutionalized population.
Released annually since 1996.
Administered by the Agency for Healthcare Research and Quality.
Please skim before you begin:
A haiku regarding this microdata:
Function Definitions
Define a function to download, unzip, and import each sas file:
library(haven)
meps_sas_import <-
function( this_url ){
this_tf <- tempfile()
download.file( this_url , this_tf , mode = 'wb' )
this_tbl <- read_sas( this_tf )
this_df <- data.frame( this_tbl )
names( this_df ) <- tolower( names( this_df ) )
this_df
}
Download, Import, Preparation
Download and import the consolidated file and the replicate weights file:
meps_cons_df <-
meps_sas_import( "https://meps.ahrq.gov/data_files/pufs/h224/h224v9.zip" )
meps_brr_df <-
meps_sas_import( "https://meps.ahrq.gov/mepsweb/data_files/pufs/h036brr/h36brr20v9.zip" )
Merge the consolidated file with the replicate weights:
meps_df <- merge( meps_cons_df , meps_brr_df )
stopifnot( nrow( meps_df ) == nrow( meps_cons_df ) )
meps_df[ , 'one' ] <- 1
Save Locally
Save the object at any point:
# meps_fn <- file.path( path.expand( "~" ) , "MEPS" , "this_file.rds" )
# saveRDS( meps_df , file = meps_fn , compress = FALSE )
Load the same object:
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:
Calculate the distribution of a categorical variable, overall and by groups:
Calculate the sum of a linear variable, overall and by groups:
Calculate the weighted sum of a categorical variable, overall and by groups:
Calculate the median (50th percentile) of a linear variable, overall and by groups:
svyquantile( ~ totexp20 , meps_design , 0.5 )
svyby(
~ totexp20 ,
~ region20 ,
meps_design ,
svyquantile ,
0.5 ,
ci = TRUE
)
Estimate a ratio:
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( ~ totexp20 , meps_design )
coef( this_result )
SE( this_result )
confint( this_result )
cv( this_result )
grouped_result <-
svyby(
~ totexp20 ,
~ region20 ,
meps_design ,
svymean
)
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( ~ totexp20 , meps_design , deff = TRUE )
# SRS with replacement
svymean( ~ totexp20 , meps_design , 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 the statistic and standard error shown under Analysis of the Total Population:
library(foreign)
xport_2002_tf <- tempfile()
xport_2002_url <- "https://meps.ahrq.gov/data_files/pufs/h70ssp.zip"
download.file( xport_2002_url , xport_2002_tf , mode = 'wb' )
unzipped_2002_xport <- unzip( xport_2002_tf , exdir = tempdir() )
meps_2002_df <- read.xport( unzipped_2002_xport )
names( meps_2002_df ) <- tolower( names( meps_2002_df ) )
meps_2002_design <-
svydesign(
~ varpsu ,
strata = ~ varstr ,
weights = ~ perwt02f ,
data = meps_2002_df ,
nest = TRUE
)
result <- svymean( ~ totexp02 , meps_2002_design )
stopifnot( round( coef( result ) , 2 ) == 2813.24 )
stopifnot( round( SE( result ) , 2 ) == 58.99 )
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 MEPS users, this code replicates previously-presented examples:
Calculate the mean (average) of a linear variable, overall and by groups: