Skip to content

Instantly share code, notes, and snippets.

View hanowell's full-sized avatar

Ben Hanowell hanowell

View GitHub Profile
library(jtools)
library(plm)
i <- rep(1:100, each = 100)
v <- rep(rnorm(100), each = 100)
a <- rnorm(10000)
t <- a + v + rnorm(10000)
y <- a + v + t + rnorm(10000)
u <- t + y + rnorm(10000)
sample_list <- list(rnorm(1000, 0, 1))
mean_vec <- c(mean(sample_list[[1]]))
sd_vec <- c(sd(sample_list[[1]]))
chains <- tibble::tibble(chain = rep,
mean_value = NA,
sd_value = NA)
for (i in 2:100000) {
sample_list[[i]] <- rnorm(1000, mean_vec[i - 1], sd_vec[i - 1])
mean_vec[i] <- mean(sample_list[[i]])
sd_vec[i] <- sd(sample_list[[i]])
@hanowell
hanowell / how_to_measure_attrition_part_2.R
Last active March 27, 2019 16:35
how_to_measure_attrition_part_2
# Load packages ----
library(tidyverse)
# Create US-state-level headcount dataset ----
business_markets <- tibble::tribble(
~city_name, ~state_name, ~mean_headcount,
"Austin", "TX", 8,
"Denver", "CO", 22,
"Los Angeles", "CA", 97,
"Minneapolis", "MN", 7,
@hanowell
hanowell / how_to_measure_attrition_part_1.R
Created March 21, 2019 22:37
how_to_measure_attrition_part_1
# Load packages ----
library(tidyverse)
library(RcppRoll)
library(scales)
# Create three different headcount growth models ----
dates <- seq.Date(as.Date("2006-01-01"), as.Date("2018-12-31"), by = "day") %>%
tibble::tibble(daily_date = ., t = 1:length(.)) %>%
dplyr::mutate(
month_begin_date = lubridate::floor_date(daily_date, unit = "month")
@hanowell
hanowell / nested_donut_bullshit.R
Last active February 17, 2017 17:55
Here is some code to make some plots demonstrating the lie factor that results from using nested donut plots when you should just use a bar chart.
# Clear. Dat. Workspace.
rm(list = ls())
# Load. Dem. Packages.
library(tidyverse)
# Create. Dat. Data.
dat <- data.frame(category = LETTERS[1:4],
fraction = c(0, 20, 30, 50)) # Extra zero datapoint to keep hole in the donut.
@hanowell
hanowell / depressing_plots
Last active May 25, 2016 20:57
This Gist creates a plot of smoothed zip-code-tabulation-area-level median household income against the percent of non-White residents, percent of Black residents and percent of Hispanic residents. Expect to be depressed. But first, set up your Census API key following the instructions in the link that follows. See section 3.3. http://eglenn.scr…
library(acs)
library(dplyr)
library(ggplot2)
zips <- acs::geo.make(zip = "*")
# Get median household income.
income <-
acs::acs.fetch(
endyear = 2014, span = 5,