Skip to content

Instantly share code, notes, and snippets.

Avatar

Grant McDermott grantmcdermott

View GitHub Profile
View ols-vs-iv.R
library(estimatr)
library(data.table)
## Function for single simulation
dm_sim = function(...) {
## Params
b0 = 1; b1 = 1; rhoXZ = 0.5; rho1 = 0.25
rho2_vec = c(0.5, 0.25, 0, -0.25, -0.5)
nobs1 = 1000; nobs2 = 100
## "In" sample draw
@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)
@grantmcdermott
grantmcdermott / hidpi.sh
Last active May 20, 2020
Script for fixing HiDPI issues on Linux (for programs that I use). Useful for automating steps outlined here: https://wiki.archlinux.org/index.php/HiDPI#Applications
View hidpi.sh
#!/bin/sh
DDIR='/usr/share/applications'
## There are two basic hidpi cases that we need to distinguish and correct for:
## 1) Electron-based apps and 2) QT-based apps
## 1) Electron-based apps (append '--force-scale-device-factor=2' to executable)
for FILE in atom.desktop brave-browser.desktop code-oss.desktop google-chrome.desktop skypeforlinux.desktop slack.desktop
do
@grantmcdermott
grantmcdermott / replace_comp.R
Last active Apr 13, 2020
Replace by position
View replace_comp.R
## Context: https://twitter.com/grant_mcdermott/status/1249825585175523328
set.seed(123)
m = matrix(1:10000000, ncol = 1000)
x_pos = sample.int(nrow(m), size = nrow(m)/2)
y_pos = sample.int(ncol(m), size = ncol(m)/2)
m_pos = (y_pos-1)*nrow(m) + x_pos
library(data.table)
@grantmcdermott
grantmcdermott / zip_census.R
Last active Jan 23, 2020
Get (possible) census tracts for address based on zip code
View zip_census.R
## Context: https://twitter.com/grant_mcdermott/status/1220409051013697553
library(tidycensus)
# census_api_key("YOUR KEY GOES HERE", install = TRUE)
library(tigris)
options(tigris_use_cache = TRUE)
library(sf)
library(tidyverse)
## Get ZIP codes for whole US. First time you run this it will take a while, but
@grantmcdermott
grantmcdermott / coef-plots.R
Created Dec 27, 2019
Examples of how to plot multiple coefficients from multiple models (using ggplot2 and broom::tidy)
View coef-plots.R
## Context: https://twitter.com/grant_mcdermott/status/1210654424600731648
library(tidyverse)
library(broom)
library(hrbrthemes) ## theme(s) I like
df =
mtcars %>%
mutate(vs = factor(vs), am = factor(am))
@grantmcdermott
grantmcdermott / hsantanna_scrape.R
Last active Nov 19, 2019
Brazilian crime data
View hsantanna_scrape.R
## Context: https://twitter.com/hsantanna/status/1196184374481506304
## Thanks to Will May: https://twitter.com/williamcmay/status/1196268860418281472?s=20
library(tidyverse)
library(rvest)
library(memoise)
ssp_url = 'http://www.ssp.sp.gov.br/estatistica/pesquisa.aspx'
## get the region/municipality form
View bland_scrape.R
## Background: https://twitter.com/JamesBland_Econ/status/1192519670009434114
# devtools::install_github("ropensci/RSelenium")
library(RSelenium)
## Start Selenium driver and browser client
rsd = rsDriver(browser = "firefox") # "chrome" is the default
rsc = rsd$client
## Navigate to the page
You can’t perform that action at this time.