Skip to content

Instantly share code, notes, and snippets.

View grantmcdermott's full-sized avatar

Grant McDermott grantmcdermott

View GitHub Profile
View maths-economics-2b-schelling
rm(list=ls())
library(dplyr)
library(ggplot2)
library(cowplot)
library(extrafont)
greens <-
data_frame(pop = seq(0, 50, length = 51)) %>%
mutate(tol = 2 - pop/(50/2)) %>%
@grantmcdermott
grantmcdermott / default_aes.md
Created August 20, 2019 01:14 — forked from dpseidel/default_aes.md
Default ggplot2 aesthetics table
View default_aes.md

Default ggplot2 aesthetics by geom.

geom alpha angle colour family fill fontface height hjust lineheight linetype shape size stroke vjust weight width xmax xmin ymax ymin
GeomAbline NA black 1 0.5
GeomAnnotationMap NA NA grey20 1 0.5
GeomArea NA NA grey20 1 0.5
GeomBar NA NA grey35 1 0.
View fe-cluster-sim.csv
cl y x fe
1 1.5370923925782167 -20.96212959102115 1
1 -1.8734183679775684 -1.839943693864785 1
1 7.550159713148152 17.039214859757113 1
1 2.8293020975816763 16.50853213076337 1
1 -1.5116432128953963 13.428072424536378 1
1 3.3770810866412213 14.014340195044714 2
1 4.006253032023515 -1.209196878297718 2
1 3.4871187962096153 3.077224608721335 2
1 0.042378253992417764 -5.207642778212252 2
@grantmcdermott
grantmcdermott / bland_scrape.R
Last active November 8, 2019 19:48
RSelenium example
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
@grantmcdermott
grantmcdermott / hsantanna_scrape.R
Last active November 19, 2019 21:00
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 gfw2016-rayshader.R
### LOAD PACKAGES ###
# install.packages("pacman")
pacman::p_load(tidyverse, bigrquery, DBI, sf, stars, rayshader)
### DOWNLOAD GLOBAL FISHING WATCH DATA FROM GOOGLE BIGQUERY ###
## You'll need a valid BigQuery billing ID for this next part. Luckily, you can
## easily sign up for a 12-month free trial that gives you access to BQ, as well
@grantmcdermott
grantmcdermott / zip_census.R
Last active January 23, 2020 18:13
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 / replace_comp.R
Last active April 13, 2020 22:31
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 / gh_commit_msgs.R
Last active June 16, 2020 04:28
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 / refi-comp.R
Last active July 14, 2020 17:33
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