Skip to content

Instantly share code, notes, and snippets.

Avatar

Grant McDermott grantmcdermott

View GitHub Profile
@grantmcdermott
grantmcdermott / baker.R
Created Feb 21, 2022
Generate the "Baker" simulated dataset for staggered treatment effects
View baker.R
set.seed(1234)
# Create the base dataset as a cross-join of 1,000 firms over 30 periods
baker = expand.grid(n = 1:30, id = 1:1000)
# Add additional columns
baker =
baker |>
within({
year = n + 1980 - 1
@grantmcdermott
grantmcdermott / collapse_mask.R
Created Feb 15, 2022
Benchmarking collapse_mask
View collapse_mask.R
## Context: https://twitter.com/grant_mcdermott/status/1493400952878952448
options(collapse_mask = "all") # NB: see `help('collapse-options')`
library(dplyr)
library(data.table)
library(collapse) # Needs to come after library(dplyr) for collapse_mask to work
flights = fread('https://raw.githubusercontent.com/Rdatatable/data.table/master/vignettes/flights14.csv')
View syllabus.qmd
---
title: "Intermediate Microeconomics (EC 311)"
subtitle: "Syllabus"
author: "Grant McDermott"
date: "Department of Economics, University of Oregon"
format:
pdf:
pdf-engine: pdflatex
documentclass: article
toc: false
@grantmcdermott
grantmcdermott / bboot.R
Last active May 6, 2022
Bayesian bootstrap
View bboot.R
# Context: https://twitter.com/grant_mcdermott/status/1487528757418102787
library(data.table)
library(fixest)
bboot =
function(object, reps = 100L, cluster = NULL, ...) {
fixest_obj = inherits(object, c('fixest', 'fixest_multi'))
@grantmcdermott
grantmcdermott / ggiplot-examples.R
Last active Dec 24, 2021
A ggplot2 implementation fixest::iplot()
View ggiplot-examples.R
## Examples, borrowing from the intro vignette
library(fixest)
library(ggplot2)
## Source the function
source('https://gist.githubusercontent.com/grantmcdermott/db8501b0281813792bf78393fef98481/raw/ggiplot.R')
## Vanilla TWFE
est_did = feols(y ~ x1 + i(period, treat, 5) | id + period, base_did)
@grantmcdermott
grantmcdermott / mixtape-tidysynth.R
Created Jan 29, 2021
Testing out the new tidysynth package on Cunningham (2021, chap 10.)
View mixtape-tidysynth.R
# Libraries ---------------------------------------------------------------
library(tidysynth)
library(haven)
# Data --------------------------------------------------------------------
texas = read_dta('https://raw.github.com/scunning1975/mixtape/master/texas.dta')
@grantmcdermott
grantmcdermott / ols-vs-iv.R
Last active Aug 28, 2021
Redoing the simulation described on Dan Millimet's blog: https://dlm-econometrics.blogspot.com/2020/08/eye-on-prize.html
View ols-vs-iv.R
library(ivreg)
## Convenience functions
rmse = function(y, yhat) sqrt(mean((y - yhat)^2))
mae = function(y, yhat) mean(abs(y - yhat))
## Function for single simulation
dm_sim = function(...) {
## Params
b0 = 1; b1 = 1; rhoXZ = 0.5; rho1 = 0.25
@grantmcdermott
grantmcdermott / augment_interval_ex.R
Last active Aug 12, 2020
Example using broom::augment(interval = '..') to visualize out of sample predictions
View augment_interval_ex.R
## Context: https://twitter.com/grant_mcdermott/status/1293673082352099328
library(broom) ## remotes::install_github('tidymodels/broom')
library(ggplot2)
## gen fake data
set.seed(123)
x = sort(runif(100, min = 0, max = 5))
y = x^2 + rnorm(100)
dat = data.frame(x, y)
@grantmcdermott
grantmcdermott / refi-comp.R
Last active Jul 14, 2020
Compare 15- vs 30-yr refi
View refi-comp.R
#' Compare expected returns of 15- vs 30-year home refi.
#'
#' This very simple function assumes you split your disposable income between
#' mortgage repayments and stock investments. The goal is to compare ROI at the end
#' of 30 years, assuming you initially have a choice between 15- and 30-year fixed rate
#' mortgage.
#'
#' @details Under the 15-year scenario, we assume that you pay the monthly premium
#' until the loan is fully repaid (i.e. after 15 years) and then immediately switch
#' over to investing the exact same amount for another 15 years. Under the 30-year
@grantmcdermott
grantmcdermott / gh_commit_msgs.R
Last active Jun 16, 2020
Simple function for extracting commit messages from a GitHub user profile
View gh_commit_msgs.R
## Context: https://twitter.com/grant_mcdermott/status/1272747522562244608
## Note: To get the most out of this function (private repos, etc.), I recommend
## adding a GitHub Personal Access Token to your .Renviron file. The easiest way
## to do this is with the {usethis} package. See here:
## https://happygitwithr.com/github-pat.html#step-by-step
library(gh)
library(purrr)
library(data.table)