# Call sources for "Shift, Not Stasis: The Geography of Post-Civil Rights Racial Inequality"

# Set this to the path of the replication package on your machine
setwd('PATH/TO/replication_package/')

# Set each of these to the file name (without extension) of the corresponding
# data extract you downloaded. See the Readme for details on each extract.
ipumsname = 'IPUMS_EXTRACT_NAME'              # IPUMS USA microdata (raw_data/ipums/<name>.Rdata)
cpsname = 'CPS_EXTRACT_NAME'                  # IPUMS CPS extract for unionization (raw_data/union_cps/<name>.xml and .dat)
nhgis_1970pop_name = 'NHGIS_1970POP_NAME'     # NHGIS 1970 county-level race population (raw_data/1970_crosswalk/<name>_csv/<name>_ds94_1970_county.csv)
nhgis_ind_name = 'NHGIS_INDUSTRIES_NAME'      # NHGIS county-level employment by industry (raw_data/nhgis_industries/<name>_*.csv)
nhgis_seg_name = 'NHGIS_SEGREGATION_NAME'     # NHGIS tract-level race counts for segregation (raw_data/nhgis_segregation/<name>_*.csv)
nhgis_slavery_name = 'NHGIS_SLAVERY_NAME'     # NHGIS 1860 county-level slavery extract (raw_data/nhgis_racial_oppression/<name>_csv/*.csv)
nhgis_sharecrop_name = 'NHGIS_SHARECROP_NAME' # NHGIS 1930 county-level sharecropping extract (raw_data/nhgis_racial_oppression/<name>_csv/*.csv)

library(tidyverse)
library(segregation)
library(Hmisc)
library(weights)
library(readstata13)
library(haven)

# Build code - clean and construct main outcomes
source('code/build_code/prep_1_xwalk_1970_byrace.R')
source('code/build_code/prep_2_pumas_to_msas.R')
source('code/build_code/prep_3_spm.R')
source('code/build_code/prep_4_ipums.R')
source('code/build_code/prep_5_sumstats.R')
source('code/build_code/prep_6_matched_sumstats_weighted.R')

# Build code - create predictor variables
source('code/build_code/prep_7_union_cps.R')
source('code/build_code/prep_8_cen_industry.R')
source('code/build_code/prep_9_segregation.R')
source('code/build_code/prep_10_segregation_ltdb.R')
source('code/build_code/prep_11_racial_oppression.R')

# At this point, stop the program until the HRR factor has been created in Stata
# using the program "prep_12_racial_oppression_factor.do"
# When that program has run, press [Enter] to continue
readline(prompt = "Pause this script until you have run prep_12_racial_oppression_factor.do in Stata. Once that has run, press [Enter]")

# Build code - create MSA-year-level datasets with outcomes and predictor variables.
source('code/build_code/prep_13_regressions_outcomes.R')
source('code/build_code/prep_14_regressions_predictors.R')


# Analysis code
library(tigris); options(tigris_use_cache = TRUE)
library(sf);
library(ggrepel)
library(scales)
theme_set(theme_bw())

library(lm.beta)
library(estimatr)
library(car)
library(gridExtra)

# Run regressions and decomposition
source('code/analysis_code/analysis_1_regressions.R')
source('code/analysis_code/analysis_2_reg_sigchange.R')
source('code/analysis_code/analysis_3_decomp_rr.R')
source('code/analysis_code/analysis_4_cormatrix.R')

# Create output figures and tables
source('code/analysis_code/graphs_1_trends.R')
source('code/analysis_code/graphs_2_maps_outcomes.R')
source('code/analysis_code/graphs_3_maps_medinc.R')
source('code/analysis_code/graphs_4_reg_coefs.R')
source('code/analysis_code/graphs_5_reg_table.R')
source('code/analysis_code/graphs_6_statistics_for_paper.R')

