Skip to content

Instantly share code, notes, and snippets.

@jpearl1395
Last active March 1, 2016 06:03
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jpearl1395/1347a6ee4518ec7bf3a1 to your computer and use it in GitHub Desktop.
library(readr)
library(dplyr)
library(ggplot2)
library(RColorBrewer)
library(maptools)
library(ggmap)
library(gtools)
setwd('/Users/joshpearl/Documents/Dartmouth/Junior Year 16W-16X/16W/HIST 90.01- US Hist Through Census/Labor Assignment')
a <- read_csv('usa_00010.csv')
# Filter for native born American men and select neccessary variables
b <- filter(a, SEX==1)
c <- filter(b, BPL>=1 & BPL<=56)
d <- select(c, YEAR, CPI99, PERWT, VETVIETN, SEX, AGE, BIRTHYR, BPL, EDUC, EDUCD, EMPSTAT,
INCWAGE)
# Label Vietnam Veterans and select 10% and 90% quartiles to get rid of outliers
e <- mutate(d, VietVet=factor(ifelse(VETVIETN==2, 1, 0), labels=c('non-Veteran', 'Veteran')))
ff <- filter(e, YEAR==1980 & VietVet=='Veteran')
fa <- rep(quantile(ff$AGE, c(.1, .9), times=PERWT))
fb <- filter(e, BIRTHYR>=1980-fa[2] & BIRTHYR<=1980-fa[1])
# Breakdown and create income categories
g <- mutate(fb, income=ifelse(INCWAGE==999999, 0, INCWAGE*CPI99))
i <- mutate(g, incomecat=ifelse(income==0, 1,
ifelse(income<20000, 2,
ifelse(income<40000, 3,
ifelse(income<60000, 4,
ifelse(income<80000, 5,
ifelse(income<100000, 6,
ifelse(income<120000, 7,
ifelse(income<999999, 8)))))))))
j <- mutate(i, incomecat=factor(incomecat, labels=c('No Income', '$1 - $19,999',
'$20,000 - $39,000', '$40,000 - $59,000',
'$60,000 - $79,000', '$80,000 - $99,999',
'$100,000 - $119,000', '$120,000+')))
# Breakdown and create birthyear cohorts for those that are working
k <- mutate(filter(j, EMPSTAT!=3), cohort=ifelse(BIRTHYR<1940, 1,
ifelse(BIRTHYR<1943, 2,
ifelse(BIRTHYR<1946, 3,
ifelse(BIRTHYR<1949, 4,
ifelse(BIRTHYR<1952, 5,
ifelse(BIRTHYR<1955, 6)))))))
m <- mutate(k, cohort=factor(cohort, labels=c('1937 - 1939', '1940 - 1942',
'1943 - 1945', '1946 - 1948',
'1949 - 1951', '1952 - 1954')))
# Find Median income of each birthyear cohort (Vet and Non-Vet)
n <- summarise(group_by(m, YEAR, cohort, VietVet), medianincome=median(rep(income,times=PERWT)))
o <- ggplot(n, aes(x=YEAR, y=medianincome, color=cohort)) + geom_line() +
geom_point() + scale_y_continuous(labels=scales::comma) +facet_grid(VietVet~.) +
labs(title='Income by Birth year Cohorts', x='Year', y='Median Income', color='Birth year Cohort')
print(o)
# Create education cohorts
p <- mutate(m, educlevel=ifelse(EDUC<6, 0,
ifelse(EDUC==6, 1,
ifelse(EDUC<10, 2,
ifelse(EDUC==10, 3,
ifelse(EDUC==11, 4, 5))))))
q <- mutate(p, educlevel=factor(educlevel, labels=c("Did not Graduate High school",
"High school (Grade 12)",
"Some College (Less than 4 Years)",
"Bachelor's Degree (4 Years)",
"Master's, Doctoral or Professional Degree (5+ Years)")))
# Graph education cohorts with median income, Veteran vs. Non-Veteran
r <- summarise(group_by(q, YEAR, educlevel, VietVet), medianincome=median(rep(income, times=PERWT)))
s <- ggplot(r, aes(x=YEAR, y=medianincome, color=educlevel)) + geom_line() + geom_point() +
scale_y_continuous(labels=scales::comma) + facet_grid(VietVet~.) +
labs(title='Income by Education Attainment', x='Year', y='Median Income', color='Education Level')
print(s)
# Education per cohort, Veteran vs. Non-Veteran
t<- filter(m, AGE>39) %>% mutate(educlevel=ifelse(EDUC<6, 0,
ifelse(EDUC==6, 1,
ifelse(EDUC<10, 2,
ifelse(EDUC==10, 3,
ifelse(EDUC==11, 4, 5))))))
t2<- mutate(t, educlevel=factor(educlevel, labels=c("Did not Graduate High school",
"High school (Grade 12)",
"Some College (Less than 4 Years)",
"Bachelor's Degree (4 Years)",
"Master's, Doctoral or Professional Degree (5+ Years)")))
t3 <- summarise(group_by(t2, educlevel, cohort, VietVet), Total=sum(PERWT))
u <- ggplot(t3, aes(x=cohort, y=Total)) + geom_bar(aes(fill=educlevel), stat='identity', position='dodge') +
facet_grid(VietVet~.) + labs(title='Education Attainment by Birth year Cohorts', x='Birth year Cohort', y='Total', fill='Education Level') +
scale_fill_brewer(palette='Set2') + scale_y_continuous(labels=scales::comma)
print(u)
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment