# Make clean tables of regression coefficients for text and appendix

dir.create('output/regressions/tables')

# Bring in output
coefs = read_csv('output/regressions/all_coefs.csv')
r2s = read_csv('output/regressions/all_r2s.csv')



# Clean variable names
fullvars = unique(coefs$var)
print(fullvars)


fullvars = c("(Intercept)","logpop10",
             "pct_b_pop","pct_o_pop",  "pct_all_cit_imm",
             "union_combined","cemp_pct_manuf","cemp_pct_public",
             "dissimilarity_index",'dissimilarity_index_ltdb',
             "pct_all_col_ba",'pct_ba_lograt_bw','hrrv',
             'incar_comb_total','incar_comb_bwlrat',
             "regionMidwest","regionSouth","regionWest", 
             'incar_comb_bwdif','total_prison_pop_rate','imprate_vera_lrat_bw',
             'iso_black','iso_black_ltdb','exposure_b_w','exposure_b_w_ltdb','theil','theil_ltdb',
             'slave_pct:hrrvalid','cropfarms_pctblack:hrrvalid','hrrbaker:hrrvalid','hrr4:hrrvalid','numdis:hrrvalid','southman:hrrvalid','hrr_st:hrrvalid','hrr4_st:hrrvalid',
             'r2','nobs')

cleanvars = c('Intercept','MSA population (log10)', 
              '% Black','% neither Black nor white','% foreign-born',
              'Unionization rate','% Manufacturing','% Public administration',
              'BW dissimilarity index','BW dissimilarity index (LTDB)',
              "% with bachelor's degree",'Logged BA ratio','HRR factor',
              'State incarceration rate','Logged incarc. ratio',
              'Midwest','South','West',
              'BW difference in incarc.','State imprisionment rate','Logged imp. ratio',
              'Black isolation index','Black isolation index (LTDB)','BW exposure index','BW exposure index (LTDB)','BW Theil index','BW Theil index (LTDB)',
              '% Enslaved, 1860','% Black\nsharecroppers, 1930','HRR factor','HRR factor','Number of\ndisfranchisement devices','Support for\nSouthern Manifesto','HRR factor','HRR factor',
              'R-squared','Observations')

coefs$Variable = factor(coefs$var, levels = fullvars, labels = cleanvars)


# Do in loop all three
for(outv in c('med_ldiff','med_b','med_w' )){
# For text, we want baseline model for all years 
textcoefs = coefs %>% filter(racedef == 'expansive', incsamp == 'all_faminc_sqrt', mod == 'Baseline', outvar %in% outv) #,'med_b','med_w' ))

# Make string of coef with stars for significance
textcoefs = mutate(textcoefs, 
                   coefclean = case_when(
                     pvalue < .01 ~ paste(round(coef,3),'***',sep = ''),
                     pvalue < .05 & pvalue >= .01 ~ paste(round(coef,3),'**',sep = ''),
                     pvalue < .1 & pvalue >= .05 ~ paste(round(coef,3),'*',sep = ''),
                     pvalue >= .05 ~ paste(round(coef,3),'',sep = '')
                   ),
                   lowconf = (coef - se * 1.96) %>% round(digits = 3) %>% as.character(),
                   hiconf = (coef + se * 1.96) %>% round(digits = 3) %>% as.character(),
                   confinf = paste0('(',lowconf,', ',hiconf,')'),
                   outvar = factor(outvar, levels = c('med_ldiff','med_b','med_w'))
                   ) 


# Now convert to wide format 
textwide = textcoefs %>% select(Variable,year,outvar,coefclean,confinf) %>% pivot_longer(cols = c(coefclean, confinf)) 
textwide$name = factor(textwide$name, levels = c('coefclean','confinf'))
textwide = textwide %>% pivot_wider(id_cols = Variable,names_sort = T,names_from = c(year, outvar,name), values_from = c(value)) 

  

# Now get R2
textr = r2s %>% filter(racedef == 'expansive', incsamp == 'all_faminc_sqrt', mod == 'Baseline', outvar %in% outv) %>% #,'med_b','med_w' )) %>% 
  select(year,outvar,r2,nobs) %>% pivot_longer(-c(year,outvar))
textr = mutate(textr, 
               value = str_pad(round(value, 2),3,'right',pad = '0'),
               Variable = factor(name, levels = c('r2','nobs'),labels = c('R-squared','Observations')))

textr = textr %>% select(-name) %>% pivot_wider(id_cols = Variable, names_from = c(year,outvar), values_from = value)
# textr$Variable = factor(textr$name, levels = c('r2','r2adj'),labels = c('R-squared','Observations'))
textr = textr %>% rename_with(~ paste(.x,'coefclean',sep = '_'),.cols = -Variable)

texttable = bind_rows(textwide, textr)
texttable = texttable %>% replace(is.na(.),'-')


write_csv(texttable, paste0('output/regressions/tables/reg_table_maintext_',outv,'.csv'))

}

# Try version with no CI, but all three coefficients

# For text, we want baseline model for all years 
textcoefs = coefs %>% filter(racedef == 'expansive', incsamp == 'all_faminc_sqrt', mod == 'Baseline', outvar %in% c('med_ldiff','med_b','med_w' ))

# Make string of coef with stars for significance
textcoefs = mutate(textcoefs, 
                   coefclean = case_when(
                     pvalue < .01 ~ paste(round(coef,3),'***',sep = ''),
                     pvalue < .05 & pvalue >= .01 ~ paste(round(coef,3),'**',sep = ''),
                     pvalue < .1 & pvalue >= .05 ~ paste(round(coef,3),'*',sep = ''),
                     pvalue >= .05 ~ paste(round(coef,3),'',sep = '')
                   ),
                   # lowconf = (coef - se * 1.96) %>% round(digits = 3) %>% as.character(),
                   # hiconf = (coef + se * 1.96) %>% round(digits = 3) %>% as.character(),
                   # confinf = paste0('(',lowconf,', ',hiconf,')'),
                   outvar = factor(outvar, levels = c('med_ldiff','med_b','med_w'))
) 


# Now convert to wide format 
textwide = textcoefs %>% select(Variable,year,outvar,coefclean) %>% pivot_longer(cols = c(coefclean)) 
textwide$name = factor(textwide$name, levels = c('coefclean'))
textwide = textwide %>% pivot_wider(id_cols = Variable,names_sort = T,names_from = c(year, outvar,name), values_from = c(value)) 



# Now get R2
textr = r2s %>% filter(racedef == 'expansive', incsamp == 'all_faminc_sqrt', mod == 'Baseline', outvar %in% c('med_ldiff','med_b','med_w' )) %>% 
  select(year,outvar,r2,nobs) %>% pivot_longer(-c(year,outvar))
textr = mutate(textr, 
               value = str_pad(round(value, 2),3,'right',pad = '0'),
               Variable = factor(name, levels = c('r2','nobs'),labels = c('R-squared','Observations')))

textr = textr %>% select(-name) %>% pivot_wider(id_cols = Variable, names_from = c(year,outvar), values_from = value)
# textr$Variable = factor(textr$name, levels = c('r2','r2adj'),labels = c('R-squared','Observations'))
textr = textr %>% rename_with(~ paste(.x,'coefclean',sep = '_'),.cols = -Variable)

texttable = bind_rows(textwide, textr)
texttable = texttable %>% replace(is.na(.),'-')


write_csv(texttable, 'output/regressions/tables/reg_table_maintext_allthree.csv')


## Appendix tables

# Make clean scenario column for all the different appendices combined
# Baseline
coefsbl = filter(coefs, racedef == 'expansive', mod == 'Baseline',incsamp %in% c('all_faminc_sqrt'), outvar %in% c('med_ldiff'))
coefsbl$scen = 'Baseline'

# Different samples
coefs_samp = filter(coefs, racedef == 'expansive', mod == 'Baseline',
                 incsamp %in% c('gq_hh_faminc_sqrt','emp_emp_inctot','cit_usborn_faminc_sqrt','mig_samest_faminc_sqrt','mig_difst_faminc_sqrt'), 
                 outvar %in% c('med_ldiff')) # Different measures
coefs_samp$scen = coefs_samp$incsamp

# Different pop weights and MSA
coefs_wts = filter(coefs, racedef == 'expansive', mod %in% c('Consistent MSA sample','No non-MSA observations','No population weights','Full population weights'),incsamp %in% c('all_faminc_sqrt'), outvar %in% c('med_ldiff'))
coefs_wts$scen = coefs_wts$mod


# Different race definition
coefs_rdef = filter(coefs, racedef == 'narrow', mod == 'Baseline', incsamp %in% c('all_faminc_sqrt','cit_usborn_faminc_sqrt'),outvar %in% c('med_ldiff')) # Different race definition
coefs_rdef$scen = paste(coefs_rdef$racedef,coefs_rdef$incsamp,sep = '_')


# Different incomes and measures
coefs_inc = filter(coefs,racedef == 'expansive', mod == 'Baseline',incsamp %in% c('all_faminc_norm','all_faminc_tot','all_hhinc_sqrt','gq_hh_spmuinc_sqrt','gq_hh_spmuinc_disp_sqrt'), 
                outvar %in% c('med_ldiff')) # Different measures
coefs_inc$scen = coefs_inc$incsamp

# Rankd
coefs_rank =filter(coefs, racedef == 'expansive', mod == 'Baseline',incsamp %in% c('all_faminc_sqrt'), 
                     outvar %in% c('med_rgap')) # Different measures
coefs_rank$scen = coefs_rank$outvar

# matching 
coefs_match = filter(coefs, racedef == 'expansive', mod == 'Baseline',incsamp %in% c('all_faminc_sqrt_matched_b_age_sex'), 
                     outvar %in% c('med_ldiff')) # Different measures
coefs_match$scen = coefs_match$incsamp


# Different specifications
coefs_mod = filter(coefs, racedef == 'expansive', mod %in% c('Region dummies','HRR excluded','Segregation and incarceration excluded\n(full MSA sample in each year)','Incarceration inequality only','Incarceration difference','Imprisonment rate') ,incsamp %in% c('all_faminc_sqrt'), outvar %in% c('med_ldiff'))
coefs_mod$scen = coefs_mod$mod

# Differnet segregations
coefs_seg = filter(coefs, racedef == 'expansive', mod %in% c('Isolation index','Exposure index','Theil index','Dissimilarity index (consistent tract boundaries)','Isolation index (consistent tract boundaries)','Theil index (consistent tract boundaries)'),
                   incsamp %in% c('all_faminc_sqrt'), outvar %in% c('med_ldiff'))
coefs_seg$scen = coefs_seg$mod


# Different hrrs
coefs_hrr = filter(coefs, racedef == 'expansive', mod %in% c('4-component HRR','2-variable state-level HRR','4-variable state-level HRR','Baker HRR','Slavery only','Sharecropping only','Disfranchisement devices only','Southern Manifesto only'), incsamp %in% c('all_faminc_sqrt'), outvar %in% c('med_ldiff'))
coefs_hrr$scen = coefs_hrr$mod

# Different percentiles
coefs_pctiles = filter(coefs,racedef == 'expansive', mod %in% c('Baseline') ,incsamp %in% c('all_faminc_sqrt'), outvar %in% c('p05_ldiff','p10_ldiff','p25_ldiff','p75_ldiff','p90_ldiff','p95_ldiff','mean_ldiff'))
coefs_pctiles$scen = coefs_pctiles$outvar



coefsa = bind_rows(coefsbl,coefs_samp, coefs_wts,coefs_rdef,coefs_inc,coefs_rank,coefs_match,coefs_mod,coefs_seg,coefs_hrr,coefs_pctiles)


## Now R2s
r2sbl = filter(r2s, racedef == 'expansive', mod == 'Baseline',incsamp %in% c('all_faminc_sqrt'), outvar %in% c('med_ldiff'))
r2sbl$scen = 'Baseline'

# Different samples
r2s_samp = filter(r2s,racedef == 'expansive', mod == 'Baseline',
                    incsamp %in% c('gq_hh_faminc_sqrt','emp_emp_inctot','cit_usborn_faminc_sqrt','mig_samest_faminc_sqrt','mig_difst_faminc_sqrt'), 
                    outvar %in% c('med_ldiff')) # Different measures
r2s_samp$scen = r2s_samp$incsamp

# Different pop weights and MSA
r2s_wts = filter(r2s, racedef == 'expansive', mod %in% c('Consistent MSA sample','No non-MSA observations','No population weights','Full population weights'),incsamp %in% c('all_faminc_sqrt'), outvar %in% c('med_ldiff'))
r2s_wts$scen = r2s_wts$mod


# Different race definition
r2s_rdef = filter(r2s, racedef == 'narrow', mod == 'Baseline', incsamp %in% c('all_faminc_sqrt','cit_usborn_faminc_sqrt'),outvar %in% c('med_ldiff')) # Different race definition
r2s_rdef$scen = paste(r2s_rdef$racedef,r2s_rdef$incsamp,sep = '_')


# Different incomes and measures
r2s_inc = filter(r2s, racedef == 'expansive', mod == 'Baseline',incsamp %in% c('all_faminc_norm','all_faminc_tot','all_hhinc_sqrt','gq_hh_spmuinc_sqrt','gq_hh_spmuinc_disp_sqrt'), 
                   outvar %in% c('med_ldiff')) # Different measures
r2s_inc$scen = r2s_inc$incsamp

# Rank gap
r2s_rank =filter(r2s, racedef == 'expansive', mod == 'Baseline',incsamp %in% c('all_faminc_sqrt'), 
                   outvar %in% c('med_rgap')) # Different measures
r2s_rank$scen = r2s_rank$outvar

# matching 
r2s_match = filter(r2s, racedef == 'expansive', mod == 'Baseline',incsamp %in% c('all_faminc_sqrt_matched_b_age_sex'), 
                     outvar %in% c('med_ldiff')) # Different measures
r2s_match$scen = r2s_match$incsamp


# Different specifications
r2s_mod = filter(r2s, racedef == 'expansive', mod %in% c('Region dummies','HRR excluded','Segregation and incarceration excluded\n(full MSA sample in each year)','Incarceration inequality only','Imprisonment rate') ,incsamp %in% c('all_faminc_sqrt'), outvar %in% c('med_ldiff'))
r2s_mod$scen = r2s_mod$mod

# Differnet segregations
r2s_seg = filter(r2s, racedef == 'expansive', mod %in% c('Isolation index','Exposure index','Theil index','Dissimilarity index (consistent tract boundaries)','Isolation index (consistent tract boundaries)','Theil index (consistent tract boundaries)'),
                   incsamp %in% c('all_faminc_sqrt'), outvar %in% c('med_ldiff'))
r2s_seg$scen = r2s_seg$mod


# Different hrrs
r2s_hrr = filter(r2s,  racedef == 'expansive', mod %in% c('4-component HRR','2-variable state-level HRR','4-variable state-level HRR','Baker HRR','Slavery only','Sharecropping only','Disfranchisement devices only','Southern Manifesto only'), incsamp %in% c('all_faminc_sqrt'), outvar %in% c('med_ldiff'))
r2s_hrr$scen = r2s_hrr$mod

# Different percentiles
r2s_pctiles = filter(r2s, racedef == 'expansive', mod %in% c('Baseline') ,incsamp %in% c('all_faminc_sqrt'), outvar %in% c('p05_ldiff','p10_ldiff','p25_ldiff','p75_ldiff','p90_ldiff','p95_ldiff','mean_ldiff'))
r2s_pctiles$scen = r2s_pctiles$outvar



r2sa = bind_rows(r2sbl,r2s_samp, r2s_wts,r2s_rdef,r2s_inc,r2s_rank,r2s_match,r2s_mod,r2s_seg,r2s_hrr,r2s_pctiles)





# Going to do first alt samples, then alt definitions, then alt out vars
coefsa = mutate(coefsa, 
                # scen = case_when(
                #   mod != 'baseline' ~ mod,
                #   incsamp != 'all_faminc_sqrt' ~ incsamp,
                #   outvar != 'rat_bw_med' ~ outvar,
                #   racedef != 'expansive' ~ racedef,
                #   TRUE ~ 'baseline'
                # ),

               coefclean = case_when(
                 pvalue < .01 ~ paste(round(coef,3),'***',sep = ''),
                 pvalue < .05 & pvalue >= .01 ~ paste(round(coef,3),'**',sep = ''),
                 pvalue < .1 & pvalue >= .05 ~ paste(round(coef,3),'*',sep = ''),
                 pvalue >= .05 ~ paste(round(coef,3),'',sep = '')
                 ),
               
               lowconf = (coef - se * 1.96) %>% round(digits = 3) %>% as.character(),
               hiconf = (coef + se * 1.96) %>% round(digits = 3) %>% as.character(),
               confinf = paste0('(',lowconf,', ',hiconf,')'),

              se = as.character(round(se,3)),
              pvalue = as.character(round(pvalue,3)))

# rsall = mutate(rsall, 
#                 scen = case_when(
#                   mod != 'baseline' ~ mod,
#                   incsamp != 'all_faminc_sqrt' ~ incsamp,
#                   outvar != 'rat_bw_med' ~ outvar,
#                   racedef != 'expansive' ~ racedef,
#                   TRUE ~ 'baseline'
#                 )
#              )


print(unique(coefsa$scen))
print(unique(r2sa$scen))

scens = c('Baseline', # Definitions
          'gq_hh_faminc_sqrt','cit_usborn_faminc_sqrt','mig_samest_faminc_sqrt','mig_difst_faminc_sqrt','emp_emp_inctot',
          "Consistent MSA sample","No non-MSA observations",
          'No population weights','Full population weights',
          'narrow_all_faminc_sqrt',
          'all_hhinc_sqrt','all_faminc_norm','all_faminc_tot','gq_hh_spmuinc_sqrt','gq_hh_spmuinc_disp_sqrt',
          'med_rgap',
          'all_faminc_sqrt_matched_b_age_sex',
          'Isolation index','Theil index','Exposure index','Dissimilarity index (consistent tract boundaries)','Isolation index (consistent tract boundaries)','Theil index (consistent tract boundaries)',
          'Region dummies','HRR excluded','Segregation and incarceration excluded\n(full MSA sample in each year)','Incarceration inequality only','Incarceration difference','Imprisonment rate',
          '4-component HRR','2-variable state-level HRR','4-variable state-level HRR','Baker HRR','Slavery only','Sharecropping only','Disfranchisement devices only','Southern Manifesto only',
          'p05_ldiff','p10_ldiff','p25_ldiff','p75_ldiff','p90_ldiff','p95_ldiff','mean_ldiff'
          
)
# scennames = c('Baseline','Household income, sqrt-normalized','Family income, size-normalized','Total family income (not normalized)',
#               'BW mean income ratio','Difference in logged median income','Rank gap',
#               'Narrow definition of "Black"','Excluding group quarters','US-born citizens','Adults over 18','Adults over 25','Employed persons (individual income)','MSAs where segregation is measured in 1969','South only','White pop. weights','Total pop. weights',
#               '2','3','4','5','6','Spatial sample','Spatial error model')
coefsa$cleanscen = factor(coefsa$scen, levels = scens)
# coefsa$scen = factor(coefsa$scen, levels = scens)
r2sa$cleanscen = factor(r2sa$scen, levels = scens)
# rsa$scen = factor(rsa$scen, levels = scens)

coefsa = coefsa %>% arrange(year,cleanscen,var)
r2sa = r2sa %>% arrange(year,cleanscen)


# 3 tables: alt definitions and out vars, alt samples, alt models

# 1. alternative income definitions 

# First table - definitions and outcome variables
scenst = c('Baseline', # Definitions
           'all_hhinc_sqrt','all_faminc_norm','all_faminc_tot','gq_hh_spmuinc_sqrt','gq_hh_spmuinc_disp_sqrt','med_rgap'
)

# Get coeffiencts and format
cao = filter(coefsa, scen %in% scenst) %>% arrange(scen)

caol = cao %>% select(year,coefclean,confinf,cleanscen,var) %>% pivot_longer(c(coefclean,confinf))
# caol$value = round(caol$value,3)
caow = caol %>% pivot_wider(id_cols = c(year,var), names_from = c(name,cleanscen), values_from = c(value))

# Get R2s and format
rao = filter(r2sa, scen %in% scenst)
rao = pivot_longer(rao, c(r2,nobs),names_to = 'var') #,values_to = 'r2')

rao$value = as.character(round(rao$value,3))

raow = rao %>% pivot_wider(id_cols = c(year,var),names_from = c(cleanscen),values_from = value,names_prefix = 'coefclean_')

at1 = bind_rows(caow,raow) 
at1 = at1 %>% replace(is.na(.),'-')

# Make clean names
at1$Variable = factor(at1$var, levels = fullvars, labels = cleanvars)
at1 = at1 %>% select(year,Variable,everything(),-var) %>%  arrange(year,Variable)
write_csv(at1, paste('output/regressions/tables/reg_table_appendix_incdefs.csv',sep = ''))


# 2. Different populations

# First table - definitions and outcome variables
scenst = c('Baseline', # Definitions
           'gq_hh_faminc_sqrt','emp_emp_inctot','cit_usborn_faminc_sqrt','mig_samest_faminc_sqrt','mig_difst_faminc_sqrt','narrow_all_faminc_sqrt'
)

# Get coeffiencts and format
cao = filter(coefsa, scen %in% scenst)

caol = cao %>% select(year,coefclean,confinf,cleanscen,var) %>% pivot_longer(c(coefclean,confinf))
# caol$value = round(caol$value,3)
caow = caol %>% pivot_wider(id_cols = c(year,var), names_from = c(name,cleanscen), values_from = c(value))

# Get R2s and format
rao = filter(r2sa, scen %in% scenst)
rao = pivot_longer(rao, c(r2,nobs),names_to = 'var') #,values_to = 'r2')

rao$value = as.character(round(rao$value,3))

raow = rao %>% pivot_wider(id_cols = c(year,var),names_from = c(cleanscen),values_from = value,names_prefix = 'coefclean_')

at1 = bind_rows(caow,raow) 
at1 = at1 %>% replace(is.na(.),'-')

# Make clean names
at1$Variable = factor(at1$var, levels = fullvars, labels = cleanvars)
at1 = at1 %>% select(year,Variable,everything(),-var) %>%  arrange(year,Variable)
write_csv(at1, paste('output/regressions/tables/reg_table_appendix_pops.csv',sep = ''))


# 3. Different MSA samples


# First table - definitions and outcome variables
scenst = c('Baseline', # Definitions
           'all_faminc_sqrt_matched_b_age_sex','Consistent MSA sample','No non-MSA observations','No population weights','Full population weights'
           )

# Get coeffiencts and format
cao = filter(coefsa, scen %in% scenst)

caol = cao %>% select(year,coefclean,confinf,cleanscen,var) %>% pivot_longer(c(coefclean,confinf))
# caol$value = round(caol$value,3)
caow = caol %>% pivot_wider(id_cols = c(year,var), names_from = c(name,cleanscen), values_from = c(value))

# Get R2s and format
rao = filter(r2sa, scen %in% scenst)
rao = pivot_longer(rao, c(r2,nobs),names_to = 'var') #,values_to = 'r2')

rao$value = as.character(round(rao$value,3))

raow = rao %>% pivot_wider(id_cols = c(year,var),names_from = c(cleanscen),values_from = value,names_prefix = 'coefclean_')

at1 = bind_rows(caow,raow) 
at1 = at1 %>% replace(is.na(.),'-')

# Make clean names
at1$Variable = factor(at1$var, levels = fullvars, labels = cleanvars)
at1 = at1 %>% select(year,Variable,everything(),-var) %>%  arrange(year,Variable)
write_csv(at1, paste('output/regressions/tables/reg_table_appendix_msasamps.csv',sep = ''))



# 4. Different models

# First table - definitions and outcome variables
scenst = c('Baseline', # Definitions
           'Region dummies','HRR excluded','Segregation and incarceration excluded\n(full MSA sample in each year)','Incarceration inequality only','Incarceration difference','Imprisonment rate'
           )

# Get coeffiencts and format
cao = filter(coefsa, scen %in% scenst)

caol = cao %>% select(year,coefclean,confinf,cleanscen,var) %>% pivot_longer(c(coefclean,confinf))
# caol$value = round(caol$value,3)
caow = caol %>% pivot_wider(id_cols = c(year,var), names_from = c(name,cleanscen), values_from = c(value))

# Get R2s and format
rao = filter(r2sa, scen %in% scenst)
rao = pivot_longer(rao, c(r2,nobs),names_to = 'var') #,values_to = 'r2')

rao$value = as.character(round(rao$value,3))

raow = rao %>% pivot_wider(id_cols = c(year,var),names_from = c(cleanscen),values_from = value,names_prefix = 'coefclean_')

at1 = bind_rows(caow,raow) 
at1 = at1 %>% replace(is.na(.),'-')

# Make clean names
at1$Variable = factor(at1$var, levels = fullvars, labels = cleanvars)
at1 = at1 %>% select(year,Variable,everything(),-var) %>%  arrange(year,Variable)
write_csv(at1, paste('output/regressions/tables/reg_table_appendix_mods.csv',sep = ''))



# 5. Segregation

# First table - definitions and outcome variables
scenst = c('Baseline', # Definitions
           'Isolation index','Exposure index','Theil index','Dissimilarity index (consistent tract boundaries)','Isolation index (consistent tract boundaries)','Theil index (consistent tract boundaries)'
           )

# Get coeffiencts and format
cao = filter(coefsa, scen %in% scenst)

caol = cao %>% select(year,coefclean,confinf,cleanscen,var) %>% pivot_longer(c(coefclean,confinf))
# caol$value = round(caol$value,3)
caow = caol %>% pivot_wider(id_cols = c(year,var), names_from = c(name,cleanscen), values_from = c(value))

# Get R2s and format
rao = filter(r2sa, scen %in% scenst)
rao = pivot_longer(rao, c(r2,nobs),names_to = 'var') #,values_to = 'r2')

rao$value = as.character(round(rao$value,3))

raow = rao %>% pivot_wider(id_cols = c(year,var),names_from = c(cleanscen),values_from = value,names_prefix = 'coefclean_')

at1 = bind_rows(caow,raow) 
at1 = at1 %>% replace(is.na(.),'-')

# Make clean names
at1$Variable = factor(at1$var, levels = fullvars, labels = cleanvars)
at1 = at1 %>% select(year,Variable,everything(),-var) %>%  arrange(year,Variable)
write_csv(at1, paste('output/regressions/tables/reg_table_appendix_seg.csv',sep = ''))


# 6. HRR


# First table - definitions and outcome variables
scenst = c('Baseline', # Definitions
           '4-component HRR','4-variable state-level HRR','Baker HRR','2-variable state-level HRR',
           'Slavery only','Sharecropping only','Disfranchisement devices only','Southern Manifesto only'
           
           )

# Get coeffiencts and format
cao = filter(coefsa, scen %in% scenst)

caol = cao %>% select(year,coefclean,confinf,cleanscen,var) %>% pivot_longer(c(coefclean,confinf))
# caol$value = round(caol$value,3)

# Make clean names
caol$var = case_when(caol$var %in% c('hrrvalid','hrrv',
                                     'hrrbaker','hrrbaker:hrrvalid','hrrinvalid','hrr4','hrr4:hrrvalid','hrr_st:hrrvalid','hrr4_st:hrrvalid') ~ 'hrrv',
                     .default = caol$var)


caow = caol %>% pivot_wider(id_cols = c(year,var), names_from = c(name,cleanscen), values_from = c(value))

# Get R2s and format
rao = filter(r2sa, scen %in% scenst)
rao = pivot_longer(rao, c(r2,nobs),names_to = 'var') #,values_to = 'r2')

rao$value = as.character(round(rao$value,3))

raow = rao %>% pivot_wider(id_cols = c(year,var),names_from = c(cleanscen),values_from = value,names_prefix = 'coefclean_')

at1 = bind_rows(caow,raow) 
at1 = at1 %>% replace(is.na(.),'-')

# Make clean names
at1$Variable = factor(at1$var, levels = fullvars, labels = cleanvars)
at1 = at1 %>% select(year,Variable,everything(),-var) %>%  arrange(year,Variable)
write_csv(at1, paste('output/regressions/tables/reg_table_appendix_hrr.csv',sep = ''))


# 7.  percentiles



# First table - definitions and outcome variables
scenst = c('Baseline', # Definitions
           'mean_ldiff','p10_ldiff','p25_ldiff','p75_ldiff','p90_ldiff'
)

# Get coeffiencts and format
cao = filter(coefsa, scen %in% scenst)

caol = cao %>% select(year,coefclean,confinf,cleanscen,var) %>% pivot_longer(c(coefclean,confinf))
# caol$value = round(caol$value,3)
caow = caol %>% pivot_wider(id_cols = c(year,var), names_from = c(name,cleanscen), values_from = c(value))

# Get R2s and format
rao = filter(r2sa, scen %in% scenst)
rao = pivot_longer(rao, c(r2,nobs),names_to = 'var') #,values_to = 'r2')

rao$value = as.character(round(rao$value,3))

raow = rao %>% pivot_wider(id_cols = c(year,var),names_from = c(cleanscen),values_from = value,names_prefix = 'coefclean_')

at1 = bind_rows(caow,raow) 
at1 = at1 %>% replace(is.na(.),'-')

# Make clean names
at1$Variable = factor(at1$var, levels = fullvars, labels = cleanvars)
at1 = at1 %>% select(year,Variable,everything(),-var) %>%  arrange(year,Variable)
write_csv(at1, paste('output/regressions/tables/reg_table_appendix_pctiles.csv',sep = ''))

