-
Notifications
You must be signed in to change notification settings - Fork 1
/
messer.R
268 lines (248 loc) · 13.9 KB
/
messer.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
#' Neighborhood Deprivation Index based on Messer et al. (2006)
#'
#' Compute the Neighborhood Deprivation Index (Messer) values.
#'
#' @param geo Character string specifying the geography of the data either census tracts \code{geo = "tract"} (the default) or counties \code{geo = "county"}.
#' @param year Numeric. The year to compute the estimate. The default is 2020, and the years between 2010 and 2020 are currently available.
#' @param imp Logical. If TRUE, will impute missing census characteristics within the internal \code{\link[psych]{principal}}. If FALSE (the default), will not impute.
#' @param quiet Logical. If TRUE, will display messages about potential missing census information and the proportion of variance explained by principal component analysis. 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 Neighborhood Deprivation Index (NDI) of U.S. census tracts or counties for a specified geographical referent (e.g., US-standardized) based on Messer et al. (2006) \doi{10.1007/s11524-006-9094-x}.
#'
#' The function uses the \code{\link[tidycensus]{get_acs}} function to obtain U.S. Census Bureau 5-year American Community Survey characteristics used for computation involving a principal component analysis with the \code{\link[psych]{principal}} function. The yearly estimates are available for 2010 and after when all census characteristics became available. The eight characteristics are:
#' \itemize{
#' \item{C24030: }{percent males in management, science, and arts occupation}
#' \item{B25014: }{percent of crowded housing}
#' \item{B17017: }{percent of households in poverty}
#' \item{B25115: }{percent of female headed households with dependents}
#' \item{B19058: }{percent of households on public assistance}
#' \item{B19001: }{percent of households earning <$30,000 per year}
#' \item{B06009: }{percent earning less than a high school education}
#' \item{B23025: }{percent unemployed (2011 onward)}
#' \item{B23001: }{percent unemployed (2010 only)}
#' }
#'
#' Use the internal \code{state} and \code{county} arguments within the \code{\link[tidycensus]{get_acs}} function to specify the referent for standardizing the NDI (Messer) values. For example, if all U.S. states are specified for the \code{state} argument, then the output would be a U.S.-standardized index.
#'
#' The continuous NDI (Messer) values are z-transformed, i.e., "standardized," and the categorical NDI (Messer) values are quartiles of the standardized continuous NDI (Messer) values.
#'
#' Check if the proportion of variance explained by the first principal component is high (more than 0.5).
#'
#' @return An object of class 'list'. This is a named list with the following components:
#'
#' \describe{
#' \item{\code{ndi}}{An object of class 'tbl' for the GEOID, name, NDI (standardized), NDI (quartile), and raw census values of specified census geographies.}
#' \item{\code{pca}}{An object of class 'principal', returns the output of \code{\link[psych]{principal}} used to compute the NDI values.}
#' \item{\code{missing}}{An object of class 'tbl' of the count and proportion of missingness for each census variable used to compute the NDI.}
#' }
#'
#' @import dplyr
#' @importFrom psych principal
#' @importFrom stats quantile
#' @importFrom stringr str_trim
#' @importFrom tidycensus get_acs
#' @importFrom tidyr pivot_longer separate
#' @export
#'
#' @seealso \code{\link[tidycensus]{get_acs}} for additional arguments for geographic referent selection (i.e., \code{state} and \code{county}).
#'
#' @examples
#' \dontrun{
#' # Wrapped in \dontrun{} because these examples require a Census API key.
#'
#' # Tract-level metric (2020)
#' messer(geo = "tract", state = "GA", year = 2020)
#'
#' # Impute NDI for tracts (2020) with missing census information (median values)
#' messer(state = "tract", "GA", year = 2020, imp = TRUE)
#'
#' # County-level metric (2020)
#' messer(geo = "county", state = "GA", year = 2020)
#'
#' }
#'
messer <- function(geo = "tract", year = 2020, imp = FALSE, quiet = FALSE, ...) {
# Check arguments
match.arg(geo, choices = c("county", "tract"))
stopifnot(is.numeric(year), year %in% 2010:2020)
# select census variables
vars <- c(PctMenMgmtBusScArti_num1 = "C24030_018", PctMenMgmtBusScArti_num2 = "C24030_019",
PctMenMgmtBusScArti_den = "C24030_002",
PctCrwdHH_num1 = "B25014_005", PctCrwdHH_num2 = "B25014_006",
PctCrwdHH_num3 = "B25014_007", PctCrwdHH_num4 = "B25014_011",
PctCrwdHH_num5 = "B25014_012", PctCrwdHH_num6 = "B25014_013",
PctCrwdHH_den = "B25014_001",
PctHHPov_num = "B17017_002", PctHHPov_den = "B17017_001",
PctFemHeadKids_num1 = "B25115_012", PctFemHeadKids_num2 = "B25115_025",
PctFemHeadKids_den = "B25115_001",
PctPubAsst_num = "B19058_002", PctPubAsst_den = "B19058_001",
PctHHUnder30K_num1 = "B19001_002", PctHHUnder30K_num2 = "B19001_003",
PctHHUnder30K_num3 = "B19001_004", PctHHUnder30K_num4 = "B19001_005",
PctHHUnder30K_num5 = "B19001_006", PctHHUnder30K_den = "B19001_001",
PctEducLessThanHS_num = "B06009_002", PctEducLessThanHS_den = "B06009_001",
PctUnemp_num = "B23025_005", PctUnemp_den = "B23025_003")
if (year == 2010) {
# select census variables
vars <- c(vars[-c(26,27)], PctUnemp_den = "B23001_001",
PctUnemp_1619M = "B23001_008", PctUnemp_2021M = "B23001_015",
PctUnemp_2224M = "B23001_022", PctUnemp_2529M = "B23001_029",
PctUnemp_3034M = "B23001_036", PctUnemp_3544M = "B23001_043",
PctUnemp_4554M = "B23001_050", PctUnemp_5559M = "B23001_057",
PctUnemp_6061M = "B23001_064", PctUnemp_6264M = "B23001_071",
PctUnemp_6569M = "B23001_076", PctUnemp_7074M = "B23001_081",
PctUnemp_75upM = "B23001_086", PctUnemp_1619F = "B23001_094",
PctUnemp_2021F = "B23001_101", PctUnemp_2224F = "B23001_108",
PctUnemp_2529F = "B23001_115", PctUnemp_3034F = "B23001_122",
PctUnemp_3544F = "B23001_129", PctUnemp_4554F = "B23001_136",
PctUnemp_5559F = "B23001_143", PctUnemp_6061F = "B23001_150",
PctUnemp_6264F = "B23001_157", PctUnemp_6569F = "B23001_162",
PctUnemp_7074F = "B23001_167", PctUnemp_75upF = "B23001_172")
# acquire NDI variables
ndi_vars <- suppressMessages(suppressWarnings(tidycensus::get_acs(geography = geo,
year = year,
output = "wide",
variables = vars, ...)))
if (geo == "tract") {
ndi_vars <- ndi_vars %>%
tidyr::separate(NAME, into = c("tract", "county", "state"), sep = ",") %>%
dplyr::mutate(tract = gsub("[^0-9\\.]","", tract))
} else {
ndi_vars <- ndi_vars %>% tidyr::separate(NAME, into = c("county", "state"), sep = ",")
}
ndi_vars <- ndi_vars %>%
dplyr::mutate(OCC = (PctMenMgmtBusScArti_num1E + PctMenMgmtBusScArti_num2E) / PctMenMgmtBusScArti_denE,
CWD = (PctCrwdHH_num1E + PctCrwdHH_num2E + PctCrwdHH_num3E +
PctCrwdHH_num4E + PctCrwdHH_num5E + PctCrwdHH_num6E) / PctCrwdHH_denE,
POV = PctHHPov_numE / PctHHPov_denE,
FHH = (PctFemHeadKids_num1E + PctFemHeadKids_num2E) / PctFemHeadKids_denE,
PUB = PctPubAsst_numE / PctPubAsst_denE,
U30 = (PctHHUnder30K_num1E + PctHHUnder30K_num2E + PctHHUnder30K_num3E +
PctHHUnder30K_num4E + PctHHUnder30K_num5E) / PctHHUnder30K_denE,
EDU = PctEducLessThanHS_numE / PctEducLessThanHS_denE,
EMP = (PctUnemp_1619ME + PctUnemp_2021ME +
PctUnemp_2224ME + PctUnemp_2529ME +
PctUnemp_4554ME + PctUnemp_5559ME +
PctUnemp_6061ME + PctUnemp_6264ME +
PctUnemp_6569ME + PctUnemp_7074ME +
PctUnemp_75upME + PctUnemp_1619FE +
PctUnemp_2021FE + PctUnemp_2224FE +
PctUnemp_2529FE + PctUnemp_4554FE +
PctUnemp_5559FE + PctUnemp_6061FE +
PctUnemp_6264FE + PctUnemp_6569FE +
PctUnemp_7074FE + PctUnemp_75upME) / PctUnemp_denE,
county = stringr::str_trim(county))
} else {
# acquire NDI variables
ndi_vars <- suppressMessages(suppressWarnings(tidycensus::get_acs(geography = geo,
year = year,
output = "wide",
variables = vars, ...)))
if (geo == "tract") {
ndi_vars <- ndi_vars %>%
tidyr::separate(NAME, into = c("tract", "county", "state"), sep = ",") %>%
dplyr::mutate(tract = gsub("[^0-9\\.]","", tract))
} else {
ndi_vars <- ndi_vars %>% tidyr::separate(NAME, into = c("county", "state"), sep = ",")
}
ndi_vars <- ndi_vars %>%
dplyr::mutate(OCC = (PctMenMgmtBusScArti_num1E + PctMenMgmtBusScArti_num2E) / PctMenMgmtBusScArti_denE,
CWD = (PctCrwdHH_num1E + PctCrwdHH_num2E + PctCrwdHH_num3E +
PctCrwdHH_num4E + PctCrwdHH_num5E + PctCrwdHH_num6E) / PctCrwdHH_denE,
POV = PctHHPov_numE / PctHHPov_denE,
FHH = (PctFemHeadKids_num1E + PctFemHeadKids_num2E) / PctFemHeadKids_denE,
PUB = PctPubAsst_numE / PctPubAsst_denE,
U30 = (PctHHUnder30K_num1E + PctHHUnder30K_num2E + PctHHUnder30K_num3E +
PctHHUnder30K_num4E + PctHHUnder30K_num5E) / PctHHUnder30K_denE,
EDU = PctEducLessThanHS_numE / PctEducLessThanHS_denE,
EMP = PctUnemp_numE / PctUnemp_denE,
county = stringr::str_trim(county))
}
# generate NDI
ndi_vars_pca <- ndi_vars %>%
dplyr::select(OCC, CWD, POV, FHH, PUB, U30, EDU, EMP)
# replace infinite values as zero (typically because denominator is zero)
ndi_vars_pca <- do.call(data.frame,
lapply(ndi_vars_pca,
function(x) replace(x, is.infinite(x), 0)))
# run principal component analysis
pca <- psych::principal(ndi_vars_pca,
nfactors = 1,
n.obs = nrow(ndi_vars_pca),
covar = FALSE,
scores = TRUE,
missing = imp)
# warning for missingness of census characteristics
missingYN <- ndi_vars_pca %>%
dplyr::select(OCC, CWD, POV, FHH, PUB, U30, EDU, EMP) %>%
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 (nrow(missingYN) != 0) {
message("Warning: Missing census data")
} else {
returnValue(missingYN)
}
# warning for proportion of variance explained by PC1
if (pca$Vaccounted[2] < 0.50) {
message("Warning: The proportion of variance explained by PC1 is less than 0.50.")
}
}
# NDI quartiles
NDIQuart <- data.frame(PC1 = pca$scores) %>%
dplyr::mutate(NDI = PC1 / pca$value[1]^2,
NDIQuart = cut(NDI,
breaks = stats::quantile(NDI,
probs = c(0, 0.25, 0.50, 0.75, 1),
na.rm = TRUE),
labels = c("1-Least deprivation", "2-BelowAvg deprivation",
"3-AboveAvg deprivation", "4-Most deprivation"),
include.lowest = TRUE),
NDIQuart = factor(replace(as.character(NDIQuart),
is.na(NDIQuart),
"9-NDI not avail"),
c(levels(NDIQuart), "9-NDI not avail"))) %>%
dplyr::select(NDI, NDIQuart)
# format output
ndi <- cbind(ndi_vars, NDIQuart) %>%
dplyr::mutate(OCC = round(OCC, digits = 1),
CWD = round(CWD, digits = 1),
POV = round(POV, digits = 1),
FHH = round(FHH, digits = 1),
PUB = round(PUB, digits = 1),
U30 = round(U30, digits = 1),
EDU = round(EDU, digits = 1),
EMP = round(EMP, digits = 1),
NDI = round(NDI, digits = 4))
if (geo == "tract") {
ndi <- ndi %>%
dplyr::select(GEOID,
state,
county,
tract,
NDI, NDIQuart,
OCC, CWD, POV, FHH, PUB, U30, EDU, EMP)
} else {
ndi <- ndi %>%
dplyr::select(GEOID,
state,
county,
NDI, NDIQuart,
OCC, CWD, POV, FHH, PUB, U30, EDU, EMP)
}
ndi <- ndi %>%
dplyr::mutate(county = stringr::str_trim(county),
state = stringr::str_trim(state)) %>%
dplyr::arrange(GEOID) %>%
dplyr::as_tibble()
out <- list(ndi = ndi,
pca = pca,
missing = missingYN)
return(out)
}