Repository navigation
Expand file tree
/
Copy pathacs.R
More file actions
564 lines (362 loc) · 15.4 KB
/
Copy pathacs.R
File metadata and controls
564 lines (362 loc) · 15.4 KB
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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
# source( "S:/Users/AnthonyD/Current Population Survey/repository/acs.R" , echo = TRUE )
microdata_name <- 'acs'
source( "S:/Users/AnthonyD/Current Population Survey/repository/settings.R" )
state_preimputation <-
function(
# only variable that changes across states
this_state
){
source( "S:/Users/AnthonyD/Current Population Survey/repository/settings.R" )
source( paste0( code_folder , "kff recodes.R" ) )
# # # # # # # # #
x <- readRDS( paste0( "R:/American Community Survey/states/" , this_state , year - 1 , "_1yr.rds" ) )
if( year - 1 >= 2023 ){ x[ , 'st' ] <- x[ , 'state' ] ; x[ , 'state' ] <- NULL }
# immediately remove everyone except civilian noninstitutionalized
x <- x[ x[ , paste0( 'type' , if( year > 2020 ) 'hugq' ) ] %in% c( 1 , 3 ) & !( x[ , 'esr' ] %in% 4:5 ) , ]
# immediately remove everyone outside of the poverty universe
x <- subset( x , !is.na( povpip ) )
if( year >= 2020 ){
before_nrow <- nrow( x )
# put `relp` back on the file
relp_crosswalk <-
structure(
list(
relshipp = c("20", "21", "22", "23", "24", "25",
"26", "27", "28", "29", "30", "31", "32", "33",
"34", "35", "36", "37", "38"),
relp = c("00", "01", "13", "01", "13", "02", "03",
"04", "05", "06", "07", "08", "09", "10", "12", "14",
"15", "16", "17")
),
class = "data.frame",
row.names = c(NA, -19L)
)
x <- merge( x , relp_crosswalk )
# end of `relp` remake
if( nrow( x ) != before_nrow ) stop( paste( this_state , "did not merge properly" ) )
}
# coerce numeric variables to numeric
suppressWarnings(
numeric_vars <- sapply( x , function( w ) sum( is.na( w ) ) == sum( is.na( as.numeric( w ) ) ) )
)
# except for serialno, which causes a problem when it's numeric.
# needs to stay character for merges
numeric_vars[ names( numeric_vars ) == 'serialno' ] <- FALSE
x[ numeric_vars ] <- sapply( x[ numeric_vars ] , as.numeric )
# calculate weighted premiums for pumas
source( paste0( code_folder , "premiums for acs geographies.R" ) )
# skip premiums for puerto rico
if( this_state == 'pr' ){
x[ , c( 'puma_name' , 'sls' , 'lcb' ) ] <- NA
} else {
before_nrow <- nrow( x )
x <- merge( x , wtd_prems )
if( nrow( x ) != before_nrow ) stop( paste( this_state , "did not merge properly" ) )
# check to see if you are missing premium vars
if( any( is.na( x[ c( "sls" , "lcb" ) ] ) ) ) stop( "records should never have missing premium values" )
}
# # # # # # # # # # # # # # # # # # # # # # # #
# add state names and expansion state status #
# # # # # # # # # # # # # # # # # # # # # # # #
# limit the medicaid_fpl table to non-senior non-bhp columns
# excludes: caidelig_senior caidelig_bhp
mcfpl_ne <- medicaid_fpl[ , grep( 'gestfips|parent|adult|infant|toddler|child' , names( medicaid_fpl ) , value = TRUE ) ]
# determine which states are universally at or above 138%
mcfpl_ne[ , 'expansion_state_current' ] <- as.numeric( apply( mcfpl_ne[ , -which( names( mcfpl_ne ) == 'gestfips' ) ] , 1 , min ) >= 1.38 )
# this leaves out wisconsin, despite currently not having anybody in the gap
# vector of state fips codes containing only expansion states
exp_states <- mcfpl_ne[ mcfpl_ne[ , 'expansion_state_current' ] %in% 1 , 'gestfips' ]
# paste expansion state status, state names, and stateab onto the table
x <-
transform(
x ,
expansion_state_current = factor( as.numeric( st %in% exp_states ) + 1 , levels = 1:2 , labels = c( 'non-expansion state' , 'expansion state' ) ) ,
state_name =
factor(
match( st , medicaid_fpl[ , 'gestfips' ] ) ,
levels = 1:52 ,
labels = medicaid_fpl[ , 'statename' ]
) ,
stateab =
factor(
match( st , medicaid_fpl[ , 'gestfips' ] ) ,
levels = 1:52 ,
labels = medicaid_fpl[ , 'stateab' ]
)
)
# # # # # # # # # # # # # # # # # # # #
# add kff health insurance hierarchy #
# # # # # # # # # # # # # # # # # # # #
x <- acs_health_insurance( x )
# # # # # # # # # # # # # # #
# add citizenship variable #
# # # # # # # # # # # # # # #
x[ , 'kff_primer_citshp_4' ] <-
factor(
ifelse( x[ , 'cit' ] %in% 1:3 , 1 ,
ifelse( x[ , 'cit' ] %in% 4 , 2 ,
ifelse( x[ , 'yoep' ] %in% seq( year - 5 , year - 1 ) , 3 ,
4 ) ) ) ,
labels = c( 'us citizen - native' , 'us citizen - naturalized' , 'non-citizen - resident for less than 5 years' , 'non-citizen - resident for 5+ years' )
)
# # # # # # # # # # # # # # # # # # #
# add family relationship variables #
# # # # # # # # # # # # # # # # # # #
x <- acs_sploc( x )
x <- acs_momloc_poploc( x )
x <- acs_families( x )
x <- add_parent( x , microdata_name = 'acs' )
x <- primer_recodes( x , microdata_name = 'acs' )
x <- add_famtype( x , microdata_name = 'acs' )
# unimputed df can be saved here
saveRDS( x , file = paste0( acs_output_folder , "/" , year - 1 , " " , this_state , " unimputed 1yr.rds" ) , compress = FALSE )
# skip unauthorized imputation for puerto rico
if( this_state != 'pr' ) {
source( paste0( code_folder , "unauthorized imputation functions.R" ) )
x <- x[ order( x[ , 'serialno' ] , x[ , 'sporder' ] ) , ]
set.seed( year )
ua_result <- impute_unauthorized( x , dataset_named_year = year - 1 , microdata_name = "acs" , n = 5 )
xl <-
lapply(
1:5 ,
function( i ){
xu <- ua_result[ , c( 'serialno' , 'sporder' , paste0( 'unauthorized' , i ) ) ]
names( xu )[ -1:-2 ] <- 'unauthorized'
merge( x , xu )
}
)
# save all five implicates with only the undocumented flag
for( i in 1:5 ){
saveRDS( xl[[i]] , file = paste0( acs_output_folder , "/" , year - 1 , " " , this_state , " " , i , " undocumented 1yr.rds" ) , compress = FALSE )
}
}
invisible( NULL )
}
state_postimputation <-
function(
# only variable that changes across states
this_level
){
this_state <- substr( this_level , 1 , 2 )
this_i <- substr( this_level , 3 , 3 )
source( "S:/Users/AnthonyD/Current Population Survey/repository/settings.R" )
source( paste0( code_folder , "kff recodes.R" ) )
source( paste0( code_folder , "hiu functions.R" ) )
source( paste0( code_folder , "hiu offer regression functions.R" ) )
source( paste0( code_folder , "medicaid fpl functions.R" ) )
source( paste0( code_folder , "chip fpl functions.R" ) )
source( paste0( code_folder , "aca premium functions.R" ) )
source( paste0( code_folder , "aca eligibility functions.R" ) )
# # # # # # # # #
x <- readRDS( paste0( acs_output_folder , "/" , year - 1 , " " , this_state , " " , this_i , " undocumented 1yr.rds" ) )
caidl <-
create_hiu(
x ,
irs_year = year - 1 ,
hhspov_year = year - 1 ,
microdata_name = 'acs' ,
program = 'medicaid' ,
income_period = 'annualized' ,
income_inflation_factor = 1 ,
blank_unauthorized = FALSE
)
x <- rnam( x , caidl , n = 'caid' )
txl <-
create_hiu(
x ,
irs_year = myear + 1 ,
hhspov_year = myear ,
microdata_name = 'acs' ,
program = 'aptc' ,
keep_hiuid = TRUE ,
keep_pqcpqr = TRUE ,
ssi_blank = FALSE ,
income_period = 'annualized' ,
income_inflation_factor = miif ,
blank_unauthorized = TRUE
)
x <- rnam( x , txl , n = 'tax' )
# # # # # # # # # # # # #
# # construct offer hiu #
# # # # # # # # # # # # #
x[ , 'of_id' ] <- x[ , 'tax_id' ]
# if the individual is not an eligible qualifying child
# AND also not the household claimant,
# the individual is not going to receive an offer with
# the rest of the tax-claiming family.
rr <- !( x[ , 'eligible_qc' ] ) & !( x[ , 'tax_claimant' ] %in% 1 ) & !is.na( x[ , 'of_id' ] )
# make them into their own HIU
x[ rr , 'of_id' ] <-
paste(
x[ rr , 'of_id' ] ,
x[ rr , 'sporder' ]
)
# give undocumenteds a census_of_id
# because they could have an offer at work
x[ , 'census_of_id' ] <- x[ , 'of_id' ]
x[ is.na( x[ , 'census_of_id' ] ) , 'census_of_id' ] <-
paste(
x[ is.na( x[ , 'census_of_id' ] ) , 'serialno' ] ,
x[ is.na( x[ , 'census_of_id' ] ) , 'sporder' ]
)
x[ , 'of_id' ] <- NULL
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # distribute the census offer variable to everyone in the offer-unit #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
x <- x[ order( x[ , 'serialno' ] , x[ , 'sporder' ] ) , ]
set.seed( year )
x <- hiu_offer_imputation( x , year = year )
# # # # # # # # # # # # # # # # # # # #
# add medicaid eligibility thresholds #
# # # # # # # # # # # # # # # # # # # #
x <- add_medicaid_eligibility( x , entered_five_years_ago = seq( year - 5 , year - 1 ) , mcfpl = medicaid_fpl , microdata_name = 'acs' )
# # # # # # # # # # # # # # # # # #
# add chip eligibility thresholds #
# # # # # # # # # # # # # # # # # #
x <- add_chip_eligibility( x , entered_five_years_ago = seq( year - 5 , year - 1 ) , chfpl = chip_fpl , microdata_name = 'acs' )
# confirm that the maximum of schip + mchip + title xix
# produces the same result as caid_lvl for all children. no exceptions.
y <- subset( x , agep %in% 0:18 )
y[ , 'max_lvl' ] <- apply( y[ , c( 'schip_lvl' , 'mchip_lvl' , 'xix_lvl' ) ] , 1 , max )
# confirm that all individuals in the data set are either eligible for both caid_lvl and max_lvl,
# or they are eligible for neither. these two results must be perfectly correlated.
stopifnot( all.equal( cor( no.na( y[ , 'caidpov' ] < y[ , 'max_lvl' ] ) , no.na( y[ , 'caidpov' ] < y[ , 'caid_lvl' ] ) ) , 1 ) )
# note that some records might have lower max_lvl values than caid_lvl
# because mchip eligibility has income floors.
# these income floors should never affect whether an individual is eligible
# for at least one of the three programs (unless some state has a gap in coverage)
# `caid_lvl` shows each individual's maximum fpl for medicaid coverage
# the three `schip/mchip/xix_lvl` variables show the eligibility ceiling dependent on the individual's fpl
# the `max_lvl` variable is not stored on the main `x` object
# because it does not have much utility for anything aside from chip-financing analyses
# # # # # # # # # # # # # # # # # # # #
# construct potential marketplace hiu #
# # # # # # # # # # # # # # # # # # # #
x <- potential_marketplace_hiu_construction_fun( x )
# # # # # # # # # # # # # # # # # # # # # # # # # # #
# add age factors and then premiums to each record #
# # # # # # # # # # # # # # # # # # # # # # # # # # #
# copy variables to their cps names
x[ , 'h_year' ] <- year
x[ , 'gestfips' ] <- x[ , 'st' ]
x[ , 'a_age' ] <- x[ , 'agep' ]
x <- add_age_factors( x , aca_age_factors_fn )
# # # # # # # # # # # # # # # # # #
# calculate hiu-wide slcspremiums #
# # # # # # # # # # # # # # # # # #
x <- aggregate_premiums( x )
# remove cps variable copies
x[ , 'h_year' ] <- x[ , 'gestfips' ] <- x[ , 'a_age' ] <- NULL
# # # # # # # # # # # # # #
# calculate premium caps #
# # # # # # # # # # # # # #
x <- add_aca_premium_caps( x )
# # # # # # # # # # # # # # # # #
# calculate subsidy eligibility #
# # # # # # # # # # # # # # # # #
x <-
transform(
x ,
mp_aca_subsidy = pmax( no.na( mp_aca_slcsp - aca_capped_prem , 0 ) , 0 ) ,
mp_aca_subsidy_per_person = pmax( no.na( mp_aca_slcsp - aca_capped_prem , 0 ) / mpsize , 0 ) ,
mp_arpa_subsidy = pmax( no.na( mp_aca_slcsp - arpa_capped_prem , 0 ) , 0 ) ,
mp_arpa_subsidy_per_person = pmax( no.na( mp_aca_slcsp - arpa_capped_prem , 0 ) / mpsize , 0 )
)
# # # # # # # # # # # # # # #
# calculate aca eligibility #
# # # # # # # # # # # # # # #
# copy variables to their cps names
x[ , 'peinusyr' ] <- x[ , 'yoep' ]
x[ , 'prcitshp' ] <- ifelse( x[ , 'cit' ] == 5 , 5 , 1 )
x[ , 'a_age' ] <- x[ , 'agep' ]
x <- add_aca_eligibility( x , entered_five_years_ago = seq( year - 5 , year - 1 ) )
# remove cps variable copies
x[ , 'prcitshp' ] <- x[ , 'peinusyr' ] <- x[ , 'a_age' ] <- NULL
# # # # # # # # # # # # # # # # #
# identifiers need state fips code added so they're distinct across states
x[ , 'tax_id' ] <- paste( x[ , 'st' ] , x[ , 'tax_id' ] )
x[ , 'mp_id' ] <- paste( x[ , 'st' ] , x[ , 'mp_id' ] )
x[ , 'census_of_id' ] <- paste( x[ , 'st' ] , x[ , 'census_of_id' ] )
saveRDS( x , file = paste0( acs_output_folder , "/" , year - 1 , " " , this_state , " " , this_i , " eligibility 1yr.rds" ) , compress = FALSE )
invisible( NULL )
}
# states_to_process <- c( 'dc' , 'vt' , 'wy' , 'ak' , 'sd' , 'hi' )
# states_to_process <- 'vt'
# all states + DC WITH puerto rico
states_to_process <- tolower( c( 'DC' , 'PR' , state.abb ) )
big_states <- c( 'ca' , 'tx' , 'ny' , 'fl' , 'il' , 'pa' )
# make sure big states start processing first
states_to_process <- states_to_process[ order( states_to_process %in% big_states , decreasing = TRUE ) ]
cl <- makeCluster( 10 , type = "SOCK" )
print(
system.time(
clusterApplyLB(
cl ,
states_to_process ,
state_preimputation
)
)
)
stopCluster( cl )
# all states + DC WITHOUT puerto rico
states_to_process <- setdiff( states_to_process , 'pr' )
# append one thru five to each state
states_x_implicates_to_process <- levels( interaction( states_to_process , 1:5 , sep = '' ) )
# make sure big states start processing first
states_x_implicates_to_process <- states_x_implicates_to_process[ order( substr( states_x_implicates_to_process , 1 , 2 ) %in% big_states , decreasing = TRUE ) ]
cl <- makeCluster( 10 * 5 , type = "SOCK" )
print(
system.time(
clusterApplyLB(
cl ,
states_x_implicates_to_process ,
state_postimputation
)
)
)
stopCluster( cl )
# # # # # # # # # # # # # # # # # # # # #
# save five implicates as parquet files #
# # # # # # # # # # # # # # # # # # # # #
stack_and_save_one_implicate <-
function( this_implicate ){
source( "S:/Users/AnthonyD/Current Population Survey/repository/settings.R" )
# all states without puerto rico
states_to_process <- tolower( c( 'DC' , state.abb ) )
rds_filepaths <-
paste0(
acs_output_folder ,
"/" ,
( year - 1 ) ,
" " ,
states_to_process ,
" " ,
this_implicate ,
" eligibility 1yr.rds"
)
parquet_filepath <-
paste0(
acs_output_folder ,
"/" ,
( year - 1 ) ,
" us " ,
this_implicate ,
" eligibility 1yr.parquet"
)
w <- lapply( rds_filepaths , readRDS )
x <- do.call( rbind , w )
write_parquet( x , parquet_filepath )
invisible( NULL )
}
# repeat this for all five implicates
cl <- makeCluster( 5 , type = "SOCK" )
print(
system.time(
clusterApplyLB(
cl ,
1:5 ,
stack_and_save_one_implicate
)
)
)
stopCluster( cl )