Skip to content

Instantly share code, notes, and snippets.

@benzipperer
benzipperer / realtalk_build_instructions.md
Last active January 31, 2025 21:26
instructions to build the realtalk package https://economic.github.io/realtalk/

realtalk R package build instructions

Requirements

Update frequency

monthly, upon BLS CPI release

Update instructions

@benzipperer
benzipperer / function_names.R
Created October 19, 2024 01:42
show all the function names in a given set of R scripts using the treesitter package
# show all the function names in a given set of R scripts
function_definitions = function(files) {
purrr:::map(files, function_definitions_script) |>
purrr::list_rbind()
}
function_definitions_script = function(file) {
text = brio::read_lines(file) |>
paste(collapse = "\n")
@benzipperer
benzipperer / cpi_u_rs_extended_annual.csv
Last active June 21, 2024 15:02
create old version of extended CPI-U-RS in R
year cpi_u_rs_extended
1937 24.3
1938 23.8
1939 23.5
1940 23.6
1941 24.9
1942 27.6
1943 29.2
1944 29.7
1945 30.4
@benzipperer
benzipperer / weighted_percentiles.R
Created December 13, 2023 14:51
weighted percentiles and reshaping
library(tidyverse)
library(MetricsWeighted)
# here's how to calculate multiple weighted percentiles by year
# and reshape them so data is long in year but wide in percentiles
# below I explain this step by step
# first grab some data
cps_data <- epiextractr::load_org(1979:2022, year, orgwgt, wage) %>%
filter(wage > 0)
@benzipperer
benzipperer / stacked.R
Created November 16, 2023 21:48
stacked regression model tables using modelsummary and kableextra
library(tidyverse)
library(modelsummary)
library(kableExtra)
model_am1 = mtcars %>%
filter(am == 1) %>%
lm(mpg ~ wt, data = .)
model_am0 = mtcars %>%
filter(am == 0) %>%
@benzipperer
benzipperer / wage_ag051120.do
Last active October 25, 2023 16:50
modified replication package for DGLR 2020, to calculate mean employment of regression sample in Table A1, Women High School or Less
/* BEGIN BZ MODIFICATIONS */
/* this do-file requires the following files from the authors
medicaidelig.dta
UKCPR_National_Welfare_Data_Update_021520.dta
*/
copy https://github.com/Economic/state_geocodes/raw/master/state_geocodes.dta state_geocodes.dta, replace
use state_geocodes, clear
rename state_census stcps
@benzipperer
benzipperer / nursing_homes.R
Last active July 6, 2023 18:04
distribution of facility-level staffing ratios, by Census Region, June 2023
library(tidyverse)
library(ggridges)
# state codes
states_regions <- read_csv("https://raw.githubusercontent.com/cphalpert/census-regions/master/us%20census%20bureau%20regions%20and%20divisions.csv") %>%
select(state_abb = `State Code`, region = Region)
# CMS provider info
# https://data.cms.gov/provider-data/dataset/4pq5-n9py
raw <- read_csv("NH_ProviderInfo_Jun2023.csv") %>%
@benzipperer
benzipperer / school_shootings.R
Created November 16, 2022 15:57
plot time series of US K-12 shootings
library(tidyverse)
library(lubridate)
library(hrbrthemes)
# K-12 shooting incident data from https://www.chds.us/ssdb/data-map/
readxl::read_excel("SSDB_Raw_Data.xlsx", sheet = "INCIDENT") %>%
mutate(
year = year(ymd(Date)),
month = month(ymd(Date)),
month_date = ym(paste(year, month))
@benzipperer
benzipperer / wgt.R
Last active January 27, 2022 02:05
demonstrate the effects of Census rounding wages on Atlanta Fed Wage Tracker
library(tidyverse)
library(lubridate)
library(haven)
library(assertr)
library(slider)
library(hrbrthemes)
# function to bin hourly wages of hourly workers
bin_hourly <- function(df, new, old) {
df %>%