``````library(plyr)
# 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")``````