Skip to content

Instantly share code, notes, and snippets.

View leeper's full-sized avatar

Thomas J. Leeper leeper

View GitHub Profile
@leeper
leeper / oversampling.R
Created October 25, 2016 10:58
Graphs showing SRS versus stratified/oversampling
# uses dev version of 'waffle'
# devtools::install_github("leeper/waffle@patch-1")
library("waffle")
library("extrafont")
# population
set.seed(1)
N <- 900L
p <- c("Small Group 1" = 30, "Big Group 1" = 420, "Small Group 2" = 30,"Big Group 2" = 420)
glyph <- c("male", "female")[sample(1:2, N, TRUE)]
@leeper
leeper / .gitignore
Last active October 11, 2016 15:40
Reanalysis of "Corruption as a Self-Fulfilling Prophecy" (AJPS, 2016)
Figure 4*
barplots*
codebook.pdf
@leeper
leeper / analysis.R
Last active October 3, 2016 09:37
Visualization of state votes for Democratic/Republican Presidents
library("utils")
library("ggplot2")
library("ggrepel")
dat <- read.csv("data.csv", stringsAsFactors = FALSE)
dat[["total"]] <- dat[["democrat"]] + dat[["republican"]]
dat[["abbreviation"]] <- sapply(dat[["state"]], function(x) state.abb[grep(x,tolower(state.name))[1]])
ggplot(dat, aes(x = democrat, y = republican, colour = total)) +
geom_point() + geom_abline(slope = 1, intercept = 0, colour = "gray") +
@leeper
leeper / curry.R
Last active September 29, 2016 13:40
play around with currying
`%<%` <- function(FUN, arg) {
f <- formals(FUN)
if (!length(f)) {
stop("No formal arguments in ", as.character(deparse(substitute(FUN))))
}
f2 <- f[-1]
FUN2 <- function() {
arglist <- c(arg, as.list(match.call())[-1])
do.call(FUN, arglist)
}
@leeper
leeper / make_patch.R
Last active September 11, 2016 17:22
Compare two data.frames and generate the code to patch the differences
# function to identify what changed where
which_changed <- function(x, y = x) {
# argument validation
stopifnot(inherits(x, "data.frame"))
stopifnot(inherits(y, "data.frame"))
stopifnot(identical(dim(x), dim(y)))
y <- y[, names(x)]
# compare objects
@leeper
leeper / compare_coefficients.R
Last active October 14, 2016 14:09
play around with plotting one model estimate against others
get_all_estimates <- function(data, x, y, power = 1L) {
xvar <- x
assign(xvar, data[[xvar]])
yvar <- y
assign(yvar, data[[yvar]])
data <- data[, !names(data) %in% c(xvar, yvar), drop = FALSE]
datalist <- list()
@leeper
leeper / epochalypse.R
Created May 16, 2016 10:01
notes on calendar conversions
# excel windows and mac (see `as.Date()`)
# julian/gregorian; new style/old style
# mayan calendar
# armenian
# assyrian
# baha'i
# benghali
# berber
# buddhist
# burmese
@leeper
leeper / jsevents.html
Last active May 16, 2016 08:24
Javascript event tracking
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div>
<form>
<div>
<p>Mouse: <input type="text" id="xmove" />
<input type="text" id="ymove" />
@leeper
leeper / checkurls.R
Last active April 30, 2016 06:36
Check URLs in a document
# Check URLs in a document
## This code will extract URLs from a text document using regex,
## then execute an HTTP HEAD request on each and report whether
## the request failed, whether a redirect occurred, etc. It might
## be useful for cleaning up linkrot.
if (!require("httr")) {
install.packages("httr", repos = "http://cran.rstudio.com/")
}
@leeper
leeper / explot.R
Last active March 12, 2016 13:40
Playing with plotting difference-of-means tests
set.seed(1)
n <- 300
x <- rbinom(n, 1, .5)
y <- 4 + (2 * x) + rnorm(n, 0, 3)
m <- tapply(y, x, mean)
wm <- weighted.mean(c(mean(y[x==1]), mean(y[x==0])), c(length(y[x==1]), length(y[x==0])))
ci <- function(formula, conf.level, var.equal = FALSE) {
(tt <- t.test(formula, var.equal = var.equal, conf.level = conf.level))
rect(0, wm - diff(tt$conf.int)/2,