Skip to content

Instantly share code, notes, and snippets.



Last active Apr 24, 2017
What would you like to do?
Pulls the top five percent of U.S. medical spenders and calculates proportions of race.
# Import data from SAS transport file
data <- sasxport.get('data/h171.ssp')
# Build survey object using person weights
full_population_survey <- svydesign(ids=~1, data=data, weights=~perwt14f)
# Find cutoff for top 5 percent of spenders
top_five_percentile_cutoff <- svyquantile(~totexp14, full_population_survey, c(.95 ))[1]
# Get subset of survey that's just the top 5 percent of spenders
fivepercent <- subset(full_population_survey, totexp14 >= top_five_percentile_cutoff)
# Build a table tracking the proportion each race comprises of the top 5 percent
race_fivepercent <- prop.table(svytable(~racethx, design = fivepercent))
# Build a table tracking the proportion each race comprises of the total survey
race_full_population <- prop.table(svytable(~racethx, design = full_population_survey))
# Get percent change between five percent population and total population
(race_fivepercent - race_full_population) / race_full_population
# Returns this:
# 1 2 3 4 5
# -0.48050311 0.20804408 -0.08487131 -0.46280463 -0.19622867
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment