Skip to content

Instantly share code, notes, and snippets.

@ylelkes
Created March 17, 2021 14:05
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 ylelkes/d3c116584dd9457e66705af3da2a994f to your computer and use it in GitHub Desktop.
Save ylelkes/d3c116584dd9457e66705af3da2a994f to your computer and use it in GitHub Desktop.
library(dplyr)
library(ggthemes)
library(ggplot2)
anes <- rio::import("~/Dropbox/anes_timeseries_cdf_dta/anes_timeseries_cdf_stata13.dta")
anes$year <- anes$VCF0004
anes$demrep <- as.factor(car::recode(as.numeric(anes$VCF0301),"1:3='Democrat';5:7='Republican';else=NA"))
anes$VCF0218[anes$VCF0218>97]=NA
anes$VCF0224[anes$VCF0224>97]=NA
anes$outaffect <- NA
anes$outaffect[which(anes$demrep=="Republican")] <- anes$VCF0218[which(anes$demrep=="Republican")]
anes$outaffect[which(anes$demrep=="Democrat")] <- anes$VCF0224[which(anes$demrep=="Democrat")]
anes$inaffect <- NA
anes$inaffect[which(anes$demrep=="Democrat")] <- anes$VCF0218[which(anes$demrep=="Democrat")]
anes$inaffect[which(anes$demrep=="Republican")] <- anes$VCF0224[which(anes$demrep=="Republican")]
anes$inaffect[anes$inaffect>97]=NA
anes$outaffect[anes$outaffect>97]=NA
anes$affpol <- (anes$inaffect-anes$outaffect)
anes$mode <- as.factor(car::recode(anes$VCF0017,"0='FTF';1:3='Telephone';4='Internet'"))
anes %>% group_by(year,mode) %>% summarise(affpol=weighted.mean(affpol,VCF0011z,na.rm=T)) -> out
out <- na.omit(out)
ggplot(out,aes(x=year,y=affpol,lty=mode,color=mode)) + geom_line() + theme_economist() + geom_point() + scale_colour_economist()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment