Skip to content

Instantly share code, notes, and snippets.

View kylebutts's full-sized avatar

Kyle F Butts kylebutts

View GitHub Profile

Brewing Log 🍾

Blueberry Ginger Beer (9/6)

  • 1.5 gallons water
  • 3 cup sugar
  • 2 cups Ginger
  • 2 lb. Blueberry (primary)
  • EC-1118

Starting Gravity: 1.055

@kylebutts
kylebutts / make_panel_present.R
Last active October 30, 2020 14:12
Make Spreadsheet to check for missing obs. in panels. Row is id, column is year, element = 1 if present.
# Calling the panel identifiers as id, year
df %>%
mutate(present = 1) %>%
complete(nesting(id), year) %>%
mutate(present = replace_na(present, 0)) %>%
select(id, year, present) %>%
pivot_wider(names_from = year, values_from = present)
library(texreg)
extract_body <- function(gt) {
if(inherits(gt, "gt_tbl")) gt <- as.character(gt::as_latex(gt))
stringr::str_match(gt, "(?s)\\\\midrule\\n(.*)\\\\bottomrule")[[2]]
}
df <- tibble(
treat = 1:20 > 10,
library(tidyverse)
library(sf)
library(tigris)
library(rmapshaper)
source("https://gist.githubusercontent.com/kylebutts/7dc66a01ec7e499faa90b4f1fd46ef9f/raw/15196997e5aad41696b03c49be3bfaaca132fdf2/theme_kyle.R")
counties <- tigris::counties(class = "sf") %>%
# Continental
filter(!(STATEFP %in% c("02", "15", "60", "66", "69", "72", "78"))) %>%
gistup
@kylebutts
kylebutts / make_table.R
Created November 16, 2020 22:15
summary_table_body.R
library(tidyverse)
df <- mtcars %>%
group_by(cyl) %>%
summarize(mean_mpg = mean(mpg), sd_mpg = sd(mpg)) %>%
mutate(
mpg_tex = paste0(format(round(mean_mpg, 2), nsmall = 2), " (", format(round(sd_mpg, 2), nsmall = 2), ")"),
tex = paste(cyl, mpg_tex, sep = " & ")
)
@kylebutts
kylebutts / cps3re74.csv
Last active November 22, 2020 16:57
Data for Oxaca-Blinder Estimator for Treatment Effect Estimation - Kline, 2011 and Kline, 2014
treat age ed black hisp married nodeg re74 re75 re78 age2
1 37 11 1 0 1 1 0 0 9930.0458984375 1369
1 22 9 0 1 0 1 0 0 3595.89404296875 484
1 30 12 1 0 0 0 0 0 24909.44921875 900
1 27 11 1 0 0 1 0 0 7506.14599609375 729
1 33 8 1 0 0 1 0 0 289.789886474609 1089
1 22 9 1 0 0 1 0 0 4056.49389648438 484
1 23 12 1 0 0 0 0 0 0 529
1 32 11 1 0 0 1 0 0 8472.158203125 1024
1 22 16 1 0 0 0 0 0 2164.02197265625 484
@kylebutts
kylebutts / oaxaca-blinder-estimator.R
Last active November 23, 2020 01:44
Oxaca-Blinder Estimator for Treatment Effect Estimation - Kline, 2011 and Kline, 2014
## -----------------------------------------------------------------------------
## oaxaca-blinder-estimator.R
## Kyle Butts, CU Boulder Economics
##
## This creates R functions for an Oaxaca-Blinder Estimator following:
## Patrick Kline - Oaxaca-Blinder as a Reweighting Estimator (2011)
## Patrick Kline - A note on variance estimation for the Oaxaca estimator of average treatment effects (2014)
## -----------------------------------------------------------------------------
@kylebutts
kylebutts / Conley Spatial-HAC Standard Errors.md
Last active August 25, 2021 16:24
Conley Spatial-HAC Standard Errors with Rcpp
@kylebutts
kylebutts / extract_body_gt.R
Last active December 7, 2020 23:16
Extract Latex body from gt table
#' Requires gt::gt() object or gt::as_latex() object and returns just the body of the gt table
extract_body_gt <- function(gt) {
if(inherits(gt, "gt_tbl")) gt <- as.character(gt::as_latex(gt))
stringr::str_match(gt, "(?s)\\\\midrule\\n(.*)\\\\bottomrule")[[2]]
}
library(tidyverse, warn.conflicts = FALSE)
library(gt, warn.conflicts = FALSE)
# HTML