Skip to content

Instantly share code, notes, and snippets.

View mattblackwell's full-sized avatar

Matthew Blackwell mattblackwell

View GitHub Profile
@mattblackwell
mattblackwell / beamer-overlay.Rmd
Last active April 14, 2024 20:53
How to create quick and dirty beamer animations with rmarkdown and knitr
---
title: 'Beamer Overlay Test'
output:
beamer_presentation:
incremental: true
theme: CambridgeUS
---
```{r, echo = FALSE}
@mattblackwell
mattblackwell / clinton-rust-belt.R
Last active January 1, 2017 09:39
Analysis of the shift from 2012 to 2016
group.mean <- function(x, group) {
out <- tapply(x, group, mean, na.rm = TRUE)
out[group]
}
data(state.fips, package = "maps")
state.fips <- unique(state.fips[,c("fips","abb")])
state.fips$abb <- as.character(state.fips$abb)
state.fips <- rbind(state.fips, c(2, "AK"))
state.fips <- rbind(state.fips, c(15, "HI"))
fips county_name.x trump.share blkprop2010 blkprop40 w.med.income2014
45001 Abbeville 0.369 0.282763504740921 0.424796127512974 39287
45003 Aiken 0.316 0.245810404812023 0.41912412853594 52247
45007 Anderson 0.3195 0.160426664386563 0.26353819100009 45788
45009 Bamberg 0.401 0.615312441358604 0.606340181301293 44127
45011 Barnwell 0.427 0.442730206445338 0.626576621312941 44781
45013 Beaufort 0.301 0.192870747628411 0.670735581068204 65475
45015 Berkeley 0.3398 0.250299421399774 0.64711736950752 58645
45019 Charleston 0.264 0.297647975922949 0.491911977209859 64441
45021 Cherokee 0.42 0.203787358606483 0.234154400720937 38562
@mattblackwell
mattblackwell / rmdrender
Created July 14, 2015 21:21
Shell script to render Rmd files
#! /bin/sh
if [ -r "$1" ]; then
mdfile="$1"
Rscript -e "rmarkdown::render('${mdfile}', 'all')"
else
exit 0
fi
@mattblackwell
mattblackwell / gist:7587294
Created November 21, 2013 18:47
Using the subset argument to overimpute() in Amelia
library(Amelia)
data(africa)
a.out <- amelia(africa, ts = "year", cs = "country")
overimpute(a.out, "gdp_pc")
overimpute(a.out, "gdp_pc", subset = country == "Burkina Faso")
overimpute(a.out, "gdp_pc", subset = gdp_pc >=500)
@mattblackwell
mattblackwell / gist:6985383
Created October 15, 2013 01:51
MCMC sampler for polling aggregation with covariance smoothing
## Function to implment the MCMC sampler
polling.gibbs.kal.fixedS <- function(y, size, dates, states, big, ## data
F, X=NULL, XL = NULL, Z=NULL, ## covariates
m.0, C.0, b = NULL, B = NULL, ## hyper-parameters
starts, phi, sigma.a, nu, beta, k, ## starting values
tune, A,
iters = 10000, thin = 1, burnin = 0) { ## mcmc stuff
aa <- proc.time()
## pre-gibbs cleaning (calculate variances, setup times, etc)
@mattblackwell
mattblackwell / attenuation.R
Created May 30, 2012 18:01
Dynamic plots for demonstrating attenuation bias
### Create plots for MO talk
library(tikzDevice)
x <- rnorm(200,0,.4)
y <- x + rnorm(200, 0, .4)
night <- rgb(26,31,30, max=255)
beach <- rgb(227,223,186, max=255)
@mattblackwell
mattblackwell / gist:1203787
Created September 8, 2011 16:14
Covariates in the Covariance Gibbs
## Function to implment the MCMC sampler
polling.gibbs.kal <- function(y, size, dates, states, big, ## data
F, X=NULL, XL = NULL, Z=NULL, ## covariates
m.0, C.0, b = NULL, B = NULL, ## hyper-parameters
c.0, d.0, e.0, f.0, g.0, h.0,
starts, phi, sigma.a, nu, beta, ## starting values
tune, A, tune.phi,
iters = 10000, thin = 1, burnin = 0) { ## mcmc stuff
aa <- proc.time()