Skip to content

Instantly share code, notes, and snippets.

@jlsutherland
Last active December 6, 2018 01:23
Show Gist options
  • Select an option

  • Save jlsutherland/de29df172edcebb8bd69978990ea4c23 to your computer and use it in GitHub Desktop.

Select an option

Save jlsutherland/de29df172edcebb8bd69978990ea4c23 to your computer and use it in GitHub Desktop.
Replicate Levendusky (2009) Fig 3.7, add ANES 2016
library(tidyverse)
library(survey)
library(scales)
anes16 <- readstata13::read.dta13("~/Downloads/anes_timeseries_2016.dta")
anescdf <- readstata13::read.dta13("~/Downloads/anes_timeseries_cdf.dta") %>%
filter(VCF0004 %in% c(1984, 2004))
preference_vars <- c(
"VCF0809", # guaranteed jobs scale
"VCF0839", # gov spend-serv scale
"VCF0843", # def spend scale
"VCF0806", # gov health ins scale
"VCF0830", # aid to mins scale
"VCF0838" # abortion scale
)
preference_vars_16 <- c(
"V161189", # guaranteed jobs scale
"V161178", # gov spend-serv scale
"V161181", # def spend scale
"V161184", # gov health ins scale
"V161198", # aid to mins scale
"V161232" # abortion scale
)
# Reverse-coded vars... please double check me on this.
# Abortion scale is reverse coded.
anescdf$VCF0838 <- as.numeric(gsub("(\\d).*", "\\1", anescdf$VCF0838))
anescdf$VCF0838 <- 5 - anescdf$VCF0838
anescdf$VCF0838[anescdf$VCF0838 %in% c(-4, 5)] <- -9
anes16$V161232 <- 5 - anes16$V161232
anes16$V161232[anes16$V161232 %in% c(0, 13, 14)] <- -9
# Spend-serv scale is reverse coded.
anescdf$VCF0839 <- 8 - anescdf$VCF0839
anescdf$VCF0839[anescdf$VCF0839 %in% c(-1, 8)] <- -9
anes16$V161178 <- 8 - anes16$V161178
anes16$V161178[anes16$V161178 %in% c(-91, 16,17)] <- -9
# Clean out NADKs (rough).
nadk_codes <- c(-9, -8, 99, 9, 10, 0)
for (i in preference_vars_16) {
anes16[,i][anes16[,i] %in% nadk_codes] <- NA
anes16[,i] <- rescale(anes16[,i], to=c(1,7), na.rm=T)
}
for (i in preference_vars) {
anescdf[,i] <- anescdf[,i] + 1
anescdf[,i][anescdf[,i] %in% nadk_codes] <- NA
anescdf[,i] <- rescale(anescdf[,i], to=c(1,7), na.rm=T)
}
# Check our recoded data.
summary(anes16[,preference_vars_16])
summary(anescdf[,preference_vars])
# Compute the mean issue positions.
anes16$isspos <- rowMeans(anes16[,preference_vars_16], na.rm = T)
anescdf$isspos <- rowMeans(anescdf[,preference_vars], na.rm = T)
# Init the survey design.
design16 <- svydesign(
id = ~V160202,
strata = ~V160201,
data = anes16,
weights = ~V160102,
nest = TRUE
)
# Produce density estimates.
dens16 <- svysmooth(~isspos, design16, bandwidth = .3, xlim=c(0.5, 7.5))
dens04 <- density(anescdf$isspos[anescdf$VCF0004 == 2004], na.rm = T, bw = .3)
dens84 <- density(anescdf$isspos[anescdf$VCF0004 == 1984], na.rm = T, bw = .3)
dens16 <- as.data.frame(dens16$isspos)
dens04 <- as.data.frame(cbind(x=dens04$x, y=dens04$y))
dens84 <- as.data.frame(cbind(x=dens94$x, y=dens94$y))
dens16$year <- 2016
dens04$year <- 2004
dens84$year <- 1984
pdat <- rbind(dens16, dens04, dens84)
pdat$year <- as.factor(pdat$year)
# Make plot.
p <- pdat %>%
ggplot(aes(x=x, y=y, lty=year)) +
geom_line() +
theme_classic() +
scale_linetype_discrete(name="ANES Year") +
scale_x_continuous(breaks=c(1, 4, 7), labels=c("Liberal", "Moderate", "Conservative"), name="\nAverage Issue Position") +
theme(
axis.line.y = element_blank(),
axis.text.y = element_blank(),
axis.title.y = element_blank(),
axis.ticks.y = element_blank(),
axis.text = element_text(color="black", size=11)
)
ggsave(filename="~/Downloads/polplot_gronke.png", height = 7, width = 6)
# Compare specific issue positions.
pref16 <- formula(paste0("~", paste(preference_vars_16, collapse="+")))
means_16 <- as.data.frame(svymean(pref16, design16, na.rm=T))
means_cdf <- t(rbind(
anescdf[,preference_vars] %>%
summarise_all(funs(
mean(., na.rm=T)
)),
anescdf[,preference_vars] %>%
summarise_all(funs(
sd(., na.rm=T)
)) / sqrt(colSums(!is.na(anescdf[,preference_vars])))
))
diff_means <- cbind(
means_16[,1] - means_cdf[,1],
sqrt(means_16[,2]^2 + means_cdf[,2]^2)
)
diff_means
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment