Gabri, Pablo, Joey December 14, 2018
The fecScrape package provides functions to interface with the OpenFEC API. OpenFEC API allows you to access funding data on candidates and committees. This package allows users to scrape individual- and aggregated-level donation data, plot these data to examine the timecourse of donation as well as geographic spread of donations, and does some basic summary statistics.
During this class project, we came across another package which inspired our own code and thoughts on how to develop this package. Please check out the tidyusafec package, written by Stephen Holzman.
Installation is done locally for now. Make sure that the project is specified to fecScrape_project and that both libraries are installed.
library(devtools)
library(roxygen2)
setwd("./fecScrape") # pathway for where the package folder is located
document() # knits? the package together for use
library(fecScrape)
Function Name | Description |
---|---|
query_candidate_list | This function returns the list of all candidates that run in the 2018 Senate Election for a given state. |
query_choose_cand | This function allows the user to manually pick 2 opposing candidates (1 Republican and 1 Democrat) from the list retrieved using query_candidate_list for comparison. |
query_contributions_all | This function retrieves all individual level contributions for every candidate in the list provided. |
query_itemized_contributions | This function opreates within query_contributions_all and it retrieves individual contributions when provided with eahc candidate's principal committees id |
query_openfec | This function srapes the FEC websites using FEC aPIs. It operates within the query_itemized_contributions and the query_candidate_all higher level functions |
plot_avg_donation | This functions elaborates the individual contributions data from the two main candidates selected and plots the average daily donations. |
plot_cum_donation | This functions elaborates the individual contributions data from the two main candidates selected and plots the cumulative daily donations. |
plot_top_cities | This function plot a graph bar showing individual donations' origin for the top n cities for each of the opposing candiates. |
plot_occupations | This function plot a percentage stacked bar plot showing the shares of the top n individual contributors' occupations. All remaning occupations are automatically grouped in the "others" category (up to 8 different top occupations can be selected.). |
For our example, we will focus on the recent 2018 Sentate race between Sheldon Whitehouse and Robert Flanders of Rhode Island. We wanted to choose Texas, since Beto and Cruz raised tens of millions of dollars, but the scraping takes a very long time!
# Find and select candidates
my_api <- "_______________________" # change to your api_key please!
ri_data <- query_candidate_list(
api_key = my_api,
state = "RI",
election_year = 2018,
office = "S"
)
ri_data$name
# Select candidates of interest
ri_chosen_data <- choose_cand(ri_data, 3, 5) #numbers are optional, script will prompt for them, 3 specifies Flanders, #5 specifies Whitehouse
head(ri_chosen_data)
# Find all individual donations to each candidates' primary committee
ri_indiv_data <- query_contributions_all(
input_candlist = ri_chosen_data,
api_key = my_api
)
ri_avg_donation <- plot_avg_donation(ri_indiv_data)
ri_avg_donation
ri_cum_donation <- plot_cum_donation(ri_indiv_data)
ri_cum_donation
ri_cities_donation <- plot_top_cities(3, ri_indiv_data)
ri_cities_donation
ri_occup_donation <- plot_occupations(4, ri_indiv_data)
ri_occup_donation