-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipums.R
546 lines (424 loc) · 16.7 KB
/
ipums.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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
# some convenience functions for dealing with IPUMS imports in SPSS format
library(tidyverse)
library(foreign)
library(stringr)
ipums_DIR <- getwd();
# import the SPSS file from IPUMS and check for any problems with factors
# posed to StackOverflow here: http://stackoverflow.com/questions/40987639/how-to-diagnose-duplicated-levels-in-an-r-import
ipums_load <- function(filepath) {
print("Loading IPUMS file. This may take a few minutes since it's a large extracts, but you only have to do it once.")
ipums <- read.spss(filepath, to.data.frame = TRUE)
print(paste("Loaded", NROW(ipums), "rows."));
# loop through each column that's a factor to see if there are duplicates,
# which produce the red "duplicated levels in factors are deprecated" warning
# but typically don't present any problems
for (name in names(ipums)) {
type <- class(ipums[[name]]);
if (type == "factor") {
dups <- anyDuplicated(levels(ipums[[name]]))
if (dups != 0) {
print(paste("Duplicate factor in", name, "at index", dups))
fac <- levels(ipums[[name]])
culprit <- fac[dups]
matches <- subset(ipums, ipums[[name]]==culprit)
print(paste("The offending value is", culprit, "which shows up", NROW(matches), "times in the data."))
if (NROW(matches) == 0) {
print("Since that value never occurs, I wouldn't worry about this.");
}
}
}
}
return (ipums);
}
ipums_field_AGE <- function(data) {
#data$AGEN <- as.character(data[,"AGE"]);
#data[!is.na(data$AGEN) & (data$AGEN == "Less than 1 year old" | data$AGEN == "Under 1 year"), "AGEN"] <- "0"
values <- levels(data$AGE)
# Descriptions of less than 1 is "0"
values <- replace(values, values == "Less than 1 year old" | values == "Under 1 year", "0")
# Descriptions of an age following by specification is just the year
# E.g. "90 (90+ in 1980 and 1990)"
values <- lapply(values, function(x) {
if (grepl("^[0-9]+ ", x)) {
x = str_split(x, " ")[[1]][[1]]
}
x = str_replace(x, "\\+", "")
return(x)
})
valuesAsYears <- as.numeric(values)
levels(data$AGE) <- valuesAsYears
data$AGE <- as.numeric(data$AGE)
return(data)
}
ipums_field_AGE_COHORT <- function(data, targetYear, source="CENSUS") {
if (!"YEAR_BORN" %in% colnames(data)) {
return(data)
}
fpath = file.path(ipums_DIR, "variables", "age_cohorts.csv")
canonical <- as.data.frame(read.csv(fpath, stringsAsFactors = F))
canonical <- canonical[canonical$Source == source,]
data$AGE_COHORT <- NA
data$TARGET_AGE <- targetYear - data$YEAR_BORN
for (i in 1:NROW(canonical)) {
maxAge = canonical[i,"MAX"]
cohort = canonical[i,"AGE_COHORT"]
data[is.na(data$AGE_COHORT) & data$TARGET_AGE <= maxAge, "AGE_COHORT"] <- cohort
print(paste("Added age cohort", cohort));
}
data <- subset(data, select = -TARGET_AGE )
return(data)
}
# STATES
ipums_field_STATEFIP <- function(data) {
if (!("STATEFIP" %in% colnames(data))) {
print("Skipping `ipums_field_STATEFIP` since 'STATEFIP' isn't present.")
return(data);
}
print("Adding state FIPs values and abbreviations")
# hand-crafted file that converts the state FIPs values to state names and abbreviations
fpath = file.path(ipums_DIR, "variables", "states.csv")
canonical <- as.data.frame(read.csv(fpath,
colClasses=c("character","character","character","logical")
))
data$STATE_NAME <- as.character(data$STATEFIP)
data$STATE_ABBR <- NA
data$STATE_FIPS <- NA
data <- subset(data, select = -STATEFIP )
convertField <- function(row) {
fips <- row$FIPS
abbr <- row$ABBR
name <- row$NAME
data$STATE_FIPS[data$STATE_NAME==name] <- fips
data$STATE_ABBR[data$STATE_NAME==name] <- abbr
return (data);
}
for (i in 1:NROW(canonical)) {
data <- convertField(canonical[i,])
}
data$STATE_ABBR <- as.factor(data$STATE_ABBR)
data$STATE_NAME <- as.factor(data$STATE_NAME)
data$STATE_FIPS <- as.factor(data$STATE_FIPS)
return(data);
}
ipums_field_COUNTY_FIPS <- function(data) {
if (!("COUNTYFIP" %in% colnames(data))) {
print("Skipping `ipums_field_county_FIPS` since 'COUNTYFIP' isn't present.");
return(data);
}
if (!("STATE_FIPS" %in% colnames(data))) {
print("converting states to FIPS")
data <- ipums_field_STATEFIP(data);
}
data$COUNTY_FIPS <- as.factor(paste0(data$STATE_FIPS, str_pad(data$COUNTYFIP, 3, pad="0")))
return(data)
}
# add names of PUMAs
ipums_field_PUMA_CODE <- function(data) {
if (!("PUMA" %in% colnames(data))) {
print("Skipping `ipums_field_PUMA` since 'PUMA' isn't present.");
return(data);
}
if (!("STATE_FIPS" %in% colnames(data))) {
print("converting states to FIPS")
data <- ipums_field_STATEFIP(data);
}
data$PUMA_CODE <- as.factor(paste0(data$STATE_FIPS, str_pad(data$PUMA, 5, pad="0")))
return(data);
}
ipums_field_DENSITY <- function(data, popYear=2017) {
data$PUMA_CODE <- as.character(data$PUMA_CODE)
fpath = file.path(ipums_DIR, "variables", "pumas.csv")
pumas <- read.csv(fpath, header=TRUE, colClasses = c(rep("character", 3), rep("numeric", 8)), stringsAsFactors = F)
pumas <- pumas %>% filter(USPS != "PR")
pumaPopulation <- data %>%
filter(YEAR == popYear) %>%
group_by(STATE_NAME, STATE_ABBR, STATE_FIPS, PUMA_CODE) %>%
summarize(
n = n(),
POP = sum(PERWT),
HH = sum(HHWT)
)
pumas <- merge(pumas, pumaPopulation, by="PUMA_CODE")
pumas <- pumas[,c("PUMA_CODE", "STATE_ABBR", "PUMA_NAME", "POP", "HH", "ALAND_SQMI")]
pumas$DENSITY_HH <- pumas$HH / pumas$ALAND_SQMI
pumas$DENSITY_POP <- pumas$POP / pumas$ALAND_SQMI
#pumas$URBAN_STATUS <- NA
#pumas[pumas$METRO=="In metropolitan area: In central/principal city", "URBAN_STATUS"] <- "Urban"
#pumas[pumas$METRO=="In metropolitan area: Not in central/principal city", "URBAN_STATUS"] <- "Suburban"
#pumas[pumas$METRO=="Not in metropolitan area", "URBAN_STATUS"] <- "Rural"
#summary(pumas[pumas$URBAN_STATUS == "Urban", "DENSITY"])
#summary(pumas[pumas$URBAN_STATUS == "Suburban", "DENSITY"])
#summary(pumas[pumas$URBAN_STATUS == "Rural", "DENSITY"])
# Imputed
#pumas[is.na(pumas$URBAN_STATUS) & pumas$DENSITY > 7500, "URBAN_STATUS"] <- "Urban"
#pumas[is.na(pumas$URBAN_STATUS) & pumas$DENSITY > 1000, "URBAN_STATUS"] <- "Suburban"
#summary(pumas[is.na(pumas$URBAN_STATUS), "DENSITY"])
#pumas[is.na(pumas$URBAN_STATUS), "URBAN_STATUS"] <- "Rural"
data <- left_join(data, pumas[,c("PUMA_CODE", "DENSITY_HH", "DENSITY_POP")], by="PUMA_CODE")
return(data);
}
# RACE AND ETHNICITY
ipums_field_RACE_ETHNICITY <- function(data) {
if (!"RACE" %in% colnames(data) | !"HISPAN" %in% colnames(data)) {
print("Skipping `ipums_field_RACE` since 'RACE' and 'HISPAN' aren't both present.")
return(data);
}
fpath = file.path(ipums_DIR, "variables", "race.csv")
canonical <- as.data.frame(read.csv(fpath, stringsAsFactors = F))
data$IS_HISPANIC <- F
data$RACE_SIMPLIFIED <- ""
data$RACE_ETHNICITY <- ""
data[data$HISPAN != "Not Hispanic", "IS_HISPANIC"] <- T
data$IS_HISPANIC <- as.logical(data$IS_HISPANIC)
for (i in 1:NROW(canonical)) {
originalRace = canonical[i,"RACE"]
simplifiedRace = canonical[i,"RACE_SIMPLIFIED"]
data[data$RACE_ETHNICITY == "" & data$RACE == originalRace, "RACE_SIMPLIFIED"] <- simplifiedRace
#print(paste("Converted", originalRace, "to", simplifiedRace));
}
data$RACE_ETHNICITY <- ifelse(data$IS_HISPANIC == T, "Hispanic", paste0(data$RACE_SIMPLIFIED, ", not-Hispanic"))
data$RACE_ETHNICITY <- as.factor(data$RACE_ETHNICITY)
data$RACE_SIMPLIFIED <- as.factor(data$RACE_SIMPLIFIED)
return(data);
}
# EDUCATION
ipums_field_EDUC <- function(data) {
if (!("EDUC" %in% colnames(data))) {
print("Skipping `ipums_field_EDUC` since 'EDUC' isn't present.")
return(data);
}
sourceKey = "EDUCD"
if (!("EDUCD" %in% colnames(data))) {
sourceKey = "EDUC"
}
# hand-crafted file that converts the many EDUCD values to more general categories
fpath = file.path(ipums_DIR, "variables", "educd.csv")
canonical <- as.data.frame(read.csv(fpath, stringsAsFactors = F))
print(paste("Simplifying", sourceKey, "into EDUC_SIMPLIFIED and EDUC_HAS_DEGREE"));
data$EDUC_SIMPLIFIED <- ""
data$EDUC_HAS_DEGREE <- ""
convertField <- function(row) {
original <- as.character(row[1]);
data[!is.na(data[[sourceKey]]) & data[[sourceKey]] == original, "EDUC_SIMPLIFIED"] <- as.character(row[2])
data[!is.na(data[[sourceKey]]) & data[[sourceKey]] == original, "EDUC_HAS_DEGREE"] <- as.character(row[3])
return (data);
}
for (i in 1:nrow(canonical)) {
data <- convertField(canonical[i,])
}
data$EDUC_SIMPLIFIED <- as.factor(data$EDUC_SIMPLIFIED)
data$EDUC_HAS_DEGREE <- as.logical(data$EDUC_HAS_DEGREE)
data <- subset(data, select = -EDUC)
if ("EDUCD" %in% colnames(data)) {
data <- subset(data, select = -EDUCD)
}
return(data);
}
# YEARS IN HOUSE
ipums_field_MOVEDIN <- function(data) {
if (!("MOVEDIN" %in% colnames(data))) {
print("Skipping `ipums_field_MOVEDIN` since 'MOVEDIN' isn't present.")
return(data);
}
# hand-crafted file that converts the many EDUCD values to more general categories
fpath = file.path(ipums_DIR, "variables", "movedin.csv")
canonical <- as.data.frame(read.csv(fpath, stringsAsFactors = F))
#keys = names(data)[grepl("^EDUCD(_[A-Z]+)?$", names(data))];
print("Simplifying MOVEDIN into HOME_TENURE_MIN and HOME_TENURE_MAX");
data$HOME_TENURE_MIN <- NA
data$HOME_TENURE_MAX <- NA
convertField <- function(row) {
original <- as.character(row[1]);
data[!is.na(data$MOVEDIN) & data$MOVEDIN == original, "HOME_TENURE_MIN"] <- as.numeric(row[3])
data[!is.na(data$MOVEDIN) & data$MOVEDIN == original, "HOME_TENURE_MAX"] <- as.numeric(row[4])
return (data);
}
for (i in 1:nrow(canonical)) {
data <- convertField(canonical[i,])
}
#data$EDUC_SIMPLIFIED <- as.factor(data$EDUC_SIMPLIFIED)
data <- subset(data, select = -MOVEDIN)
return(data);
}
# Match occupation names to the OCCSOC variable
ipums_field_OCCSOC <- function(data) {
if (!("OCCSOC" %in% colnames(data))) {
print("Skipping `ipums_field_OCCSOC` since 'OCCSOC' isn't present.")
return(data);
}
print("Adding OCCSOC occupation names")
# hand-crafted file that converts the OCCSOC codes to descriptions, including condensed categories
fpath = file.path(ipums_DIR, "variables", "occsoc.csv")
canonical <- as.data.frame(read.csv(fpath,
colClasses=rep("character", 4)
))
data$OCCSOC_TITLE <- ""
convertField <- function(row) {
title <- row$OCCSOC_TITLE
occsoc <- row$OCCSOC
data$OCCSOC_TITLE[data$OCCSOC==occsoc] <- title
return (data);
}
for (i in 1:NROW(canonical)) {
#print(paste(i, canonical[i,"OCCSOC_TITLE"]));
data <- convertField(canonical[i,])
}
data$OCCSOC_TITLE <- as.factor(data$OCCSOC_TITLE)
data <- subset(data, select = -OCCSOC)
return(data)
}
# BIRTHPLACE
ipums_field_BPL <- function(data, source="ACS") {
if (!("BPL" %in% colnames(data))) {
print("Skipping `ipums_field_BPL` since 'BPL' isn't present.")
return(data);
}
print("Adding BORN_US")
# hand-crafted file that converts the OCCSOC codes to descriptions, including condensed categories
fpath = file.path(ipums_DIR, "variables", "birthplace.csv")
canonical <- as.data.frame(read.csv(fpath, stringsAsFactors = F))
canonical <- canonical[grepl(source, canonical$Source),]
convertField <- function(row) {
location = row$BPL
bornInUS = row$BORN_US
data[data$BPL == location, "BORN_US"] <- bornInUS
return (data);
}
data$BORN_US <- NA
for (i in 1:NROW(canonical)) {
#print(paste(i, canonical[i,"BPL"]));
data <- convertField(canonical[i,])
}
return(data)
}
# CITIZENSHIP
ipums_field_CITIZEN <- function(data, source="ACS") {
if (!"CITIZEN" %in% colnames(data)) {
print("Skipping `ipums_field_CITIZEN` since 'CITIZEN' isn't present.")
return(data);
}
print("Adding IS_CITIZEN")
# hand-crafted file that converts the OCCSOC codes to descriptions, including condensed categories
fpath = file.path(ipums_DIR, "variables", "citizen.csv")
canonical <- as.data.frame(read.csv(fpath, stringsAsFactors = F))
canonical <- canonical[grepl(source, canonical$Source),]
convertField <- function(row) {
status = row$CITIZEN
isCitizen = row$IS_CITIZEN
data[data$CITIZEN == status, "IS_CITIZEN"] <- isCitizen
return (data);
}
data$IS_CITIZEN <- NA
for (i in 1:NROW(canonical)) {
#print(paste(i, canonical[i,"CITIZEN"]));
data <- convertField(canonical[i,])
}
return(data)
}
# Whether a person is eligible to vote
ipums_field_VOTING_ELIGIBLE <- function(data, electionYear, includeBirthQuarter = F) {
if ("YEAR_BORN" %in% colnames(data)) {
data$BIRTHYR <- data$YEAR_BORN
}
if (!"AGE" %in% colnames(data) & !"BIRTHYR" %in% colnames(data)) {
print("Skipping `ipums_field_VOTING_ELIGIBLE` since neither 'AGE' or 'BIRTHYR' are present. ('BIRTHYR' is preferred).")
return(data);
}
if (!"CITIZEN" %in% colnames(data)) {
print("Skipping `ipums_field_VOTING_ELIGIBLE` since 'CITIZEN' isn't present.")
return(data);
}
if (!"BIRTHYR" %in% colnames(data)) {
print("Computing birth year from AGE");
if (!"AGEN" %in% colnames(data)) { # if `ipums_convert_AGE` wasn't run
data <- ipums_convert_AGE(data);
}
data$BIRTHYR <- data$YEAR - data$AGEN;
}
if (!"IS_CITIZEN" %in% colnames(data)) {
print("Running `ipums_field_CITIZEN`")
data <- ipums_field_CITIZEN(data);
}
minimumYear <- electionYear - 18;
keyName = paste0("CAN_VOTE_", electionYear)
# We'll subtract those who can't vote
data[[keyName]] <- T;
data[[keyName]][!data$IS_CITIZEN] <- F
data[[keyName]][data$BIRTHYR > minimumYear] <- F
# This is the closest we can get to eliminating those who turn 18 after Election Day
if (includeBirthQuarter) {
print(paste0("Eliminating late ", minimumYear, " babies."))
data[[keyName]][data$BIRTHYR == minimumYear & data$BIRTHQTR == "Oct-Nov-Dec"] <- F
}
# While institutionalized populations can vote in some cases, this appears to be rare
data[[keyName]][data$GQ == "Group quarters--Institutions"] <- F
if ("YEAR_BORN" %in% colnames(data)) {
data <- subset(data, select = -BIRTHYR )
}
return(data)
}
# Whether a person was eligible to vote in the year surveyed
ipums_field_VOTING_ELIGIBLE_IN_YEAR <- function(data) {
if ("YEAR_BORN" %in% colnames(data)) {
data$BIRTHYR <- data$YEAR_BORN
}
if (!"AGE" %in% colnames(data) & !"BIRTHYR" %in% colnames(data)) {
print("Skipping `ipums_field_VOTING_ELIGIBLE` since neither 'AGE' or 'BIRTHYR' are present. ('BIRTHYR' is preferred).")
return(data);
}
if (!"CITIZEN" %in% colnames(data)) {
print("Skipping `ipums_field_VOTING_ELIGIBLE` since 'CITIZEN' isn't present.")
return(data);
}
if (!"BIRTHYR" %in% colnames(data)) {
print("Computing birth year from AGE");
if (!"AGEN" %in% colnames(data)) { # if `ipums_convert_AGE` wasn't run
data <- ipums_convert_AGE(data);
}
data$BIRTHYR <- data$YEAR - data$AGEN;
}
if (!"IS_CITIZEN" %in% colnames(data)) {
print("Running `ipums_field_CITIZEN`")
data <- ipums_field_CITIZEN(data);
}
#minimumYear <- electionYear - 18;
keyName = "VOTING_ELIGIBLE"
# We'll subtract those who can't vote
data[[keyName]] <- T;
data[[keyName]][!data$IS_CITIZEN] <- F
data[[keyName]][data$BIRTHYR > (data$YEAR - 18)] <- F
# While institutionalized populations can vote in some cases, this appears to be rare
data[[keyName]][data$GQ == "Group quarters--Institutions"] <- F
if ("YEAR_BORN" %in% colnames(data)) {
data <- subset(data, select = -BIRTHYR )
}
return(data)
}
ipums_field_YEAR_NATURALIZED <- function(data) {
if (!"YRNATUR" %in% colnames(data)) {
print("Skipping `ipums_field_YEAR_NATURALIZED` since 'YRNATUR' isn't present.")
return(data);
}
values <- levels(data$YRNATUR)
valuesAsYears <- lapply(values, function(x) {
isYr = grepl("^[0-9]{4}", x)
return(ifelse(isYr, substr(x, 1, 4), NA))
})
valuesAsYears <- as.numeric(valuesAsYears)
levels(data$YRNATUR) <- valuesAsYears
data$YRNATUR <- as.numeric(data$YRNATUR)
return(data)
}
# convert a factors to their correct types. NOT RECOMMENDED
ipums_convert_factors <- function(ipums) {
ipums$YEAR <- as.numeric(ipums$YEAR)
print("de-factorizing remainder of factored columns into characters")
types <- lapply(ipums, class)
factor_columns <- names(types[types=="factor"])
print("Converting factors to the appropriate types")
for (column in factor_columns) {
print(paste("Converting", column))
ipums[[column]] <- as.character(ipums[[column]])
}
return(ipums);
}