Skip to content

Instantly share code, notes, and snippets.

View jamesdunham's full-sized avatar

James Dunham jamesdunham

  • Center for Security and Emerging Technology
  • New York
View GitHub Profile
@jamesdunham
jamesdunham / scrape_congressional_session-years.r
Created November 14, 2016 15:17
scrape Congressional session-years from history.house.gov
library(rvest)
library(data.table)
library(lubridate)
doc = read_html("http://history.house.gov/Institution/Session-Dates/All/")
sessions = doc %>%
html_nodes(xpath = "//div[contains(@class, 'manual-table')]/table") %>%
html_table() %>%
as.data.frame()
plot_rhats <- function(dgirt_out) {
# Save to disk plots of split R-hats for theta-bars (from a particular model)
#
# This function produces two plots showing the average R-hat in each year by
# 1) state and 2) party, each averaging over the other. It assumes that
# particular variable names exist in dgirt_out as the grouping variable (D_pid3),
# time variable (D_year), and geographic variable (D_abb).
# From the stanfit object extract R-hats and effective sample sizes
tb_sum <- summary(dgirt_out, par = "theta_bar", verbose = TRUE)$summary
@jamesdunham
jamesdunham / smoother.R
Last active August 12, 2016 22:15
Smooth interpolation of missing data with loess
smooth_years <- function(d, varnames = NULL) {
# Smooth variables over years using a loess interpolation
#
# param d: a dataframe
# param varnames: the names of the variables to smooth over;
# if NULL names will be guessed.
#
# Returns a data.frame 2.5x larger than the input,
# containing the original values and predicted values from a
# loess fit. Assumes that a variable called 'year' exists.