Skip to content

Instantly share code, notes, and snippets.

@arm5077
Last active April 24, 2017 17:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arm5077/24e271cdca96762fde87e5fa424399af to your computer and use it in GitHub Desktop.
Save arm5077/24e271cdca96762fde87e5fa424399af to your computer and use it in GitHub Desktop.
Pulls the top five percent of U.S. medical spenders and calculates proportions of race.
library(Hmisc)
library(survey)
# 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