-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02_load and pull data.R
408 lines (310 loc) · 18.9 KB
/
02_load and pull data.R
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#### LOAD DATA ####
### crosswalks
NOLAcrosswalk <- read.csv('inputs/NOLAcrosswalk.csv')
colnames(NOLAcrosswalk) <- c("tract", "nbhd")
NOLAcrosswalk <- NOLAcrosswalk %>%
mutate(tract6 = str_pad(tract,6,pad = "0"))
FIPScrosswalk <- read_csv('inputs/fips_state_county_crosswalk.csv') # aggregating LE
### geographies
tracts.la <- sf::st_read(here("inputs/tl_2010_22_tract10/tl_2010_22_tract10.shp"))
parishes_sf <- tigris::counties(state = "22", class = "sf")
Orleans.water_sf <- tigris::area_water("22", "Orleans Parish", class = "sf")
Jefferson.water_sf <- tigris::area_water("22", "Jefferson Parish", class = "sf")
StCharles.water_sf <- tigris::area_water("22", "St. Charles Parish", class = "sf")
# Plaquemines.water_sf <- tigris::area_water("22", "Plaquemines Parish", class = "sf")
# StJames.water_sf <- tigris::area_water("22", "St. James Parish", class = "sf")
# StBernard.water_sf <- tigris::area_water("22", "St. Bernard Parish", class = "sf")
# StTammany.water_sf <- tigris::area_water("22", "St. Tammany Parish", class = "sf")
otherwater.simple_sf <- sf::st_read(here("inputs/water/Otherwater_clipped_SimplifyP.shp"))
wetlands.simple_sf <- sf::st_read(here("inputs/water/Wetlands2_Clip_SimplifyPolyg.shp"))
GNO.parishfips <- c("051", "071", "075", "087", "089", "093", "095", "103")
##For Countries line graph
## Figure 1.3 Life expectancy in selected countries, 1950-2019 (source: World Bank)
LECountry <- read.csv("inputs/LE_country.csv")
## Source: World bank
## https://data.worldbank.org/indicator/SP.DYN.LE00.IN
### Aggregated LE & prob death ###
load("inputs/usLE.Rdata")
load("inputs/stateLE.Rdata")
load("inputs/msaLE.Rdata")
load("inputs/parishLE.Rdata")
load("inputs/msaProbDeath.Rdata")
load("inputs/usProbDeathagg.Rdata")
### Chapter 1 mortality
###For Deaths of Despair graphics
#Figure 1.4 Deaths per 100,000 from external causes (Source: CDC Wonder)
US_Liverraw <- read.csv("inputs/Mortality/DD/US_Liver.txt", sep="\t")
US_ODraw <- read.csv("inputs/Mortality/DD/US_OD.txt", sep="\t")
US_Suicideraw <- read.csv("inputs/Mortality/DD/US_Suicide.txt", sep="\t")
Metro_Liverraw <- read.csv("inputs/Mortality/DD/Metro_Liver.txt", sep="\t")
Metro_ODraw <- read.csv("inputs/Mortality/DD/Metro_OD.txt", sep="\t")
Metro_Suicideraw <- read.csv("inputs/Mortality/DD/Metro_Suicide.txt", sep="\t")
#Figure 1.5: Deaths per 100,000 from chronic diseases (Source: CDC Wonder)
Metro_Cancerraw <- read.csv("inputs/Mortality/DD/Metro_Cancer.txt", sep="\t")
Metro_Heartraw <- read.csv("inputs/Mortality/DD/Metro_Heart.txt", sep="\t")
Metro_HIVraw <- read.csv("inputs/Mortality/DD/Metro_HIV.txt", sep="\t")
US_Cancerraw <- read.csv("inputs/Mortality/DD/US_Cancer.txt", sep="\t")
US_Heartraw <- read.csv("inputs/Mortality/DD/US_Heart.txt", sep="\t")
US_HIVraw <- read.csv("inputs/Mortality/DD/US_HIV.txt", sep="\t")
##Source: CDC Wonder
## Figure1.7:Middleagemortalityratesper100,000(source:CDCWonder)
## Figure 1.8:Mortality trends for youth and elderly per 100,000 (source: CDCWonder)
Metro_Totalraw <- read.csv("inputs/Mortality/Death_Rates/Metro_Total.txt", sep="\t")
US_Totalraw <- read.csv("inputs/Mortality/Death_Rates/US_Total.txt", sep="\t")
## Figure1.10:Blackandwhitemortalityratesper100,000,NewOrleansmetro (source:CDCWonder)
Metro_Blackraw <- read.csv("inputs/Mortality/Death_Rates/Metro_Black.txt", sep="\t")
Metro_Whiteraw <- read.csv("inputs/Mortality/Death_Rates/Metro_White.txt", sep="\t")
## Figure1.11:Blackandwhitemortalityratesper100,000,UnitedStates(source: CDCWonder)
US_Blackraw <- read.csv("inputs/Mortality/Death_Rates/US_Black.txt", sep="\t")
US_Whiteraw <- read.csv("inputs/Mortality/Death_Rates/US_White.txt", sep="\t")
## Figure 1.9 decline in all0cause, age-adjusted mortality rates per 100,000 (Source: CDC Wonder)
OrleansMortraw <- read.csv("inputs/Mortality/MortalityOrleans.txt", sep="\t")
USMortraw <- read.csv("inputs/Mortality/MortalityUS.txt", sep="\t")
MetroMortraw <- read.csv("inputs/Mortality/MortalityMetro.txt", sep="\t")
AllgeoMortraw <- read.csv("inputs/Mortality/MortalityMetrro.txt", sep="\t") #This one does not include Orleans
### Chapter 2 historical census
black1970_2010 <- read.dbf("inputs/Sampson/popblack1970_2010.dbf")
NOLAcrosswalk<- read.csv("inputs/Crosswalk2010full.csv") %>%
mutate(GEOID = as.character(GEOID)) %>%
mutate(Neighborhood = as.character(Neighborhood))
pov1970_2010 <- read.dbf("inputs/Sampson/poppov1970_2010.dbf")
#Income segregation
incomebuckets1970raw <- read.csv("inputs/Sampson/incomebuckets1970.csv")
incomebuckets1980raw <- read.csv("inputs/Sampson/incomebuckets1980.csv")
incomebuckets1990raw <- read.csv("inputs/Sampson/incomebuckets1990.csv")
incomebuckets2000raw <- read.csv("inputs/Sampson/incomebuckets2000.csv")
incomebuckets2010raw <- read.csv("inputs/Sampson/incomebuckets2010.csv")
###
#### PULL DATA ####
###
#### USALEEP data ####
## download using this link and store locally: https://ftp.cdc.gov/pub/Health_Statistics/NCHS/Datasets/NVSS/USALEEP/CSV/US_B.CSV
#LEusbRaw <- ## your local USALEEP file ##
LEusb <- LEusbRaw %>%
rename(ageGrp = Age.Group,
probDeath = nq.x.,
numSurv = l.x.,
numDying = nd.x.,
personYrs = nL.x.,
personYrsAbove = T.x.,
LE_x = e.x.,
LE_SE_probDeath = se.nq.x..,
LE_SE_x = se.e.x..,
GEOID = Tract.ID) %>%
dplyr::select(-contains('2KX'))
LEusbLA <- LEusb %>%
filter(str_sub(GEOID,1,2)=="22")
#### Opportunity Atlas data ####
temp <- tempfile()
download.file("https://opportunityinsights.org/wp-content/uploads/2018/10/tract_outcomes.zip",temp)
unzip(temp, exdir = paste0(getwd(),"/inputs/opinsights"))
unlink(temp)
OpAtlas.tract_outcomes <- read_csv("inputs/opinsights/tract_outcomes_simple.csv",
col_types = cols(county = col_character(),
cz = col_character(), state = col_character(),
tract = col_character())) %>%
mutate(state = str_pad(state,2, pad="0"),
county = str_pad(county,3, pad= "0"),
tract = str_pad(tract,6, pad="0"),
GEOID = paste0(state, county, tract))
OpAtlas.tract_covariates <- read_csv("inputs/opinsights/tract_outcomes_simple.csv",
col_types = cols(county = col_character(),
cz = col_character(), state = col_character(),
tract = col_character())) %>%
mutate(state = str_pad(state,2, pad="0"),
county = str_pad(county,3, pad= "0"),
tract = str_pad(tract,6, pad="0"),
GEOID = paste0(state, county, tract),
FIPS_5 = as.numeric(paste0(state, county)))
#### acs ####
# us <- unique(fips_codes$state)[1:51]
# acs15 <- map_df(us, function(x) {
# get_acs(geography = "tract",
# year = 2015,
# variables = c("B03002_001", "B03002_004"), # tot pop and Black non-Hispanic pop
# state = x,
# survey = "acs5")
# })
#
# acs <- acs15 %>%
# dplyr::select(-NAME, -moe) %>%
# spread(key = variable, value = estimate) %>%
# transmute(GEOID=GEOID, ACS.pctBlack = B03002_004/B03002_001) %>%
# mutate(ACS.majBlack = ifelse(ACS.pctBlack >= .5,TRUE, FALSE)) # is the tract 50%+ Black?
# save(acs,file = "inputs/acs.Rdata")
load("inputs/acs.Rdata")
# acs15.NOmsa <- get_acs(geography = "tract",
# year = 2015,
# variables = c("B03002_001", "B03002_004", "B19013_001E"), # tot pop, Black non-Hispanic pop
# state = "LA",
# survey = "acs5")
#
# acs15.NOmsa.dp <- get_acs(geography = "tract",
# year = 2015,
# variables = c( "DP02_0058E", bach = "DP02_0064E", gradProf = "DP02_0065E"), # ed data
# state = "LA",
# survey = "acs5")
#
# acs.NOmsa <- acs15.NOmsa %>%
# bind_rows(acs15.NOmsa.dp) %>%
# dplyr::select(-NAME, -moe) %>%
# filter(str_sub(GEOID,3,5) %in% GNO.parishfips) %>%
# spread(key = variable, value = estimate) %>%
# transmute(GEOID=GEOID, ACS.pctBlack = B03002_004/B03002_001, ACS.medInc = B19013_001, ACS.pctBachPlus = (DP02_0064 + DP02_0065)/DP02_0058)
#
# save(acs.NOmsa,file = "inputs/acsNOmsa.Rdata")
load("inputs/acsNOmsa.Rdata")
#### MSAs ####
## this table comes originally from the Census at this site: https://www.census.gov/geographies/reference-files/time-series/demo/metro-micro/delineation-files.html
## Sept 2018 was the most recent available
## due to formatting, the file has to be downloaded and cleaned up - that's why it's stored in inputs
MSAlist <- read_csv("inputs/MSA list.csv")
MSAlist$FIPS_5 <- as.numeric(MSAlist$FIPS_5)
names(MSAlist) <- c("city", "metroORmicro", "county", "stateName", "stateCode", "countyCode", "centralORoutlying", "FIPS_5")
#### building blocks ####
## tract & LE
geoLE <- LEusb %>%
filter(ageGrp == "Under 1") %>%
dplyr::select(GEOID, LE_x, LE_SE_x) %>%
rename(LE = LE_x, LE_se = LE_SE_x)
# tract & LE & MSA
geoLEMSA <- geoLE %>%
mutate(FIPS_5 = as.numeric(substr(GEOID,1,5))) %>%
left_join(.,MSAlist, by = c("FIPS_5")) %>%
dplyr::select(GEOID, FIPS_5, LE, LE_se, city, metroORmicro)
#### smooshing ####
alldata <- LEusb %>%
mutate(FIPS_5 = as.numeric(substr(GEOID,1,5))) %>%
left_join(.,MSAlist, by = c("FIPS_5")) %>%
left_join(., OpAtlas.tract_covariates, by=c("GEOID")) %>%
left_join(., OpAtlas.tract_outcomes_simple, by=c("GEOID")) %>%
left_join(., acs, by=c("GEOID"))
#### AGGREGATING LE ####
### LA
popLA10 <- get_decennial(geography = "tract",
variables = c("P001001"),
state = c("LA"))
popLA15 <- get_acs(geography = "tract",
year = 2015,
variables = c("B01003_001"),
state = c("LA"),
survey = "acs5")
popLA <- left_join(popLA10, popLA15, by = "GEOID") %>%
rename(pop10 = value, pop15 = estimate) %>%
dplyr::select(-NAME.x, -NAME.y, -variable.x, -variable.y) %>%
mutate(popPooled = pop10 + pop15)
### US
us <- unique(fips_codes$state)[1:51]
pop10 <- map_df(us, function(x) {
get_decennial(geography = "tract", variables = "P001001",
state = x)
})
pop15 <- map_df(us, function(x) {
get_acs(geography = "tract",
year = 2015,
variables = "B01003_001",
state = x,
survey = "acs5")
})
metropop15 <- get_acs(geography = "metropolitan statistical area/micropolitan statistical area",
year = 2015,
variables = "B01003_001",
survey = "acs5") %>%
filter(grepl("Metro Area", NAME)) %>%
filter(estimate > 1000000) %>%
mutate(NAME = str_sub(NAME, 1,-12))
#### PROBABLILITY OF DEATH ####
### Population data
age2000.vars <-c("P001001","P012003","P012004","P012005","P012006","P012007","P012008","P012009","P012010","P012011","P012012","P012013","P012014","P012015","P012016","P012017","P012018","P012019","P012020","P012021","P012022","P012023","P012024","P012025","P012027","P012028","P012029","P012030","P012031","P012032","P012033","P012034","P012035","P012036","P012037","P012038","P012039","P012040","P012041","P012042","P012043","P012044","P012045","P012046","P012047","P012048","P012049")
age2000.names <-c("pop","m_under5","m5_9","m10_14","m15_17","m18_19","m20","m21","m22_24","m25_29","m30_34","m35_39","m40_44","m45_49","m50_54","m55_59","m60_61","m62_64","m65_66","m67_69","m70_74","m75_79","m80_84","m85over","f_under5","f5_9","f10_14","f15_17","f18_19","f20","f21","f22_24","f25_29","f30_34","f35_39","f40_44","f45_49","f50_54","f55_59","f60_61","f62_64","f65_66","f67_69","f70_74","f75_79","f80_84","f85over")
popLA10Raw <- get_decennial(geography = "tract",
variables = age2000.vars,
state = c("LA"))
age.vars <-c("B01003_001E", "B01003_001M","B01001_003E","B01001_004E","B01001_005E","B01001_006E","B01001_007E","B01001_008E","B01001_009E","B01001_010E","B01001_011E","B01001_012E","B01001_013E","B01001_014E","B01001_015E","B01001_016E","B01001_017E","B01001_018E","B01001_019E","B01001_020E","B01001_021E","B01001_022E","B01001_023E","B01001_024E","B01001_025E","B01001_027E","B01001_028E","B01001_029E","B01001_030E","B01001_031E","B01001_032E","B01001_033E","B01001_034E","B01001_035E","B01001_036E","B01001_037E","B01001_038E","B01001_039E","B01001_040E","B01001_041E","B01001_042E","B01001_043E","B01001_044E","B01001_045E","B01001_046E","B01001_047E","B01001_048E","B01001_049E","B01001_003M","B01001_004M","B01001_005M","B01001_006M","B01001_007M","B01001_008M","B01001_009M","B01001_010M","B01001_011M","B01001_012M","B01001_013M","B01001_014M","B01001_015M","B01001_016M","B01001_017M","B01001_018M","B01001_019M","B01001_020M","B01001_021M","B01001_022M","B01001_023M","B01001_024M","B01001_025M","B01001_027M","B01001_028M","B01001_029M","B01001_030M","B01001_031M","B01001_032M","B01001_033M","B01001_034M","B01001_035M","B01001_036M","B01001_037M","B01001_038M","B01001_039M","B01001_040M","B01001_041M","B01001_042M","B01001_043M","B01001_044M","B01001_045M","B01001_046M","B01001_047M","B01001_048M","B01001_049M")
age.names <-c("pop", "popMOE", "m_under5","m5_9","m10_14","m15_17","m18_19","m20","m21","m22_24","m25_29","m30_34","m35_39","m40_44","m45_49","m50_54","m55_59","m60_61","m62_64","m65_66","m67_69","m70_74","m75_79","m80_84","m85over","f_under5","f5_9","f10_14","f15_17","f18_19","f20","f21","f22_24","f25_29","f30_34","f35_39","f40_44","f45_49","f50_54","f55_59","f60_61","f62_64","f65_66","f67_69","f70_74","f75_79","f80_84","f85over","m_under5MOE","m5_9MOE","m10_14MOE","m15_17MOE","m18_19MOE","m20MOE","m21MOE","m22_24MOE","m25_29MOE","m30_34MOE","m35_39MOE","m40_44MOE","m45_49MOE","m50_54MOE","m55_59MOE","m60_61MOE","m62_64MOE","m65_66MOE","m67_69MOE","m70_74MOE","m75_79MOE","m80_84MOE","m85overMOE","f_under5MOE","f5_9MOE","f10_14MOE","f15_17MOE","f18_19MOE","f20MOE","f21MOE","f22_24MOE","f25_29MOE","f30_34MOE","f35_39MOE","f40_44MOE","f45_49MOE","f50_54MOE","f55_59MOE","f60_61MOE","f62_64MOE","f65_66MOE","f67_69MOE","f70_74MOE","f75_79MOE","f80_84MOE","f85overMOE")
age.names.short <-c("m_under5","m5_9","m10_14","m15_17","m18_19","m20","m21","m22_24","m25_29","m30_34","m35_39","m40_44","m45_49","m50_54","m55_59","m60_61","m62_64","m65_66","m67_69","m70_74","m75_79","m80_84","m85over","f_under5","f5_9","f10_14","f15_17","f18_19","f20","f21","f22_24","f25_29","f30_34","f35_39","f40_44","f45_49","f50_54","f55_59","f60_61","f62_64","f65_66","f67_69","f70_74","f75_79","f80_84","f85over", "pop")
popLA15Raw <- get_acs(geography = "tract",
year = 2015,
variables = c(age.vars),
state = c("LA"),
survey = "acs5")
### Pulling 2017 black population for Chapter 2
np.pull <- function(variables, names = variables, year=2017, survey = "acs/acs5"){
censuskey="530ce361defc2c476e5b5d5626d224d8354b9b9a"
tract <- getCensus(name = survey,
vintage = year,
key = censuskey,
vars = variables,
region = "tract:*",
regionin = "state:22+county:051,071,075,087,089,093,095,103")
#select(-state, -county)
colnames(tract) <- c("state", "county", "tract", names)
return(tract)
}
##pulling black 2017
black2017.vars <- c("B03002_001E", "B03002_004E")
black2017.names <- c("pop2017", "black2017")
black2017 <- np.pull(variables = black2017.vars, names = black2017.names) %>%
mutate(GEOID = paste(state, county, tract, sep= "")) %>%
mutate(GEOID = as.character(GEOID)) %>%
select(GEOID, pop2017, black2017)
pov.vars <- c("B17001_001E","B17001_002E")
pov.names <- c("pop2017","pov2017")
pov2017 <- np.pull(variables = pov.vars, names = pov.names)%>%
mutate(GEOID = paste(state, county, tract, sep= "")) %>%
mutate(GEOID = as.character(GEOID)) %>%
select(GEOID, pop2017, pov2017)
incDist.vars <- c("B19001_001E","B19001_002E","B19001_003E","B19001_004E","B19001_005E","B19001_006E","B19001_007E","B19001_008E","B19001_009E","B19001_010E","B19001_011E","B19001_012E","B19001_013E","B19001_014E","B19001_015E",
"B19001_016E","B19001_017E")
incDist.names <- c("totfams","inc_less10","inc10_15","inc15_20","inc20_25","inc25_30","inc30_35","inc35_40","inc40_45","inc45_50","inc50_60","inc60_75","inc75_100","inc100_125","inc125_150","inc150_200","inc_200PLUS")
incomebuckets2017raw <- np.pull(variables = incDist.vars, names = incDist.names)
### Save to Rdata
#
# save(list = c('LEusbRaw',
# 'LEusb',
# 'LEusbLA',
# 'OpAtlas.tract_outcomes_simple',
# 'OpAtlas.tract_covariates',
# 'MSAlist',
# 'geoLE',
# 'geoLEMSA',
# 'alldata',
# 'black2017',
# 'pov2017',
# 'incomebuckets2017raw'), file ="dataPull_data.Rdata")
#### Ch 3 additional Opportunity Atlas data
# Full tract outcomes for GNO MSA
temp <- tempfile()
download.file("https://opportunityinsights.org/wp-content/uploads/2018/10/tract_outcomes.zip",temp)
unzip(temp, exdir = paste0(getwd(),"/inputs/opinsights"))
unlink(temp)
OpAtlas.tract_outcomes_full <- read_csv("inputs/opinsights/tract_outcomes_early.csv",
col_types = cols(county = col_character(),
cz = col_character(), state = col_character(),
tract = col_character())) %>%
mutate(state = str_pad(state,2, pad="0"),
county = str_pad(county,3, pad= "0"),
tract = str_pad(tract,6, pad="0"),
GEOID = paste0(state, county, tract))
OpAtlas.tract_outcomes_full.GNO <- OpAtlas.tract_outcomes_full %>%
filter(state == "22") %>%
filter(county %in% c("051", "071", "075", "087", "089", "093", "095", "103"))
rm(OpAtlas.tract_outcomes_full)
# Income crosswalk
Op.atlas.pctinccrosswalk <- read_csv("https://opportunityinsights.org/wp-content/uploads/2018/10/pctile_to_dollar_cw.csv")
# Communiting zone outcomes
OpAtlas.CZ_outcomes_simple <- read_csv("https://opportunityinsights.org/wp-content/uploads/2018/10/cz_outcomes_simple.csv")
OpAtlas.CZ_outcomes_full <- read_csv("https://opportunityinsights.org/wp-content/uploads/2018/10/cz_outcomes.csv")
# Combine tract OpAtlas with life expectancy.
OpAtlas.tract_outcomes_simple_LE.GNO <- OpAtlas.tract_outcomes_simple %>%
filter(state == "22") %>%
filter(county %in% c("051", "071", "075", "087", "089", "093", "095", "103")) %>%
#full_join(LE.GNO, by=c("GEOID"="GEOID"))
full_join(LE.GNO, by=c("GEOID"="GEOID"))
## Save to Rdata
#
# save(list = c('OpAtlas.tract_outcomes_simple_LE.GNO',
# 'Op.atlas.pctinccrosswalk',
# 'OpAtlas.tract_outcomes_full.GNO',
# 'OpAtlas.CZ_outcomes_simple',
# 'OpAtlas.CZ_outcomes_full'), file = "Ch3OpAtlas_data.Rdata")