File size: 8,247 Bytes
204f58d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
{
  library(tidyverse)
  library(haven)
  library(glue)
  library(jtools)
  library(lubridate)
  library(huxtable)
  library(multcomp)
  library(lfe)
}

# Data merging code copied in from kavanagh_g66z_data_merge.R

# load input files

# 5% Sample
set.seed(2982)
county_variables <- read_csv('replication_data/county_variables.csv') %>%
  sample_frac(.05)
transportation <- read_csv('replication_data/transportation.csv')

# changes in distancing
flat_data <- transportation %>%
  mutate(prop_home = pop_home/(pop_home + pop_not_home),
         # Define the three time periods
         time_period = case_when(
    between(date, ymd('2020-02-16'),ymd('2020-02-29')) ~ 'AAA Reference',
    between(date, ymd('2020-03-19'),ymd('2020-04-01')) ~ 'March',
    between(date, ymd('2020-08-16'),ymd('2020-08-29')) ~ 'August')
  ) %>%
  filter(!is.na(time_period), !is.na(pop_home)) %>%
  group_by(time_period, fips, state) %>%
  # Average over county, time period
  summarize(prop_home = mean(prop_home, na.rm = TRUE)) %>%
  arrange(state, fips, time_period) %>%
  group_by(fips, state) %>%
  # Scale to 100
  mutate(prop_home_change = 100*(prop_home/first(prop_home) - 1)) %>%
  filter(time_period != 'AAA Reference') %>%
  # Reshape to get one variable for March and one for August
  pivot_wider(id_cols = c('fips','state'),
              names_from = 'time_period',
              values_from = c('prop_home','prop_home_change')) %>%
  # Bring in county-level data
  right_join(county_variables, by = 'fips')




# IQR of Trump support
trumpIQR <- county_variables %>%
  dplyr::select(fips, trump_share) %>%
  unique() %>%
  pull(trump_share) %>%
  quantile(c(.25, .75), na.rm = TRUE) %>%
  {.[2] - .[1]} %>%
  unname()

# Variable construction
flat_data <- flat_data %>%
  mutate(state = factor(state)) %>%
  dplyr::select(prop_home_change_March,
         prop_home_change_August,
         income_per_capita,
         trump_share,
         male_percent,
         percent_black,
         percent_hispanic,
         percent_college,
         percent_retail,
         percent_transportation,
         percent_hes,
         prop_rural,
         ten_nineteen,
         twenty_twentynine,
         thirty_thirtynine,
         forty_fortynine,
         fifty_fiftynine,
         sixty_sixtynine,
         seventy_seventynine,
         over_eighty,
         state,
         fips) %>%
  ungroup() %>%
  # These are stored as 0-1 but everything else is 0-100
  mutate(across(starts_with('percent_'),function(x) x*100)) %>%
  mutate(male_percent = male_percent*100,
         percent_college = percent_college/100) %>%
  mutate(income_per_capita = income_per_capita/1000)


# Create regression formulae
formula_maker <- function(depvar, data) {
  vnames <- data %>%
    dplyr::select(-fips, -prop_home_change_March, -prop_home_change_August, -state) %>%
    names()
  
  form <- paste0(depvar,'~',
                 paste(vnames, collapse ='+'),
                 ' | state')

  return(as.formula(form))  
}

# Run fixed effect regressions
m1 <- felm(formula_maker('prop_home_change_March',flat_data), data = flat_data)
m2 <- felm(formula_maker('prop_home_change_August',flat_data), data = flat_data)

# Regression table
results_tab <- export_summs(m1, m2,
             digits = 3,
             model.names = c('March 19-April 1','August 16-29'),
             coefs = c('Income per Capita (Thousands)' = 'income_per_capita',
                       'Share of Trump Voters' = 'trump_share',
                       'Percent Male' = 'male_percent',
                       'Percent Black' = 'percent_black',
                       'Percent Hispanic' = 'percent_hispanic',
                       'Percent with College Degree' = 'percent_college',
                       'Percent in Retail' = 'percent_retail',
                       'Percent in Transportation' = 'percent_transportation',
                       'Percent in Health / Ed / Soc. Svcs' = 'percent_hes',
                       'Percent Rural' = 'prop_rural',
                       'Percent Age 10-19' = 'ten_nineteen',
                       'Percent Age 20-29' = 'twenty_twentynine',
                       'Percent Age 30-39' = 'thirty_thirtynine',
                       'Percent Age 40-49' = 'forty_fortynine',
                       'Percent Age 50-59' = 'fifty_fiftynine',
                       'Percent Age 60-69' = 'sixty_sixtynine',
                       'Percent Age 70-79' = 'seventy_seventynine',
                       'Percent Age 80+' = 'over_eighty'),
             statistics = c(N = 'nobs',
                            R2 = 'r.squared')) %>%
  add_footnote('More-positive numbers indicate more stay-at-home activity. State fixed effects included.')

quick_html(results_tab, file = 'regression_table.html')

# Effect of a one-IQR change in Trump share
summary(glht(m1, paste0(trumpIQR,'*trump_share = 0')))
summary(glht(m2, paste0(trumpIQR,'*trump_share = 0')))

## Additional analysis: spatial autocorrelation
{
  library(tigris)
  library(spdep)
  library(sphet)
  library(spatialreg)
}

# Get information on central county latitude/longitude
counties <- counties()
counties <- as_tibble(counties[,c('STATEFP','COUNTYFP','INTPTLAT','INTPTLON')]) %>%
  mutate(fips = as.numeric(STATEFP)*1000 + as.numeric(COUNTYFP)) %>%
  dplyr::select(-geometry, -STATEFP, -COUNTYFP) %>%
  rename(lat = INTPTLAT, lon = INTPTLON) %>%
  mutate(lat = as.numeric(lat),
         lon = as.numeric(lon))

# Bring in to data
flat_data <- left_join(flat_data, counties)

# K nearest neighbors for spatial spillovers
kn <- knearneigh(as.matrix(flat_data[,c('lon','lat'), with = FALSE]), 5)
nb <- knn2nb(kn)
listw <- nb2listw(nb)

# Create regression formulae
formula_maker <- function(depvar, data) {
  vnames <- data %>%
    dplyr::select(-fips, -prop_home_change_March, -prop_home_change_August) %>%
    names()
  
  form <- paste0(depvar,'~',
                 paste(vnames, collapse ='+'))
  
  return(as.formula(form))  
}

# Run models with spatial autocorrelation term
m3 <- lagsarlm(formula_maker('prop_home_change_March',flat_data), data = flat_data, listw = listw)
m4 <- lagsarlm(formula_maker('prop_home_change_August',flat_data), data = flat_data, listw = listw)

# Regression table
results_tab <- export_summs(m3, m4,
                            digits = 3,
                            model.names = c('March 19-April 1','August 16-29'),
                            coefs = c('Income per Capita (Thousands)' = 'income_per_capita',
                                      'Share of Trump Voters' = 'trump_share',
                                      'Percent Male' = 'male_percent',
                                      'Percent Black' = 'percent_black',
                                      'Percent Hispanic' = 'percent_hispanic',
                                      'Percent with College Degree' = 'percent_college',
                                      'Percent in Retail' = 'percent_retail',
                                      'Percent in Transportation' = 'percent_transportation',
                                      'Percent in Health / Ed / Soc. Svcs' = 'percent_hes',
                                      'Percent Rural' = 'prop_rural',
                                      'Percent Age 10-19' = 'ten_nineteen',
                                      'Percent Age 20-29' = 'twenty_twentynine',
                                      'Percent Age 30-39' = 'thirty_thirtynine',
                                      'Percent Age 40-49' = 'forty_fortynine',
                                      'Percent Age 50-59' = 'fifty_fiftynine',
                                      'Percent Age 60-69' = 'sixty_sixtynine',
                                      'Percent Age 70-79' = 'seventy_seventynine',
                                      'Percent Age 80+' = 'over_eighty',
                                      'rho' = 'rho'),
                            statistics = c(N = 'nobs',
                                           R2 = 'r.squared')) %>%
  add_footnote('More-positive numbers indicate more stay-at-home activity.\nState fixed effects included.\nSpatial autocorrelation included with 5-nearest-neighbor neighbors.')

quick_html(results_tab, file = 'spatial_regression_table.html')