Tools for Acquiring and Analyzing Political Data

Provides useful functions for obtaining commonly-used data in political analysis and political science, including from sources such as the Comparative Agendas Project < https://www.comparativeagendas.net>, which provides data on politics and policy from 20+ countries, the MIT Election and Data Science Lab < https://www.electionlab.mit.edu>, and FiveThirtyEight < https://www.FiveThirtyEight.com>.


BuildStatus MonthlyDownloads WeeklyDownloads TotalDownloads v0.1.2

A R package for acquiring and analyzing political data — including polls, election results, legislator information, and demographic data.

Author: G. Elliott Morris

For more, see the package’s project description my blog or view the vignettes (vignettes area work in progress). You can find many examples of how these data are used in the real world via my interactive R course at DataCamp.com, “Analyzing Election and Polling Data in R”.

I’ve written a detailed guide on using R for analyzing political data, A Guide to Analyzing (American) Political Data in R, that uses many of the functions and datasets from this package. The example use cases extend beyond those in the function documentation and this readme.

Installation

Install the politicaldata package from CRAN:

# install politicaldata from CRAN
install.packages('politicaldata')
 
# load the politicaldata package
library(politicaldata)

As the landscape of online data and API calls is constantly changing, the development version is likely to contain quicker bug fixes and integration of new data. You can instill the dev version using the remotes::install_github() workflow outlined here:

To get the current development version from GitHub:

# install the remotes package if it's not already
if (!requireNamespace("remotes", quietly = TRUE)) {
  install.packages("remotes")
}
 
# install dev version of politicaldata from github
remotes::install_github("elliottmorris/politicaldata")
 
# load the politicaldata package
library(politicaldata)

Usage

This package provides a variety of functions for quickly accessing different data sources used in political science and analytics. For example, you can download a data.frame of the DW-NOMINATE scores of congressional ideology computed by the VoteView project at UCLA:

# import the package
library(politicaldata)
 
# download the NOMINATE scores for the 116th House
house_ideo <- get_house_nominate(congress = 116)
 
# download the NOMINATE scores for the Senate in the 116th Congress
senate_ideo <- get_senate_nominate(congress = 116)
 
# take a look with dplyr::head()
suppressMessages(library(dplyr))
 
head(house_ideo[1:5])
#>   congress chamber icpsr state_icpsr district_code
#> 1      116   House 20301          41             3
#> 2      116   House 21102          41             7
#> 3      116   House 21192          41             2
#> 4      116   House 21193          41             5
#> 5      116   House 21376          41             1
#> 6      116   House 21500          41             6

A list of functions:

  • get_house_nominate() returns DW-NOMINATE ideology scores for each member of the U.S. House of Representatives for a specified congress, else every Representative ever.
  • get_senate_nominate() returns DW-NOMINATE ideology scores for each member of the U.S. Senate for a specified congress, else every Senator ever.
  • trump_approval_polls_538() returns a dataset of approval polls aggregated by the folks over at FiveThirtyEight.
  • get_cap_mip() returns a historical dataset of the aggregated responses to Gallup’s Most Important Problem questions, coded by major topic. Part of a suite of functions for obtaining data from the Comparative Agendas Project.

A list of datasets:

  • house_116 is a saved copy of the output from get_house_nominate(congress=116) run on the last day the package was updated (and thus should only be used for demos, unless you want outdated data).
  • senate_116 is the same as the above, bur for the Senate. Downloaded via get_senate_nominate(congress=116).
  • us_polls_history is a dataset of US presidential election polling from the 1980 through 2016 elections.
  • house_results is a dataset of results for elections to the US House of Representatives that occurred from 1976 to 2018
  • pres_results_by_cd is a dataset of results for presidential elections broken down by congressional district from 1990 to 2016

Vignettes

Come back later. For now, some examples can be found here.

Suggested related packages:

  • Rvoteview provides functions for obtaining roll call voting data, which can thus be analyzed using algorithms from the pscl package.
  • ropercenter allows you import data from the Roper Center’s iPoll directly in R, given that you know the slug of the interested poll.
  • fivethirtyeight was developed to distribute the data behind the popular data journalism website, and thus will have some overlap. FiveThirtyEight also releases most of their data on GitHub.
  • pollstR provides a way to access the aggregate toplines from Huffington Post Pollster, which is sadly no longer being updated.

Contributions

You should feel free to suggest more data and/or functions to add, open issues, submit pull requests, etc.

Contact

You can reach me by opening an issue, on Twitter, or via email (but I’d prefer you to communicate primarily via GitHub).

License

This package is open source and released under the MIT License, which only stipulates that you must distribute the License alongside the package. For more details, click on “See License” at the top right of the repository.

News

0.1.0

The initial version of politicaldata has been submitted to CRAN with functions to extract data from web pages directly into R and several datasets for analyzing Congressional legislator ideologies, election polling, and election results. You can use the beginnings of an API for data from the Comparative Agendas Project, which will be a large focus of this package's codebase moving forward.

0.1.1

politicaldata underwent changes for resubmission to CRAN.

0.2.1

The dependency structure of politicaldata has been substantially reduced and bug fixes were made to the data generating scripts for the pres_results data frame that were causing vote shares to be divided by about 1e06.

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("politicaldata")

0.1.3 by G. Elliott Morris, 5 months ago


Report a bug at https://github.com/elliottmorris/politicaldata/issues


Browse source code at https://github.com/cran/politicaldata


Authors: G. Elliott Morris [aut, cre] , Comparative Agendas Project [cph] , MIT Election and Data Science Lab [cph] , FiveThirtyEight.com [cph]


Documentation:   PDF Manual  


MIT + file LICENSE license


Imports utils

Suggests testthat, dplyr, magrittr, tidyr, ggplot2


See at CRAN