Skip to content

Instantly share code, notes, and snippets.

View kylebutts's full-sized avatar

Kyle F Butts kylebutts

View GitHub Profile
@kylebutts
kylebutts / sf_geos_vs_s2_benchmark.R
Last active December 10, 2021 20:40
Speed comparison for sf package with geos and s2 backends
library(sf)
library(s2)
nc <- sf::st_read(system.file("shape/nc.shp", package="sf"))
nc_s2 <- s2::as_s2_geography(nc)
ncpts_sf <- sf::st_sample(nc, 1000)
ncpts_s2 <- s2::as_s2_geography(ncpts_sf)
# ---- Distance Matrix ---------------------------------------------------------
bench::mark(
sf::st_distance(ncpts_sf),
@kylebutts
kylebutts / did2s.html
Last active March 4, 2022 18:41
did2s_article
This file has been truncated, but you can view the full file.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang xml:lang>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
<meta name="generator" content="distill" />
@kylebutts
kylebutts / data_to_latex_body.R
Created March 9, 2022 05:22
Export data.frame to latex body
#' Prepares table body from dataframe
#'
#' @description Note this will copy the latex code into your clipboard. This is
#' for my ease of use
#'
#' @param df Dataframe to convert to latex
df_to_table_body <- function(df, col_formatters = NULL) {
# Figure out maximum length of each column (for pretty layout)
max_col_lengths <- rep(0, times = ncol(df))
@kylebutts
kylebutts / tailwind.Rmd
Created May 9, 2022 21:40
Example of using Tailwind & Rmd
---
output:
html_document:
highlight: zenburn
self_contained: false
slim_css: TRUE
css: []
theme: null
---
@kylebutts
kylebutts / equivalence_did2s_with_individual_demeaned.R
Created June 30, 2022 20:30
With many individuals, can save a lot of time by individually demeaning everything before running `did2s`. The results are numerically equivalent
library(did2s)
library(data.table)
data(df_hom)
setDT(df_hom)
# ---- Unit FE by Dummy Variables ----------------------------------------------
did2s(df_hom,
yname = "dep_var", treatment = "treat", cluster_var = "unit",
first_stage = ~ 0 | unit + year,
library(data.table)
library(fixest)
library(did2s)
#> ℹ did2s (v0.7.0). For more information on the methodology, visit <https://www.kylebutts.com/did2s>
#> To cite did2s in publications use:
#> 
#>   Butts, Kyle (2021).  did2s: Two-Stage Difference-in-Differences
#>   Following Gardner (2021). R package version 0.7.0.
#> 
@kylebutts
kylebutts / 0.md
Last active May 8, 2023 18:04
`hatvalues.fixest` implementation

Fast implementation of hatvalues.fixest. This calculates (or approximates) the diagonals of the projection matrix $X (X' X)^{-1} X'$.

@kylebutts
kylebutts / ex.md
Created June 7, 2023 16:24
HC2 - HC1 is not necessarily PSD
library(sandwich)

# high-leverage observation #5
X = matrix(
  c(
    1, 1, 2,
    1, 2, 3,
    4, 5, 5,
 1, 1, 1,
@kylebutts
kylebutts / blockDiagonal.R
Created July 10, 2023 16:12
Create block diagonal matrix from a list of matrices in R
library(Matrix)
#' Given list of matrices, form block sparse matrix
#'
#' @param mats List of sparse matrices `dgCMatrix`.
#'
#' @return A block diagonal `dgCMatrix` with each element of mats
#' along the diagonal.
blockDiagonal <- function(mats) {
mats = lapply(mats, function(mat) {
@kylebutts
kylebutts / dont_invert_that_matrix.R
Created July 31, 2023 15:40
Don't invert that matrix
# https://www.johndcook.com/blog/2010/01/19/dont-invert-that-matrix/
A = matrix(runif(1000 * 1000), nrow = 1000, ncol = 1000)
b = matrix(runif(1000), ncol = 1)
# Solve Ax = b
max(solve(A, b) - solve(A) %*% b)
#> [1] 8.537882e-11
# Benchmark
bench::mark(