library(plyr)
load("lee.RData")
# Subsetting to Democrats
d <- d[which(d$party == "100"), ]
# Create vote share variable (DV)
d$share_t <- d$origvote/d$totvote
# Create adjusted vote margin variable
d$margin_t <- d$origvote/(d$highestvote + d$sechighestvote) - 0.5
# Create forcing variable (adjusted vote margin from previous
# year)
d <- ddply(d, c("state", "distnum", "distid", "party"), function(d.sub) {
out <- d.sub[order(d.sub$yearel), ]
out$margin_tm1 <- c(NA, out$margin_t[-nrow(out)])
return(out)
})
d$incumbent <- ifelse(d$margin_tm1 > 0, 1, 0)
# Plot treatment against forcing
plot(x = d$margin_tm1, y = d$incumbent, xlab = "Vote Margin (t-1)",
ylab = "Incumbency", main = "Vote Margin and Incumbency")
abline(v = 0, lty = 3)

mod <- lm(share_t ~ incumbent + margin_tm1, data = d)
# Create scatterplot
plot(d$margin_tm1, d$share_t, xlab = "Vote Margin (t-1)", ylab = "Vote Share",
     main = "Party Incumbency Advantage \n Linear w/ Common Slope", cex.main = 0.8)
# Plot curves
curve(mod$coefficients[1] + mod$coefficients[2] + mod$coefficients[3] * x, 0, 0.5,
      add = T, lwd = 3, col = "red")
curve(mod$coefficients[1] + mod$coefficients[3] * x, -0.5, 0,
      add = T, lwd = 3, col = "blue")

# Generate vector of bandwidths
bws <- seq(0.01, 0.30, 0.01)
# Function to trim the data and re-estimate
trimfun <- function(bw){
  # Trim dataset
  dtrim <- d[which(abs(d$margin_tm1) < bw), ]
  # Run model
  modtrim <- lm(share_t ~ incumbent + margin_tm1, data = dtrim)
  # Get coef and SE
  beta <- summary(modtrim)$coefficients[2]
  beta_se <- summary(modtrim)$coefficients[5]
  # Calculate CI
  beta_upper <- beta + 1.96 * beta_se
  beta_lower <- beta - 1.96 * beta_se
  res <- c(beta, beta_upper, beta_lower)
  return(res)
}
# Plot
bw_res <- sapply(bws, trimfun)
plot(x = bws, y = bw_res[1, ], ylim = c(-0.1, 0.15), ylab = expression(hat(beta)),
     xlab = "Bandwidth", pch = 16, main = "LATE Estimate by Bandwidth Size", cex.main = 0.8)
segments(x0 = bws, x1 = bws, y0 = bw_res[2, ], y1 = bw_res[3, ])
abline(h = 0, col = "red")


  1. Graduate School of Economics, Waseda University,