Skip to content

Instantly share code, notes, and snippets.

View grantmcdermott's full-sized avatar

Grant McDermott grantmcdermott

View GitHub Profile
@grantmcdermott
grantmcdermott / duckdb-dplyr-ex.R
Created September 30, 2023 01:22
Simple examples of how to use DuckDBs dplyr front-end in R
# preliminaries -----
# packages you'll need (install these if you don't have them)
# install.packages(c("arrow", "duckdb", "dplyr", "tidyr"))
# write a dummy parquet dataset (i.e., multiple parquet files) to disk
dir.create("airquality")
arrow::write_dataset(
airquality,
path = "airquality",
@grantmcdermott
grantmcdermott / gg-floating-axes.R
Created February 20, 2023 06:06
Floating axes in ggplot2 (via ggh4x)
# Context: https://mastodon.social/@gmcd/109895547902826763
library(ggh4x)
theme_set(
theme_bw() +
theme(
axis.line = element_line(linewidth = 0.4),
axis.ticks = element_line(linewidth = 0.4),
panel.grid.minor = element_blank(),
@grantmcdermott
grantmcdermott / baker.R
Created February 21, 2022 01:29
Generate the "Baker" simulated dataset for staggered treatment effects
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 February 15, 2022 02:18
Benchmarking collapse_mask
## 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')
@grantmcdermott
grantmcdermott / syllabus.qmd
Created February 13, 2022 00:45
EC 311 syllabus
---
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 18:50
Bayesian bootstrap
# 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 December 24, 2021 08:12
A ggplot2 implementation fixest::iplot()
## 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 January 29, 2021 00:42
Testing out the new tidysynth package on Cunningham (2021, chap 10.)
# 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 August 28, 2021 22:15
Redoing the simulation described on Dan Millimet's blog: https://dlm-econometrics.blogspot.com/2020/08/eye-on-prize.html
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 August 12, 2020 22:19
Example using broom::augment(interval = '..') to visualize out of sample predictions
## 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)