Skip to content

Instantly share code, notes, and snippets.

View leeper's full-sized avatar

Thomas J. Leeper leeper

View GitHub Profile
# setup
library("survey")
data("api")
apiclus1$proportion <- apiclus1$pcttest/100
dclus1<-svydesign(id=~dnum, fpc=~fpc, data=apiclus1)
# confint(svymean(...) == svyciprop(..., method = "me")
confint(svymean(~ proportion, dclus1))
## 2.5 % 97.5 %
## proportion 0.9852196 0.9943432
@leeper
leeper / word_count.R
Created August 16, 2018 22:07
R function to obtain a word count from a PDF
# Copyright (c) 2018 Thomas J. Leeper
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
@leeper
leeper / file.do
Created May 24, 2018 11:06
Corrected do file
* replication code: Causal Inference in Conjoint Analysis:
* Understanding Multidimensional Choices via Stated Preference Experiments
* Jens Hainmueller, Daniel Hopkins, Teppei Yamamoto
clear all
set more off
ssc install matmap
ssc install mat2txt
@leeper
leeper / stata_summary.R
Created February 21, 2018 10:53
Stata-style regression summaries
stata_summary <-
function(
x,
...
) {
# summarize
mod <- x
x <- summary(x)
# find outcome variable
@leeper
leeper / pdfs_to_bib.R
Created August 9, 2017 08:11
Small script to build a bibtex file from a collection of PDF files
library("pdftools")
# load files
files <- dir(pattern = "pdf$")
# setup metadata
year <- ifelse(grepl("\\d{4}", files), regmatches(files, regexpr("(?<=, )\\d{4}", files, perl = TRUE)), "")
journal <- unlist(lapply(files, function(x) {
if (grepl("(?<=\\().+(?=, \\d{4})", x, perl = TRUE)) {
regmatches(x, regexpr("(?<=\\().*?(?=, \\d{4})", x, perl = TRUE))
@leeper
leeper / base-code.R
Last active July 6, 2017 21:23
Base R Equivalents of the "Teach the Tidyverse to Beginners" Example
url <- "http://varianceexplained.org/files/Brauer2008_DataSet1.tds"
# Clean and tidy the data
d1 <- rio::import(url, format = "tsv")
d2 <- cbind(d1, setNames(do.call(rbind.data.frame,strsplit(d1$NAME, " ?\\|\\| ?"))[,-5],
c("name", "BP", "MF", "systematic_name")))
d3 <-
subset(
within(
reshape(d2,
@leeper
leeper / rsqlite-tutorial.R
Created June 18, 2017 13:05
Old code from an RSQLite tutorial
# R and SQLite tutorial
# Steph Locke, Mango Solutions
# 2015-11-30
library("RSQLite")
library("DBI")
dbListTables(datasetsDb())[1:2]
myDB <- dbConnect(SQLite(), "local.db")
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))
@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
@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] "¯\\___(ツ)___/¯" "¯\\_(ツ)_/¯" "¯\\(ツ)/¯" "(ツ)"