In this assignment, we will explore the relationship between unemployment rates and election outcomes.
We will use two datasets:
The file cps_oct2016.csv is an extract of the Current Population Survey (CPS) that I downloaded fromz https://data.nber.org/cps/. The CPS is used by the Bureau of Labor Statistics to calculate the monthly unemployment rate.
| Name | Description |
|---|---|
state age mlr wgt |
State Age Monthly Labor Force Recode Survey weight |
The file federalelections2016.xlsx includes results from the 2016 US presidential election. It was downloaded from: https://transition.fec.gov/general/FederalElections2016.shtml. You will need to open this file in excel to figure what data you need and how to get it in to R.
Load the CPS data in R. Restrict the sample to people who are 16 or older. Generate an indicator variable for unemplyoment that equals 1 if someone is unemployed and 0 if someone is employed. This variable should be missing for anyone not in the labor force.
Finally, use the weighted.mean() function to calculate the unemployment rate separately for each state.
library(readr)
cps<-read.csv('cps_oct2016.csv', header=TRUE, stringsAsFactors = FALSE)
cps$age[as.character(cps$age)=="80-84 Years Old"]<-"84"
cps$age[cps$age=="85+ Years Old"]<-"85"
cps$age<-as.numeric(cps$age)
cps$unemp<-NA
cps<-subset(cps,(cps$age>=25))
cps$unemp[cps$mlr=="EMPLOYED-ABSENT"|cps$mlr=="EMPLOYED-AT WORK"]<-0
cps$unemp[cps$mlr=="UNEMPLOYED-LOOKING"|cps$mlr=="UNEMPLOYED=ON LAYOFF"]<-1
weighted.mean(cps$unemp,cps$wgt, na.rm=TRUE)
## [1] 0.03545134
cps1<-subset(cps,!is.na(cps$unemp)&!is.na(cps$wgt))
library(dplyr)
## Warning: package 'dplyr' was built under R version 3.5.3
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
st.unemp<-cps1%>%
group_by(state)%>%
summarize(mean=weighted.mean(cps1$unemp,cps1$wgt))
Examine the file federalelections2016.xlsx. Find the data that shows the number of votes Clinton and Trump received in each state. Bring this data in to R. Generate a new variable equal to Donald Trump’s share of the Clinton-Trump votes.
Merge this file with the CPS. The merge() function is the built in tool for doing this in R. However, you can also use the DPLYR or data.table packages to achieve this.
library(readr)
febs <- read_csv("febs.csv", col_names = FALSE,
col_types = cols(`Clinton (D)_1` = col_number(),
`Trump (R)_1` = col_number(), X2 = col_number(),
X3 = col_number(), X4 = col_number(),
X5 = col_number(), X6 = col_number(),
X7 = col_number(), X8 = col_skip()),
skip = 4)
## Warning: The following named parsers don't match the column names: Clinton
## (D)_1, Trump (R)_1
View(febs)
names(febs)<- c("state","EVT","EVC","PVT","PVC","PVO","Total")
febs[8]<-NULL
febs$trumpshare<-febs$PVT/febs$Total
merged<-merge(febs,cps1,by.x="state",by.y="state")
Regress Trump’s vote share on the unemployment rate. Show the regression using a ggplot figure. Interpret the results.
fit<-lm(merged$unemp~merged$trumpshare)
fit
##
## Call:
## lm(formula = merged$unemp ~ merged$trumpshare)
##
## Coefficients:
## (Intercept) merged$trumpshare
## 0.04204 -0.01882
plot(merged$trumpshare,merged$unemp,xlim=c(0,2.5),ylim=c(0,.05),
xlab = "Probability of Voting for Trump",
ylab = "Employement")
abline(fit)
fit<-lm(merged\(unemp~merged\)trumpshare)
fit
plot(merged\(trumpshare,merged\)unemp,xlim=c(0,2.5),ylim=c(0,.05), xlab = “Probability of Voting for Trump”, ylab = “Employement”) abline(fit)