Skip to content

Instantly share code, notes, and snippets.

@rwalk
Last active December 27, 2016 18:32
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 rwalk/f2d6d995b5133d92ca69495c218cb44b to your computer and use it in GitHub Desktop.
Save rwalk/f2d6d995b5133d92ca69495c218cb44b to your computer and use it in GitHub Desktop.
CHIS 2015: Insurance coverage type vs fast food consumption
library(foreign)
library(car)
library(ggplot2)
library(scales)
# load the CHIS data
file <- "~/projects/CHIS/chis15_adult_stata/Data/ADULT.dta" # your file
CHIS <- read.dta(file, convert.factors = TRUE)
# Recode insurance type
recodeSTR = "
c('MEDICARE & OTHERS', 'MEDICARE ONLY', 'MEDICARE & MEDICAID') = 'MEDICARE';
"
CHIS$coverage_type = recode(CHIS$instyp, recodeSTR)
# break number times fast food consumed last week into categorical buckets
CHIS$fast_food = cut(CHIS$ac31, right=FALSE,
breaks=c(0,1,2,5,1000),
labels=c("0","1","2-4","5+"))
# compute summary df
df <- as.data.frame(prop.table(xtabs(rakedw0 ~ coverage_type + fast_food, CHIS, drop.unused.levels=TRUE),1))
# plot result
cov_types = c("MEDICARE", "MEDICAID", "EMPLOYMENT-BASED", "UNINSURED", "PRIVATELY PURCHASED")
ggplot(df[df$coverage_type %in% cov_types,], aes(x=coverage_type, y=Freq, fill=fast_food)) + geom_bar(stat="identity") +
scale_fill_manual(values=c("gray","pink","red","darkred"), name="Fast food\nlast week\n(# times)") +
xlab("coverage type") + ylab("proportion") +
ggtitle("Fast Food vs Insurance Coverage Type")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment