13.11 Lab: R Code
13.11.1 Run the following functions
13.11.2 Load data & install packages
# set.seed(48104) # ?set.seed
# Load data
data_rdd <- foreign::read.dta("https://docs.google.com/uc?id=1xWHmST5FYcfLxe9V7Hwqd2LIy_A3ninG&export=download")
# data_rdd <- foreign::read.dta("www/rdd-fouirnaies_hall_financial_incumbency_advantage.dta")
data_rdd <- data_rdd %>% rename(x_score_victorymargin = rv,
y_donationshare = dv_money,
cov_statelevel = statelevel,
cov_total_race_money = total_race_money,
cov_total_votes = total_votes,
cov_dem_inc = dem_inc,
cov_rep_inc = rep_inc,
cov_total_group_money = total_group_money) %>%
dplyr::select(x_score_victorymargin,
y_donationshare,
cov_statelevel,
cov_total_race_money,
cov_total_votes,
cov_dem_inc,
cov_rep_inc,
cov_total_group_money,
state,
dist,
year)
13.11.3 Explore data and subsetting
# names(data_rdd) # Show variables
# View(data_rdd)
# Filter out state legislative district level
table(data_rdd$cov_statelevel) # How many state-level elections are there?
0 | 1 |
---|---|
6533 | 32670 |
13.11.4 Summary statistics + graphs
Below we inspect the variables.
Q: What the min. and max. values of the variable x_score_victorymargin
mean substantively? And the same for y_donationshare
?
# Summarize score variable (also called running variable): Vote share
summary(data_rdd$x_score_victorymargin)
Min. | 1st Qu. | Median | Mean | 3rd Qu. | Max. |
---|---|---|---|---|---|
-50 | -16.05517 | 0.134985 | 1.357636 | 20.07864 | 50 |
Min. | 1st Qu. | Median | Mean | 3rd Qu. | Max. | NA’s |
---|---|---|---|---|---|---|
0 | 6.540446 | 50.60789 | 50.78094 | 96.80258 | 100 | 5467 |
# Share of donations flowing to the incumbent’s party
# Plot oucome var vs. score variable
p = qplot(x_score_victorymargin, y_donationshare, data=data_rdd) +
xlab("Democratic margin of victory at t") +
ylab("Democratic share of contributions at t+1")
p
We can do the same with plotly..
data_rdd$colour[data_rdd$x_score_victorymargin<=0] <- "Treated"
data_rdd$colour[data_rdd$x_score_victorymargin>0] <- "Control"
plot_ly(data = data_rdd,
type = "scatter",
mode = "markers",
x = data_rdd$x_score_victorymargin,
y = data_rdd$y_donationshare,
color = data_rdd$colour,
marker = list(size=3)) %>%
layout(xaxis = list(title = "Democratic margin of victory at t", dtick = 25),
yaxis = list(title = "Democratic share of contributions at t+1"))