-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpowell_wiley.R
467 lines (433 loc) · 18.9 KB
/
powell_wiley.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
456
457
458
459
460
461
462
463
464
465
466
467
#' Neighborhood Deprivation Index based on Andrews et al. (2020) and Slotman et al. (2022)
#'
#' Compute the aspatial Neighborhood Deprivation Index (Powell-Wiley).
#'
#' @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 2010 onward are currently available.
#' @param imp Logical. If TRUE, will impute missing census characteristics within the internal \code{\link[psych]{principal}} using median values of variables. If FALSE (the default), will not impute.
#' @param quiet Logical. If TRUE, will display messages about potential missing census information, standardized Cronbach's alpha, and proportion of variance explained by principal component analysis. The default is FALSE.
#' @param round_output Logical. If TRUE, will round the output of raw census and \emph{NDI} values from the \code{\link[tidycensus]{get_acs}} at one and four significant digits, respectively. The default is FALSE.
#' @param df Optional. Pass a pre-formatted \code{'dataframe'} or \code{'tibble'} with the desired variables through the function. Bypasses the data obtained by \code{\link[tidycensus]{get_acs}}. The default is NULL. See Details below.
#' @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 Neighborhood Deprivation Index (\emph{NDI}) of U.S. census tracts or counties for a specified geographical referent (e.g., US-standardized) based on Andrews et al. (2020) \doi{10.1080/17445647.2020.1750066} and Slotman et al. (2022) \doi{10.1016/j.dib.2022.108002}.
#'
#' 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 factor analysis with the \code{\link[psych]{principal}} function. The yearly estimates are available in 2010 and after when all census characteristics became available. The thirteen characteristics chosen by Roux and Mair (2010) \doi{10.1111/j.1749-6632.2009.05333.x} are:
#' \itemize{
#' \item \strong{MedHHInc (B19013)}: median household income (dollars)
#' \item \strong{PctRecvIDR (B19054)}: percent of households receiving dividends, interest, or rental income
#' \item \strong{PctPubAsst (B19058)}: percent of households receiving public assistance
#' \item \strong{MedHomeVal (B25077)}: median home value (dollars)
#' \item \strong{PctMgmtBusScArti (C24060)}: percent in a management, business, science, or arts occupation
#' \item \strong{PctFemHeadKids (B11005)}: percent of households that are female headed with any children under 18 years
#' \item \strong{PctOwnerOcc (DP04)}: percent of housing units that are owner occupied
#' \item \strong{PctNoPhone (DP04)}: percent of households without a telephone
#' \item \strong{PctNComPlm (DP04)}: percent of households without complete plumbing facilities
#' \item \strong{PctEducHSPlus (S1501)}: percent with a high school degree or higher (population 25 years and over)
#' \item \strong{PctEducBchPlus (S1501)}: percent with a college degree or higher (population 25 years and over)
#' \item \strong{PctFamBelowPov (S1702)}: percent of families with incomes below the poverty level
#' \item \strong{PctUnempl (S2301)}: percent unemployed
#' }
#'
#' Use the internal \code{state} and \code{county} arguments within the \code{\link[tidycensus]{get_acs}} function to specify the referent for standardizing the \emph{NDI} (Powell-Wiley) 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. Please note: the \emph{NDI} (Powell-Wiley) values will not exactly match (but will highly correlate with) those found in Andrews et al. (2020) \doi{10.1080/17445647.2020.1750066} and Slotman et al. (2022) \doi{10.1016/j.dib.2022.108002} because the two studies used a different statistical platform (i.e., SPSS and SAS, respectively) that intrinsically calculate the principal component analysis differently from R.
#'
#' The categorical \emph{NDI} (Powell-Wiley) values are population-weighted quintiles of the continuous \emph{NDI} (Powell-Wiley) values.
#'
#' Check if the proportion of variance explained by the first principal component is high (more than 0.5).
#'
#' Users can bypass \code{\link[tidycensus]{get_acs}} by specifying a pre-formatted data frame or tibble using the \code{df} argument. This function will compute an index using the first component of a principal component analysis (PCA) with a Promax (oblique) rotation and a minimum Eigenvalue of 1, omitting variables with absolute loading score < 0.4. The recommended structure of the data frame or tibble is an ID (e.g., GEOID) in the first feature (column), an estimate of the total population in the second feature (column), followed by the variables of interest (in any order) and no additional information (e.g., omit state or county names from the \code{df} argument input).
#'
#' @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, \emph{NDI} continuous, \emph{NDI} quintiles, 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 \emph{NDI} values.}
#' \item{\code{missing}}{An object of class 'tbl' of the count and proportion of missingness for each census variable used to compute \emph{NDI}.}
#' \item{\code{cronbach}}{An object of class 'character' or 'numeric' for the results of the Cronbach's alpha calculation. If only one factor is computed, a message is returned. If more than one factor is computed, Cronbach's alpha is calculated and should check that it is >0.7 for respectable internal consistency between factors.}
#' }
#'
#' @import dplyr
#' @importFrom MASS ginv
#' @importFrom psych alpha principal
#' @importFrom stats complete.cases cor cov2cor loadings median promax quantile sd
#' @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
#'
#' powell_wiley(df = DCtracts2020[ , -c(3:10)])
#'
#' \dontrun{
#' # Wrapped in \dontrun{} because these examples require a Census API key.
#'
#' # Tract-level NDI (Powell-Wiley; 2020)
#' powell_wiley(geo = 'tract', state = 'GA', year = 2020)
#'
#' # Impute NDI (Powell-Wiley; 2020) for tracts with missing census information (median values)
#' powell_wiley(state = 'tract', state = 'GA', year = 2020, imp = TRUE)
#'
#' }
#'
powell_wiley <- function(geo = 'tract',
year = 2020,
imp = FALSE,
quiet = FALSE,
round_output = FALSE,
df = NULL,
...) {
# Check arguments
if (!is.null(df) &
!inherits(df, c('tbl_df', 'tbl', 'data.frame'))) {
stop("'df' must be class 'data.frame' or 'tbl'")
}
if (is.null(df)) {
# Check additional arguments
match.arg(geo, choices = c('county', 'tract'))
stopifnot(is.numeric(year), year >= 2010) # all variables available 2010 onward
# Select census variables
vars <- c(
MedHHInc = 'B19013_001',
PctRecvIDR_num = 'B19054_002',
PctRecvIDR_den = 'B19054_001',
PctPubAsst_num = 'B19058_002',
PctPubAsst_den = 'B19058_001',
MedHomeVal = 'B25077_001',
PctMgmtBusScArti_num = 'C24060_002',
PctMgmtBusScArti_den = 'C24060_001',
PctFemHeadKids_num1 = 'B11005_007',
PctFemHeadKids_num2 = 'B11005_010',
PctFemHeadKids_den = 'B11005_001',
PctOwnerOcc = 'DP04_0046P',
PctNoPhone = 'DP04_0075P',
PctNComPlmb = 'DP04_0073P',
PctEduc_num25upHS = 'S1501_C01_009',
PctEduc_num25upSC = 'S1501_C01_010',
PctEduc_num25upAD = 'S1501_C01_011',
PctEduc_num25upBD = 'S1501_C01_012',
PctEduc_num25upGD = 'S1501_C01_013',
PctEduc_den25up = 'S1501_C01_006',
PctFamBelowPov = 'S1702_C02_001',
PctUnempl = 'S2301_C04_001',
TotalPopulation = 'B01001_001'
)
# Updated census variable definition(s)
if (year < 2015) {
vars <- c(vars[-13], PctNoPhone = 'DP04_0074P')
}
# Acquire NDI variables
ndi_data <- suppressMessages(suppressWarnings(
tidycensus::get_acs(
geography = geo,
year = year,
output = 'wide',
variables = vars,
...
)
))
if (geo == 'tract') {
ndi_data <- ndi_data %>%
tidyr::separate(NAME, into = c('tract', 'county', 'state'), sep = ',') %>%
dplyr::mutate(tract = gsub('[^0-9\\.]', '', tract))
} else {
ndi_data <- ndi_data %>%
tidyr::separate(NAME, into = c('county', 'state'), sep = ',')
}
ndi_data <- ndi_data %>%
dplyr::mutate(
MedHHInc = MedHHIncE,
PctRecvIDR = PctRecvIDR_numE / PctRecvIDR_denE * 100,
PctPubAsst = PctPubAsst_numE / PctPubAsst_denE * 100,
MedHomeVal = MedHomeValE,
PctMgmtBusScArti = PctMgmtBusScArti_numE / PctMgmtBusScArti_denE * 100,
PctFemHeadKids = (PctFemHeadKids_num1E + PctFemHeadKids_num2E) /
PctFemHeadKids_denE * 100,
PctOwnerOcc = PctOwnerOccE,
PctNoPhone = PctNoPhoneE,
PctNComPlmb = PctNComPlmbE,
PctEducHSPlus = (
PctEduc_num25upHSE + PctEduc_num25upSCE + PctEduc_num25upADE + PctEduc_num25upBDE +
PctEduc_num25upGDE
) / PctEduc_den25upE * 100,
PctEducBchPlus = (PctEduc_num25upBDE + PctEduc_num25upGDE) / PctEduc_den25upE * 100,
PctFamBelowPov = PctFamBelowPovE,
PctUnempl = PctUnemplE,
TotalPop = TotalPopulationE
) %>%
# Log transform median household income and median home value
# Reverse code percentages so that higher values represent more deprivation
# Round percentages to 1 decimal place
dplyr::mutate(
logMedHHInc = log(MedHHInc),
logMedHomeVal = log(MedHomeVal),
PctNoIDR = 100 - PctRecvIDR,
PctWorkClass = 100 - PctMgmtBusScArti,
PctNotOwnerOcc = 100 - PctOwnerOcc,
PctEducLTHS = 100 - PctEducHSPlus,
PctEducLTBch = 100 - PctEducBchPlus
) %>%
# Z-standardize the percentages
dplyr::mutate(
PctNoIDRZ = scale(PctNoIDR),
PctPubAsstZ = scale(PctPubAsst),
PctWorkClassZ = scale(PctWorkClass),
PctFemHeadKidsZ = scale(PctFemHeadKids),
PctNotOwnerOccZ = scale(PctNotOwnerOcc),
PctNoPhoneZ = scale(PctNoPhone),
PctNComPlmbZ = scale(PctNComPlmb),
PctEducLTHSZ = scale(PctEducLTHS),
PctEducLTBchZ = scale(PctEducLTBch),
PctFamBelowPovZ = scale(PctFamBelowPov),
PctUnemplZ = scale(PctUnempl)
)
# generate NDI
ndi_data_pca <- ndi_data %>%
dplyr::select(
logMedHHInc,
PctNoIDRZ,
PctPubAsstZ,
logMedHomeVal,
PctWorkClassZ,
PctFemHeadKidsZ,
PctNotOwnerOccZ,
PctNoPhoneZ,
PctNComPlmbZ,
PctEducLTHSZ,
PctEducLTBchZ,
PctFamBelowPovZ,
PctUnemplZ
)
} else {
# If inputing pre-formatted data:
## rename first and second features (columns) with name to match above
colnames(df)[1:2] <- c('GEOID', 'TotalPop')
ndi_data <- dplyr::as_tibble(df)
## omit the first two features (columns) typically an ID (e.g., GEOID or FIPS) and TotalPop
ndi_data_pca <- ndi_data[,-c(1:2)]
}
# Run a factor analysis using Promax (oblique) rotation and a minimum Eigenvalue of 1
nfa <- eigen(stats::cor(ndi_data_pca, use = 'complete.obs'))
nfa <- sum(nfa$values > 1) # count of factors with a minimum Eigenvalue of 1
fit <- psych::principal(ndi_data_pca, nfactors = nfa, rotate = 'none')
fit_rotate <- stats::promax(stats::loadings(fit), m = 3)
# Calculate the factors using only variables with an absolute loading score > 0.4 for the first factor
## If number of factors > 2, use structure matrix, else pattern matrix
if (nfa > 1) {
P_mat <- matrix(stats::loadings(fit_rotate), nrow = 13, ncol = nfa)
# Structure matrix (S_mat) from under-the-hood of the psych::principal() function
rot.mat <- fit_rotate$rotmat # rotation matrix
ui <- solve(rot.mat)
Phi <- cov2cor(ui %*% t(ui)) # interfactor correlation
S_mat <- P_mat %*% Phi # pattern matrix multiplied by interfactor correlation
} else {
P_mat <- matrix(fit_rotate, nrow = 13, ncol = 1)
Phi <- 1
S_mat <- P_mat
}
## Variable correlation matrix (R_mat)
R_mat <- as.matrix(cor(ndi_data_pca[complete.cases(ndi_data_pca),]))
## standardized score coefficients or weight matrix (B_mat)
B_mat <- solve(R_mat, S_mat)
# Additional PCA Information
fit_rotate$rotation <- 'promax'
fit_rotate$Phi <- Phi
fit_rotate$Structure <- S_mat
if (nfa > 1) {
fit_rotate$communality <- rowSums(P_mat ^ 2)
} else {
fit_rotate$communality <- P_mat ^ 2
}
fit_rotate$uniqueness <- diag(R_mat) - fit_rotate$communality
if (nfa > 1) {
vx <- colSums(P_mat ^ 2)
} else {
vx <- sum(P_mat ^ 2)
}
vtotal <- sum(fit_rotate$communality + fit_rotate$uniqueness)
vx <- diag(Phi %*% t(P_mat) %*% P_mat)
names(vx) <- colnames(loadings)
varex <- rbind(`SS loadings` = vx)
varex <- rbind(varex, `Proportion Var` = vx / vtotal)
if (nfa > 1) {
varex <- rbind(varex, `Cumulative Var` = cumsum(vx / vtotal))
varex <- rbind(varex, `Proportion Explained` = vx / sum(vx))
varex <- rbind(varex, `Cumulative Proportion` = cumsum(vx / sum(vx)))
}
fit_rotate$Vaccounted <- varex
if (imp == TRUE) {
ndi_data_scrs <- as.matrix(ndi_data_pca)
miss <- which(is.na(ndi_data_scrs), arr.ind = TRUE)
item.med <- apply(ndi_data_scrs, 2, stats::median, na.rm = TRUE)
ndi_data_scrs[miss] <- item.med[miss[, 2]]
} else {
ndi_data_scrs <- ndi_data_pca
}
scrs <- as.matrix(
scale(ndi_data_scrs[complete.cases(ndi_data_scrs), abs(S_mat[, 1]) > 0.4])
) %*% B_mat[abs(S_mat[, 1]) > 0.4, 1]
ndi_data_NA <- ndi_data[complete.cases(ndi_data_scrs),]
ndi_data_NA$NDI <- c(scrs)
ndi_data_NDI <- ndi_data[, c('GEOID', 'TotalPop')] %>%
dplyr::left_join(ndi_data_NA[, c('GEOID', 'NDI')], by = dplyr::join_by(GEOID))
# Calculate Cronbach's alpha correlation coefficient among the factors and verify values are above 0.7.
if (nfa == 1) {
crnbch <-
"Only one factor with minimum Eigenvalue of 1. Cannot calculate Cronbach's alpha."
} else {
cronbach <- suppressMessages(psych::alpha(
ndi_data_pca[, abs(S_mat[, 1]) > 0.4],
check.keys = TRUE,
na.rm = TRUE,
warnings = FALSE
))
crnbch <- cronbach$total$std.alpha
}
# Warning for missingness of census characteristics
missingYN <- ndi_data_pca %>%
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')
}
# Warning for Cronbach's alpha < 0.7
if (cronbach$total$std.alpha < 0.7) {
message(
"Warning: Cronbach's alpha correlation coefficient among the factors is less than 0.7."
)
}
# Warning for proportion of variance explained by FA1
if (fit_rotate$Vaccounted[2] < 0.50) {
message('Warning: The proportion of variance explained by PC1 is less than 0.50.')
}
}
# NDI quintiles weighted by tract population
NDIQuint <- ndi_data_NDI %>%
dplyr::mutate(
NDIQuint = cut(
NDI * log(TotalPop),
breaks = stats::quantile(
NDI * log(TotalPop),
probs = c(0, 0.2, 0.4, 0.6, 0.8, 1),
na.rm = TRUE
),
labels = c(
'1-Least deprivation',
'2-BelowAvg deprivation',
'3-Average deprivation',
'4-AboveAvg deprivation',
'5-Most deprivation'
),
include.lowest = TRUE
),
NDIQuint = factor(
replace(
as.character(NDIQuint),
is.na(NDIQuint) |
is.infinite(NDIQuint),
'9-NDI not avail'
),
c(levels(NDIQuint), '9-NDI not avail')
)
) %>%
dplyr::select(NDI, NDIQuint)
if (is.null(df)) {
# Format output
if (round_output == TRUE) {
ndi <- cbind(ndi_data, NDIQuint) %>%
dplyr::mutate(
PctRecvIDR = round(PctRecvIDR, digits = 1),
PctPubAsst = round(PctPubAsst, digits = 1),
PctMgmtBusScArti = round(PctMgmtBusScArti, digits = 1),
PctFemHeadKids = round(PctFemHeadKids, digits = 1),
PctOwnerOcc = round(PctOwnerOcc, digits = 1),
PctNoPhone = round(PctNoPhone, digits = 1),
PctNComPlmb = round(PctNComPlmb, digits = 1),
PctEducHSPlus = round(PctEducHSPlus, digits = 1),
PctEducBchPlus = round(PctEducBchPlus, digits = 1),
PctFamBelowPov = round(PctFamBelowPov, digits = 1),
PctUnempl = round(PctUnempl, digits = 1)
)
} else {
ndi <- cbind(ndi_data, NDIQuint)
}
if (geo == 'tract') {
ndi <- ndi %>%
dplyr::select(
GEOID,
state,
county,
tract,
NDI,
NDIQuint,
MedHHInc,
PctRecvIDR,
PctPubAsst,
MedHomeVal,
PctMgmtBusScArti,
PctFemHeadKids,
PctOwnerOcc,
PctNoPhone,
PctNComPlmb,
PctEducHSPlus,
PctEducBchPlus,
PctFamBelowPov,
PctUnempl,
TotalPop
)
} else {
ndi <- ndi %>%
dplyr::select(
GEOID,
state,
county,
NDI,
NDIQuint,
MedHHInc,
PctRecvIDR,
PctPubAsst,
MedHomeVal,
PctMgmtBusScArti,
PctFemHeadKids,
PctOwnerOcc,
PctNoPhone,
PctNComPlmb,
PctEducHSPlus,
PctEducBchPlus,
PctFamBelowPov,
PctUnempl,
TotalPop
)
}
ndi <- ndi %>%
dplyr::mutate(
state = stringr::str_trim(state),
county = stringr::str_trim(county)
) %>%
dplyr::arrange(GEOID) %>%
dplyr::as_tibble()
} else {
ndi <- cbind(df[, 1], NDIQuint, df[, 2:ncol(df)])
ndi <- dplyr::as_tibble(ndi[order(ndi[, 1]),])
}
out <- list(
ndi = ndi,
pca = fit_rotate,
missing = missingYN,
cronbach = crnbch
)
return(out)
}