Skip to content

Instantly share code, notes, and snippets.

View leeper's full-sized avatar

Thomas J. Leeper leeper

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / .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 / 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 / shrugp.R
Created November 21, 2016 12:01
Transform p-values into "shruggies" of confidence-specific shrugginess
shrugp <- function(p) {
out <- symnum(p, cutpoints = c(0, 0.001, 0.01, 0.05, 0.10, 1.00),
symbols = c("(ツ)", "¯\\(ツ)/¯", "¯\\_(ツ)_/¯", "¯\\___(ツ)___/¯", "¯\\____(ツ)____/¯"))
as.character(out)
}
pvals <- c(0.09, 0.03, 0.002, 0.000001)
shrugp(pvals)
## [1] "¯\\___(ツ)___/¯" "¯\\_(ツ)_/¯" "¯\\(ツ)/¯" "(ツ)"
@leeper
leeper / ttttable.R
Last active May 19, 2021 22:38
Grammar of Tables?
# ttable: a grammar of tables
# https://gist.github.com/leeper/f9cfbe6bd185763762e126a4d8d7c286
# aggregate/summarize
# arrange
# annotation (metadata features)
# theme
library("rtruncnorm")
library("prediction")
library("margins")
set.seed(14850)
n <- 300
error <- 4
ex <- data.frame(
d = rbinom(n,1,.5),
x = rnorm(n),
e = rnorm(n,0,error))