-
Notifications
You must be signed in to change notification settings - Fork 1
/
duncan_duncan.R
455 lines (439 loc) · 20.1 KB
/
duncan_duncan.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#' Relative Centralization based on Duncan, Cuzzort, & Duncan (1961) and Massey & Denton (1988)
#'
#' Compute the aspatial Relative Centralization (Duncan & Duncan) of a selected racial or ethnic subgroup(s) and U.S. geographies.
#'
#' @param geo_large Character string specifying the larger geographical unit of the data. The default is counties \code{geo_large = 'county'}.
#' @param geo_small Character string specifying the smaller geographical unit of the data. The default is census tracts \code{geo_small = 'tract'}.
#' @param year Numeric. The year to compute the estimate. The default is 2020, and the years 2009 onward are currently available.
#' @param subgroup Character string specifying the racial or ethnic subgroup(s) as the comparison population. See Details for available choices.
#' @param subgroup_ref Character string specifying the racial or ethnic subgroup(s) as the reference population. See Details for available choices.
#' @param crs Numeric or character string specifying the coordinate reference system to compute the distance-based metric. The default is Albers North America \code{crs = 'ESRI:102008'}.
#' @param omit_NAs Logical. If FALSE, will compute index for a larger geographical unit only if all of its smaller geographical units have values. The default is TRUE.
#' @param quiet Logical. If TRUE, will display messages about potential missing census information. The default is FALSE.
#' @param ... Arguments passed to \code{\link[tidycensus]{get_acs}} to select state, county, and other arguments for census characteristics
#'
#' @details This function will compute the aspatial Relative Centralization (\emph{RCE}) of selected racial or ethnic subgroups and U.S. geographies for a specified geographical extent (e.g., the entire U.S. or a single state) based on Duncan & Duncan (1955) \doi{10.1086/221609} and Massey & Denton (1988) \doi{10.1093/sf/67.2.281}. This function provides the computation of \emph{RCE} for any of the U.S. Census Bureau race or ethnicity subgroups (including Hispanic and non-Hispanic individuals).
#'
#' The function uses the \code{\link[tidycensus]{get_acs}} function to obtain U.S. Census Bureau 5-year American Community Survey characteristics used for the computation. The yearly estimates are available for 2009 onward when ACS-5 data are available (2010 onward for \code{geo_large = 'cbsa'} and 2011 onward for \code{geo_large = 'place'}, \code{geo_large = 'csa'}, or \code{geo_large = 'metro'}) but may be available from other U.S. Census Bureau surveys. The twenty racial or ethnic subgroups (U.S. Census Bureau definitions) are:
#' \itemize{
#' \item \strong{B03002_002}: not Hispanic or Latino \code{'NHoL'}
#' \item \strong{B03002_003}: not Hispanic or Latino, white alone \code{'NHoLW'}
#' \item \strong{B03002_004}: not Hispanic or Latino, Black or African American alone \code{'NHoLB'}
#' \item \strong{B03002_005}: not Hispanic or Latino, American Indian and Alaska Native alone \code{'NHoLAIAN'}
#' \item \strong{B03002_006}: not Hispanic or Latino, Asian alone \code{'NHoLA'}
#' \item \strong{B03002_007}: not Hispanic or Latino, Native Hawaiian and Other Pacific Islander alone \code{'NHoLNHOPI'}
#' \item \strong{B03002_008}: not Hispanic or Latino, Some other race alone \code{'NHoLSOR'}
#' \item \strong{B03002_009}: not Hispanic or Latino, Two or more races \code{'NHoLTOMR'}
#' \item \strong{B03002_010}: not Hispanic or Latino, Two races including Some other race \code{'NHoLTRiSOR'}
#' \item \strong{B03002_011}: not Hispanic or Latino, Two races excluding Some other race, and three or more races \code{'NHoLTReSOR'}
#' \item \strong{B03002_012}: Hispanic or Latino \code{'HoL'}
#' \item \strong{B03002_013}: Hispanic or Latino, white alone \code{'HoLW'}
#' \item \strong{B03002_014}: Hispanic or Latino, Black or African American alone \code{'HoLB'}
#' \item \strong{B03002_015}: Hispanic or Latino, American Indian and Alaska Native alone \code{'HoLAIAN'}
#' \item \strong{B03002_016}: Hispanic or Latino, Asian alone \code{'HoLA'}
#' \item \strong{B03002_017}: Hispanic or Latino, Native Hawaiian and Other Pacific Islander alone \code{'HoLNHOPI'}
#' \item \strong{B03002_018}: Hispanic or Latino, Some other race alone \code{'HoLSOR'}
#' \item \strong{B03002_019}: Hispanic or Latino, Two or more races \code{'HoLTOMR'}
#' \item \strong{B03002_020}: Hispanic or Latino, Two races including Some other race \code{'HoLTRiSOR'}
#' \item \strong{B03002_021}: Hispanic or Latino, Two races excluding Some other race, and three or more races \code{'HoLTReSOR'}
#' }
#'
#' Use the internal \code{state} and \code{county} arguments within the \code{\link[tidycensus]{get_acs}} function to specify geographic extent of the data output.
#'
#' \emph{RCE} is a measure of the degree to which racial or ethnic populations within smaller geographical units are located near the center of a larger geographical unit. \emph{RCE} can range in value from -1 to 1 and represents the spatial distribution of racial or ethnic populations within smaller geographical units relative to the compared to the distribution of the referent racial or ethnic population around the center of a larger geographical unit. Positive values indicate a tendency for racial or ethnic populations to reside closer to the center of a larger geographical unit than the referent racial or ethnic population, while negative values indicate the racial or ethnic population is distributed farther from the center of a larger geographical unit than the referent racial or ethnic population. A score of 0 means that racial or ethnic populations have a uniform distribution throughout a larger geographical unit. \emph{RCE} gives the proportion of racial or ethnic populations required to change residence to match the degree of centralization of the referent racial or ethnic population.
#'
#' Larger geographical units available include states \code{geo_large = 'state'}, counties \code{geo_large = 'county'}, census tracts \code{geo_large = 'tract'}, census-designated places \code{geo_large = 'place'}, core-based statistical areas \code{geo_large = 'cbsa'}, combined statistical areas \code{geo_large = 'csa'}, and metropolitan divisions \code{geo_large = 'metro'}. Smaller geographical units available include, counties \code{geo_small = 'county'}, census tracts \code{geo_small = 'tract'}, and census block groups \code{geo_small = 'cbg'}. If a larger geographical unit is comprised of only one smaller geographical unit (e.g., a U.S county contains only one census tract), then the \emph{RCE} value returned is NA. If the larger geographical unit is census-designated places \code{geo_large = 'place'}, core-based statistical areas \code{geo_large = 'cbsa'}, combined statistical areas \code{geo_large = 'csa'}, or metropolitan divisions \code{geo_large = 'metro'}, only the smaller geographical units completely within a larger geographical unit are considered in the \emph{V} computation (see internal \code{\link[sf]{st_within}} function for more information) and recommend specifying all states within which the interested larger geographical unit are located using the internal \code{state} argument to ensure all appropriate smaller geographical units are included in the \emph{RCE} computation.
#'
#' \emph{Important consideration}: The original metric used the location of the central business district (CBD) to compute the metric, but the U.S. Census Bureau has not defined CBDs for U.S. cities since the 1982 Census of Retail Trade. Therefore, this function uses the the centroids of each larger geographical unit as the 'centre', but may not represent the current CBD.
#'
#' @return An object of class 'list'. This is a named list with the following components:
#'
#' \describe{
#' \item{\code{rce}}{An object of class 'tbl' for the GEOID, name, and \emph{RCE} at specified larger census geographies.}
#' \item{\code{rce_data}}{An object of class 'tbl' for the raw census values at specified smaller census geographies.}
#' \item{\code{missing}}{An object of class 'tbl' of the count and proportion of missingness for each census variable used to compute \emph{RCE}.}
#' }
#'
#' @import dplyr
#' @importFrom sf st_centroid st_distance st_drop_geometry st_transform st_within
#' @importFrom stats complete.cases
#' @importFrom stringr str_trim
#' @importFrom tidycensus get_acs
#' @importFrom tidyr pivot_longer separate
#' @importFrom tigris combined_statistical_areas core_based_statistical_areas counties metro_divisions places states tracts
#' @importFrom units drop_units set_units
#' @importFrom utils stack
#' @export
#'
#' @seealso \code{\link[tidycensus]{get_acs}} for additional arguments for geographic extent selection (i.e., \code{state} and \code{county}).
#'
#' @examples
#' \dontrun{
#' # Wrapped in \dontrun{} because these examples require a Census API key.
#'
#' # Relative Centralization (a measure of centralization)
#' ## of non-Hispanic Black vs. non-Hispanic white populations
#' ## in census tracts within counties of Georgia, U.S.A. (2020)
#' duncan_duncan(
#' geo_large = 'county',
#' geo_small = 'tract',
#' state = 'GA',
#' year = 2020,
#' subgroup = 'NHoLB',
#' subgroup_ref = 'NHoLW'
#' )
#'
#' }
#'
duncan_duncan <- function(geo_large = 'county',
geo_small = 'tract',
year = 2020,
subgroup,
subgroup_ref,
crs = 'ESRI:102008',
omit_NAs = TRUE,
quiet = FALSE,
...) {
# Check arguments
match.arg(geo_large, choices = c('state', 'county', 'tract', 'place', 'cbsa', 'csa', 'metro'))
match.arg(geo_small, choices = c('county', 'tract', 'cbg', 'block group'))
stopifnot(is.numeric(year), year >= 2009) # all variables available 2009 onward
match.arg(
subgroup,
several.ok = TRUE,
choices = c(
'NHoL',
'NHoLW',
'NHoLB',
'NHoLAIAN',
'NHoLA',
'NHoLNHOPI',
'NHoLSOR',
'NHoLTOMR',
'NHoLTRiSOR',
'NHoLTReSOR',
'HoL',
'HoLW',
'HoLB',
'HoLAIAN',
'HoLA',
'HoLNHOPI',
'HoLSOR',
'HoLTOMR',
'HoLTRiSOR',
'HoLTReSOR'
)
)
match.arg(
subgroup_ref,
several.ok = TRUE,
choices = c(
'NHoL',
'NHoLW',
'NHoLB',
'NHoLAIAN',
'NHoLA',
'NHoLNHOPI',
'NHoLSOR',
'NHoLTOMR',
'NHoLTRiSOR',
'NHoLTReSOR',
'HoL',
'HoLW',
'HoLB',
'HoLAIAN',
'HoLA',
'HoLNHOPI',
'HoLSOR',
'HoLTOMR',
'HoLTRiSOR',
'HoLTReSOR'
)
)
# Select census variables
vars <- c(
TotalPop = 'B03002_001',
NHoL = 'B03002_002',
NHoLW = 'B03002_003',
NHoLB = 'B03002_004',
NHoLAIAN = 'B03002_005',
NHoLA = 'B03002_006',
NHoLNHOPI = 'B03002_007',
NHoLSOR = 'B03002_008',
NHoLTOMR = 'B03002_009',
NHoLTRiSOR = 'B03002_010',
NHoLTReSOR = 'B03002_011',
HoL = 'B03002_012',
HoLW = 'B03002_013',
HoLB = 'B03002_014',
HoLAIAN = 'B03002_015',
HoLA = 'B03002_016',
HoLNHOPI = 'B03002_017',
HoLSOR = 'B03002_018',
HoLTOMR = 'B03002_019',
HoLTRiSOR = 'B03002_020',
HoLTReSOR = 'B03002_021'
)
selected_vars <- vars[c('TotalPop', subgroup, subgroup_ref)]
out_names <- c(names(selected_vars), 'ALAND') # save for output
in_subgroup <- paste0(subgroup, 'E')
in_subgroup_ref <- paste0(subgroup_ref, 'E')
# Acquire RCE variables and sf geometries
out_dat <- suppressMessages(suppressWarnings(
tidycensus::get_acs(
geography = geo_small,
year = year,
output = 'wide',
variables = selected_vars,
geometry = TRUE,
keep_geo_vars = TRUE,
...
)
))
# Format output
if (geo_small == 'county') {
out_dat <- out_dat %>%
tidyr::separate(NAME.y, into = c('county', 'state'), sep = ',')
}
if (geo_small == 'tract') {
out_dat <- out_dat %>%
tidyr::separate(NAME.y, into = c('tract', 'county', 'state'), sep = ',') %>%
dplyr::mutate(tract = gsub('[^0-9\\.]', '', tract))
}
if (geo_small == 'cbg' | geo_small == 'block group') {
out_dat <- out_dat %>%
tidyr::separate(NAME.y, into = c('cbg', 'tract', 'county', 'state'), sep = ',') %>%
dplyr::mutate(
tract = gsub('[^0-9\\.]', '', tract),
cbg = gsub('[^0-9\\.]', '', cbg)
)
}
# Grouping IDs for RCE computation
if (geo_large == 'state') {
lgeom <- suppressMessages(suppressWarnings(tigris::states(year = year)))
out_dat <- out_dat %>%
dplyr::mutate(
oid = STATEFP,
state = stringr::str_trim(state)
)
}
if (geo_large == 'tract') {
lgeom <- suppressMessages(suppressWarnings(tigris::tracts(
year = year, state = unique(out_dat$state)
)))
out_dat <- out_dat %>%
dplyr::mutate(
oid = paste0(STATEFP, COUNTYFP, TRACTCE),
state = stringr::str_trim(state),
county = stringr::str_trim(county)
)
}
if (geo_large == 'county') {
lgeom <- suppressMessages(suppressWarnings(tigris::counties(
year = year, state = unique(out_dat$state)
)))
out_dat <- out_dat %>%
dplyr::mutate(
oid = paste0(STATEFP, COUNTYFP),
state = stringr::str_trim(state),
county = stringr::str_trim(county)
)
}
if (geo_large == 'place') {
stopifnot(is.numeric(year), year >= 2011) # Places only available 2011 onward
lgeom <- suppressMessages(suppressWarnings(tigris::places(
year = year, state = unique(out_dat$state)
)))
wlgeom <- sf::st_within(out_dat, lgeom)
out_dat <- out_dat %>%
dplyr::mutate(
oid = lapply(wlgeom, function(x) {
tmp <- lgeom[x, 4] %>% sf::st_drop_geometry()
lapply(tmp, function(x) { if (length(x) == 0) NA else x })
}) %>%
unlist(),
place = lapply(wlgeom, function(x) {
tmp <- lgeom[x, 5] %>% sf::st_drop_geometry()
lapply(tmp, function(x) { if (length(x) == 0) NA else x })
}) %>%
unlist()
)
}
if (geo_large == 'cbsa') {
stopifnot(is.numeric(year), year >= 2010) # CBSAs only available 2010 onward
lgeom <- suppressMessages(suppressWarnings(tigris::core_based_statistical_areas(year = year)))
wlgeom <- sf::st_within(out_dat, lgeom)
out_dat <- out_dat %>%
dplyr::mutate(
oid = lapply(wlgeom, function(x) {
tmp <- lgeom[x, 3] %>% sf::st_drop_geometry()
lapply(tmp, function(x) { if (length(x) == 0) NA else x })
}) %>%
unlist(),
cbsa = lapply(wlgeom, function(x) {
tmp <- lgeom[x, 4] %>% sf::st_drop_geometry()
lapply(tmp, function(x) { if (length(x) == 0) NA else x })
}) %>%
unlist()
)
}
if (geo_large == 'csa') {
stopifnot(is.numeric(year), year >= 2011) # CSAs only available 2011 onward
lgeom <- suppressMessages(suppressWarnings(tigris::combined_statistical_areas(year = year)))
wlgeom <- sf::st_within(out_dat, lgeom)
out_dat <- out_dat %>%
dplyr::mutate(
oid = lapply(wlgeom, function(x) {
tmp <- lgeom[x, 2] %>% sf::st_drop_geometry()
lapply(tmp, function(x) { if (length(x) == 0) NA else x })
}) %>%
unlist(),
csa = lapply(wlgeom, function(x) {
tmp <- lgeom[x, 3] %>% sf::st_drop_geometry()
lapply(tmp, function(x) { if (length(x) == 0) NA else x })
}) %>%
unlist()
)
}
if (geo_large == 'metro') {
stopifnot(is.numeric(year), year >= 2011) # Metropolitan Divisions only available 2011 onward
lgeom <- suppressMessages(suppressWarnings(tigris::metro_divisions(year = year)))
wlgeom <- sf::st_within(out_dat, lgeom)
out_dat <- out_dat %>%
dplyr::mutate(
oid = lapply(wlgeom, function(x) {
tmp <- lgeom[x, 4] %>% sf::st_drop_geometry()
lapply(tmp, function(x) { if (length(x) == 0) NA else x })
}) %>%
unlist(),
metro = lapply(wlgeom, function(x) {
tmp <- lgeom[x, 5] %>% sf::st_drop_geometry()
lapply(tmp, function(x) { if (length(x) == 0) NA else x })
}) %>%
unlist()
)
}
# Count of racial or ethnic subgroup populations
## Count of racial or ethnic comparison subgroup population
if (length(in_subgroup) == 1) {
out_dat <- out_dat %>%
dplyr::mutate(subgroup = as.data.frame(.)[, in_subgroup])
} else {
out_dat <- out_dat %>%
dplyr::mutate(subgroup = rowSums(as.data.frame(.)[, in_subgroup]))
}
## Count of racial or ethnic reference subgroup population
if (length(in_subgroup_ref) == 1) {
out_dat <- out_dat %>%
dplyr::mutate(subgroup_ref = as.data.frame(.)[, in_subgroup_ref])
} else {
out_dat <- out_dat %>%
dplyr::mutate(subgroup_ref = rowSums(as.data.frame(.)[, in_subgroup_ref]))
}
# Compute RCE
## From Duncan & Duncan (1955) https://doi.org/10.1086/221609
## RCE = \left ( \sum_{i=2}^{n}X_{i-1}Y_{i} \right ) - \left ( \sum_{i=1}^{n}X_{i}Y_{i-1} \right )
## Where for i smaller geographical units:
## X_{i} is the cumulative proportion of the subgroup population through smaller geographical unit i
## Y_{i} is the cumulative proportion of the referent subgroup population through smaller geographical unit i
## when smaller geographical units are ordered by increasing distance
## from the center of a larger geographical unit
## Compute
out_tmp <- out_dat %>%
.[.$oid != 'NANA', ] %>%
split(., f = list(.$oid)) %>%
lapply(., FUN = rce_fun, lgeom = lgeom, crs = crs, omit_NAs = omit_NAs) %>%
utils::stack(.) %>%
dplyr::mutate(
RCE = values,
oid = ind
) %>%
dplyr::select(RCE, oid) %>%
sf::st_drop_geometry()
# Warning for missingness of census characteristics
missingYN <- out_dat[, c('TotalPopE', in_subgroup, in_subgroup_ref, 'ALAND')] %>%
sf::st_drop_geometry()
names(missingYN) <- out_names
missingYN <- missingYN %>%
tidyr::pivot_longer(
cols = dplyr::everything(),
names_to = 'variable',
values_to = 'val'
) %>%
dplyr::group_by(variable) %>%
dplyr::summarise(
total = dplyr::n(),
n_missing = sum(is.na(val)),
percent_missing = paste0(round(mean(is.na(val)) * 100, 2), ' %')
)
if (quiet == FALSE) {
# Warning for missing census data
if (sum(missingYN$n_missing) > 0) {
message('Warning: Missing census data')
}
}
# Format output
out <- out_dat %>%
sf::st_drop_geometry() %>%
dplyr::left_join(out_tmp, by = dplyr::join_by(oid))
if (geo_large == 'state') {
out <- out %>%
dplyr::select(oid, state, RCE) %>%
unique(.) %>%
dplyr::mutate(GEOID = oid) %>%
dplyr::select(GEOID, state, RCE)
}
if (geo_large == 'county') {
out <- out %>%
dplyr::select(oid, state, county, RCE) %>%
unique(.) %>%
dplyr::mutate(GEOID = oid) %>%
dplyr::select(GEOID, state, county, RCE)
}
if (geo_large == 'tract') {
out <- out %>%
dplyr::select(oid, state, county, tract, RCE) %>%
unique(.) %>%
dplyr::mutate(GEOID = oid) %>%
dplyr::select(GEOID, state, county, tract, RCE)
}
if (geo_large == 'place') {
out <- out %>%
dplyr::select(oid, place, RCE) %>%
unique(.) %>%
dplyr::mutate(GEOID = oid) %>%
dplyr::select(GEOID, place, RCE)
}
if (geo_large == 'cbsa') {
out <- out %>%
dplyr::select(oid, cbsa, RCE) %>%
unique(.) %>%
dplyr::mutate(GEOID = oid) %>%
dplyr::select(GEOID, cbsa, RCE)
}
if (geo_large == 'csa') {
out <- out %>%
dplyr::select(oid, csa, RCE) %>%
unique(.) %>%
dplyr::mutate(GEOID = oid) %>%
dplyr::select(GEOID, csa, RCE)
}
if (geo_large == 'metro') {
out <- out %>%
dplyr::select(oid, metro, RCE) %>%
unique(.) %>%
dplyr::mutate(GEOID = oid) %>%
dplyr::select(GEOID, metro, RCE)
}
out <- out %>%
.[.$GEOID != 'NANA', ] %>%
dplyr::filter(!is.na(GEOID)) %>%
dplyr::distinct(GEOID, .keep_all = TRUE) %>%
dplyr::arrange(GEOID) %>%
dplyr::as_tibble()
out_dat <- out_dat %>%
dplyr::arrange(GEOID) %>%
dplyr::as_tibble()
out <- list(rce = out, rce_data = out_dat, missing = missingYN)
return(out)
}