Repository navigation
Expand file tree
/
Copy pathcps.R
More file actions
740 lines (484 loc) · 20.5 KB
/
Copy pathcps.R
File metadata and controls
740 lines (484 loc) · 20.5 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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
# source( "S:/Users/AnthonyD/Current Population Survey/repository/cps.R" , echo = TRUE )
microdata_name <- 'cps'
source( "S:/Users/AnthonyD/Current Population Survey/repository/settings.R" )
# # # # # # # # # # # # # #
source( paste0( code_folder , "load cbsa and necta to county crosswalks.R" ) )
source( paste0( code_folder , "census replication functions.R" ) )
source( paste0( code_folder , "kff recodes.R" ) )
source( paste0( code_folder , "unauthorized imputation functions.R" ) )
source( paste0( code_folder , "hiu 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( cps_rds_fn )
x <- x[ order( x[ , 'h_seq' ] , x[ , 'pppos' ] ) , ]
x[ , 'one' ] <- 1
# starting with the 2020 cps-asec,
# use ESIOFFER rather than PEOFFER
if( year >= 2020 ){
esi_vars <- grep('^esi|^i_esi',names(x),value=T)
pewn_vars <- gsub( "esi" , "pewn" , esi_vars )
pewn_vars <- gsub( "pewncould" , "pecould" , pewn_vars )
pewn_vars <- gsub( "pewnoffer" , "peoffer" , pewn_vars )
stopifnot( all( pewn_vars %in% names(x) ) )
for( i in seq_along( esi_vars ) ) x[ , pewn_vars[ i ] ] <- x[ , esi_vars[ i ] ]
}
if( redesign ){
# if pepar1==-1 then use pepar2
x[ , 'pepar1a' ] <- x[ , 'pepar1' ]
x[ , 'pepar2a' ] <- x[ , 'pepar2' ]
one_minus <- x[ , 'pepar1a' ] == -1
x[ one_minus , 'pepar1a' ] <- x[ one_minus , 'pepar2' ]
x[ one_minus , 'pepar2a' ] <- x[ one_minus , 'pepar1' ]
# redesigned file has both parents in pepar1 & pepar2
children <- x[ x[ , 'pepar1a' ] > 0 | x[ , 'pepar2a' ] > 0 , c( 'h_seq' , 'a_lineno' , 'a_sex' , 'pepar1a' , 'pepar2a' ) ]
moms_par1 <- unique( merge( x[ x[ , 'a_sex' ] == 2 , c( 'h_seq' , 'a_lineno' ) ] , children[ , c( 'h_seq' , 'pepar1a' ) ] , by.x = c( 'h_seq' , 'a_lineno' ) , by.y = c( 'h_seq' , 'pepar1a' ) ) )
dads_par1 <- unique( merge( x[ x[ , 'a_sex' ] == 1 , c( 'h_seq' , 'a_lineno' ) ] , children[ , c( 'h_seq' , 'pepar1a' ) ] , by.x = c( 'h_seq' , 'a_lineno' ) , by.y = c( 'h_seq' , 'pepar1a' ) ) )
moms_par1[ , 'moms_par1' ] <- TRUE
dads_par1[ , 'dads_par1' ] <- TRUE
x <- merge( x , moms_par1 , all.x = TRUE , by.x = c( 'h_seq' , 'pepar1a' ) , by.y = c( 'h_seq' , 'a_lineno' ) )
x <- merge( x , dads_par1 , all.x = TRUE , by.x = c( 'h_seq' , 'pepar1a' ) , by.y = c( 'h_seq' , 'a_lineno' ) )
for( i in c( 'moms_par1' , 'dads_par1' ) ) x[ is.na( x[ , i ] ) , i ] <- FALSE
x <- x[ order( x[ , 'h_seq' ] , x[ , 'pppos' ] ) , ]
x[ x[ , 'moms_par1' ] , 'pelnmom' ] <- x[ x[ , 'moms_par1' ] , 'pepar1a' ]
x[ x[ , 'moms_par1' ] , 'pelndad' ] <- x[ x[ , 'moms_par1' ] , 'pepar2a' ]
x[ x[ , 'dads_par1' ] , 'pelndad' ] <- x[ x[ , 'dads_par1' ] , 'pepar1a' ]
x[ x[ , 'dads_par1' ] , 'pelnmom' ] <- x[ x[ , 'dads_par1' ] , 'pepar2a' ]
x[ is.na( x[ , 'pelnmom' ] ) , 'pelnmom' ] <- -1
x[ is.na( x[ , 'pelndad' ] ) , 'pelndad' ] <- -1
for( i in c( 'moms_par1' , 'dads_par1' , 'pepar1a' , 'pepar2a' ) ) x[ , i ] <- NULL
# exclude infants not born during year minus one
x <- subset( x , cov > 0 )
# remove tax_id and tax_inc
x[ , 'original_tax_id' ] <- x[ , 'tax_id' ] ; x[ , 'tax_id' ] <- NULL
x[ , 'original_tax_inc' ] <- x[ , 'tax_inc' ] ; x[ , 'tax_inc' ] <- NULL
}
# hardcode family structure to fit function
if( year == 2024 ){
x[ x[ , 'h_seq' ] %in% '14964' & x[ , 'a_lineno' ] %in% 5 , 'pelndad' ] <- -1
x[ x[ , 'h_seq' ] %in% '17915' & x[ , 'a_lineno' ] %in% 6 , 'pelndad' ] <- -1
x[ x[ , 'h_seq' ] %in% '59737' & x[ , 'a_lineno' ] %in% 8 , 'pelndad' ] <- -1
}
# hardcode family structure to fit function
if( year == 2025 ){
x[ x[ , 'h_seq' ] %in% '45278' & x[ , 'a_lineno' ] %in% 4 , 'pelndad' ] <- 2
x[ x[ , 'h_seq' ] %in% '45278' & x[ , 'a_lineno' ] %in% 4 , 'pelnmom' ] <- 5
x[ x[ , 'h_seq' ] %in% '56398' & x[ , 'a_lineno' ] %in% 3 , 'pelndad' ] <- 1
x[ x[ , 'h_seq' ] %in% '56398' & x[ , 'a_lineno' ] %in% 3 , 'pelnmom' ] <- 2
}
# calculate weighted premiums for metro areas
source( paste0( code_folder , "premiums for cps geographies.R" ) )
before_nrow <- nrow( x )
x <- merge( x , wtd_prems )
stopifnot( nrow( x ) == before_nrow )
# 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( gestfips %in% exp_states ) + 1 , labels = c( 'non-expansion state' , 'expansion state' ) ) ,
state_name =
factor(
match( gestfips , medicaid_fpl[ , 'gestfips' ] ) ,
levels = 1:51 ,
labels = medicaid_fpl[ -52 , 'statename' ] # exclude PR
) ,
stateab =
factor(
match( gestfips , medicaid_fpl[ , 'gestfips' ] ) ,
levels = 1:51 ,
labels = medicaid_fpl[ -52 , 'stateab' ] # exclude PR
)
)
# merge cbsa names onto the tables
before_nrow <- nrow( x )
x <- merge( x , stacked_cbsa_names , all.x = TRUE )
stopifnot( nrow( x ) == before_nrow )
x <- x[ order( x[ , 'h_seq' ] , x[ , 'pppos' ] ) , ]
# 100% of the missing cbsa_name fields have a zero for `gtcbsa`
stopifnot( all( x[ is.na( x[ , 'cbsa_name' ] ) , 'gtcbsa' ] == 0 ) )
x[ , 'state_name_cbsa_name' ] <- factor( ifelse( is.na( x[ , 'cbsa_name' ] ) , NA , paste( x[ , 'state_name' ] , x[ , 'cbsa_name' ] ) ) )
x[ , 'cbsa_name' ] <- factor( x[ , 'cbsa_name' ] )
# # # # # # # # # # # # # # # # # # # #
# add linear census poverty variable #
# # # # # # # # # # # # # # # # # # # #
# store the number of records prior to the merge
before_nrow <- nrow( x )
# calculate the within-family total income, excluding ftype==3
# ftype =3 is "related subfamily"
faminc <- unique( x[ !( x[ , 'ftype' ] %in% 3 ) , c( 'h_seq' , 'pf_seq' , 'ftotval' ) ] )
names( faminc )[ ncol( faminc ) ] <- 'ftotval_no3'
# merge the non-ftype==3 values back on to the ftype==3 values within each household and family
x <- merge( x , faminc )
# confirm the number of records has not changed
stopifnot( nrow( x ) == before_nrow )
# construct a straight linear poverty variable
x[ , 'census_pov' ] <- x[ , 'ftotval_no3' ] / x[ , 'fpovcut' ]
# re-create the `povll` variable already found on the file
x[ , 'census_povll' ] <- 1 + findInterval( x[ , 'census_pov' ] , c( seq( 0.5 , 2 , 0.25 ) , seq( 2.5 , 5 , 0.5 ) ) )
# confirm that less than one percent of records do not match -- these were top-coded in some income category
stopifnot( ( nrow( subset( x , povll != census_povll ) ) / nrow( x ) ) < 0.01 )
# # # # # # # # # # # # # # # # # # # # #
# add census health insurance variables #
# # # # # # # # # # # # # # # # # # # # #
if( redesign ) {
x <- census_health_insurance_redesign( x )
} else {
x <- census_health_insurance_pre_redesign( x )
}
census_check( x , year , census_fn )
# # # # # # # # # # #
# add worker offer #
# # # # # # # # # # #
x <- add_census_worker_offer( x )
# # # # # # # # # # # # # # # # # # # #
# add kff health insurance hierarchy #
# # # # # # # # # # # # # # # # # # # #
x <- cps_health_insurance( x , redesign )
# # # # # # # # # # # # # # #
# add citizenship variable #
# # # # # # # # # # # # # # #
x[ , 'kff_primer_citshp_4' ] <-
factor(
ifelse( x[ , 'prcitshp' ] %in% 1:3 , 1 ,
ifelse( x[ , 'prcitshp' ] %in% 4 , 2 ,
ifelse( x[ , 'peinusyr' ] %in% efya , 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 parent variable #
# # # # # # # # # # # #
x <- add_parent( x , microdata_name = 'cps' )
# # # # # # # # # # # # # #
# add family work status #
# # # # # # # # # # # # # #
x <- add_fws( x )
# # # # # # # # # # # # #
# add family structure #
# # # # # # # # # # # # #
x <- add_famtype( x )
# # # # # # # # # # # # #
# add primer variables #
# # # # # # # # # # # # #
x <- primer_recodes( x , microdata_name = 'cps' )
# # # # # # # # # # # # # # # # #
# save the unimputed data.frame #
# # # # # # # # # # # # # # # # #
# enforce sort order so the random seed does not change
x <- x[ order( x [ , 'h_seq' ] , x[ , 'pppos' ] ) , ]
saveRDS( x , file = paste0( acs_output_folder , "/" , year , " cps asec unimputed.rds" ) , compress = FALSE )
# # # # # # # # # # # # # # # # # # # #
# prepare multiply-imputed variables #
# # # # # # # # # # # # # # # # # # # #
# cps-asec 2014 (weighting year 2013) has ten implicates for some income categories
# re-calculate total income & poverty for all ten implicates
if ( year == 2014 ){
mi_vars <- c( 'census_pov' , 'ftotval_no3' , 'ptotval' , 'int_val' , 'div_val' , 'rtm_val' )
for ( i in mi_vars ){
x[ , paste0( i , 1 ) ] <- x[ , i ]
x[ , i ] <- NULL
}
for ( j in 2:10 ){
x[ , paste0( 'ptotval' , j ) ] <- x[ , 'ptotval1' ]
for ( i in c( 'int_val' , 'div_val' , 'rtm_val' ) ){
x[ , paste0( i , j ) ] <- x[ , paste0( i , 1 ) ]
x[ x[ , 'icflag' ] %in% 1 , paste0( i , j ) ] <- x[ x[ , 'icflag' ] %in% 1 , paste0( i , '_ic' , j ) ]
}
x[ x[ , 'icflag' ] %in% 1 , paste0( 'ptotval' , j ) ] <-
x[ x[ , 'icflag' ] %in% 1 , 'ptotval1' ] -
x[ x[ , 'icflag' ] %in% 1 , 'int_val1' ] -
x[ x[ , 'icflag' ] %in% 1 , 'div_val1' ] -
x[ x[ , 'icflag' ] %in% 1 , 'rtm_val1' ] +
x[ x[ , 'icflag' ] %in% 1 , paste0( 'int_val_ic' , j ) ] +
x[ x[ , 'icflag' ] %in% 1 , paste0( 'div_val_ic' , j ) ] +
x[ x[ , 'icflag' ] %in% 1 , paste0( 'rtm_val_ic' , j ) ]
xtemp <- x[ , c( 'h_seq' , 'pf_seq' , 'ftype' , paste0( 'ptotval' , j ) ) ]
faminc <-
sqldf::sqldf(
paste0(
"select
h_seq,
pf_seq ,
sum( ptotval" , j , " ) as ftotval_no3" , j ,
" from xtemp
where ftype != 3
group by
h_seq ,
pf_seq"
)
)
rm( xtemp )
x <- merge( x , faminc )
x[ , paste0( 'census_pov' , j ) ] <-
x[ , paste0( 'ftotval_no3' , j ) ] / x[ , 'fpovcut' ]
}
} else {
# # # # # # # # # # # # #
# add retirement income #
# # # # # # # # # # # # #
if( !( 'rtm_val' %in% names( x ) ) ){
x[ , 'rtm_val' ] <- rowSums( x[ , c( 'dst_val1' , 'dst_val1_yng' , 'dst_val2' , 'dst_val2_yng' ) ] )
}
mi_vars <- NULL
}
# # # # # # # # # # # #
# impute unauthorized #
# # # # # # # # # # # #
x <- x[ order( x[ , 'h_seq' ] , x[ , 'a_lineno' ] ) , ]
set.seed( year )
ua_result <-
impute_unauthorized(
x ,
dataset_named_year = year ,
microdata_name = "cps" ,
n = 10
)
# combine all ten data.frame objects into a list
xl <-
lapply(
1:10 ,
function( i ){
if( is.null( mi_vars ) ){
xu <- ua_result[ , c( 'h_seq' , 'pppos' , paste0( 'unauthorized' , i ) ) ]
} else {
xu <-
merge(
ua_result[ , c( 'h_seq' , 'pppos' , paste0( 'unauthorized' , i ) ) ] ,
x[ , c( 'h_seq' , 'pppos' , paste0( mi_vars , i ) ) ]
)
}
names( xu )[ -1:-2 ] <- c( 'unauthorized' , mi_vars )
rem <- ec( c( mi_vars , 'unauthorized' ) , 1:10 )
merge( x[ , !( names( x ) %in% rem ) ] , xu )
}
)
# # # # # # # # # # # # # # # # # #
# initiate a survey design object #
# # # # # # # # # # # # # # # # # #
cps_design <-
svrepdesign(
weights = ~ marsupwt,
repweights = "pwwgt[1-9]",
type = "Fay",
rho = ( 1 - 1 / sqrt( 4 ) ) ,
mse = TRUE ,
data = x
)
# # # # # # # # # # # #
# impute worker offer #
# # # # # # # # # # # #
source( paste0( code_folder , "worker offer regression creation.R" ) )
source( paste0( code_folder , "worker offer regression functions.R" ) )
xl <-
lapply(
xl ,
function( w ) w[ order( w[ , 'h_seq' ] , w[ , 'a_lineno' ] ) , ]
)
set.seed( year )
xl <-
lapply(
xl ,
impute_offer ,
prediction_model = worker_offer_regression ,
weighted_sample_to_switch = offer_removals_to_sample
)
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# expand offer variable to all policyholders, including retired #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
xl <-
lapply(
xl ,
function( z ){
# if a worker has an offer or if the individual is an ESI policyholder despite not working,
# then the individual has an offer of ESI
z[ , 'kff_imputed_offer_of_esi' ] <- as.numeric( z[ , 'kff_imputed_worker_offer' ] %in% 1 | z[ , 'census_own_employment' ] %in% 1 )
z
}
)
# # # # # # # # # # # # #
# add primer variables #
# # # # # # # # # # # # #
xl <- lapply( xl , cps_mi_recodes )
# # # # # # # # # # # # # # # #
# add medicaid poverty levels #
# # # # # # # # # # # # # # # #
cl <- makeCluster( 10 , type = "SOCK" )
caidl <-
clusterApplyLB(
cl ,
xl ,
create_hiu ,
irs_year = year - 1 ,
hhspov_year = year - 1 ,
microdata_name = 'cps' ,
program = 'medicaid' ,
income_period = 'annualized' ,
irs = irs_ceilings ,
hhs = hhs_poverty ,
the_seed = year - 1 ,
income_inflation_factor = 1 ,
blank_unauthorized = FALSE ,
randomly_sample_richest = randomly_sample_richest_fun
)
xl <- mapply( rnam , xl , caidl , MoreArgs = list( n = 'caid' ) , SIMPLIFY = FALSE )
# # # # # # # # # # # # # # # # # # # #
# add tax filing unit poverty levels #
# # # # # # # # # # # # # # # # # # # #
txl <-
clusterApplyLB(
cl ,
xl ,
create_hiu ,
irs_year = myear + 1 ,
hhspov_year = myear ,
microdata_name = 'cps' ,
program = 'aptc' ,
keep_hiuid = TRUE ,
keep_pqcpqr = TRUE ,
ssi_blank = FALSE ,
income_period = 'annualized' ,
irs = irs_ceilings ,
hhs = hhs_poverty ,
the_seed = myear + 1 ,
income_inflation_factor = miif ,
blank_unauthorized = TRUE ,
randomly_sample_richest = randomly_sample_richest_fun
)
xl <- mapply( rnam , xl , txl , MoreArgs = list( n = 'tax' ) , SIMPLIFY = FALSE )
stopCluster( cl )
# # # # # # # # # # # #
# construct offer hiu #
# # # # # # # # # # # #
xl <-
lapply(
xl ,
function( z ){
z[ , 'of_id' ] <- z[ , '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 <- !( z[ , 'eligible_qc' ] ) & !( z[ , 'tax_claimant' ] %in% 1 ) & !is.na( z[ , 'of_id' ] )
# make them into their own HIU
z[ rr , 'of_id' ] <-
paste(
z[ rr , 'of_id' ] ,
z[ rr , 'a_lineno' ]
)
# give undocumenteds a census_of_id
# because they could have an offer at work
z[ , 'census_of_id' ] <- z[ , 'of_id' ]
z[ is.na( z[ , 'census_of_id' ] ) , 'census_of_id' ] <-
paste(
z[ is.na( z[ , 'census_of_id' ] ) , 'h_seq' ] ,
z[ is.na( z[ , 'census_of_id' ] ) , 'a_lineno' ]
)
z[ , 'of_id' ] <- NULL
z
}
)
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# distribute the census offer variable to everyone in the offer-unit #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
xl <-
lapply(
xl ,
function( z ){
before_nrow <- nrow( z )
z[ , 'kff_imputed_affordable_offer_of_esi' ] <-
as.numeric(
# the worker is a policyholder, the offer is affordable OR
( z[ , 'census_own_employment' ] %in% 1 ) |
# the worker has an imputed offer AND filing unit's poverty level is at or above 250% fpl
( ( z[ , 'kff_imputed_offer_of_esi' ] == 1 ) & no.na( z[ , 'taxpov' ] >= 2.5 ) )
)
# construct a data.frame with one-record-per-offer-unit containing a
# yes/no for any offer throughout the offer-unit
unit_level <- aggregate( kff_imputed_affordable_offer_of_esi ~ census_of_id , z , max )
names( unit_level )[2] <- "kff_imputed_unit_offer"
z <- merge( z , unit_level )
stopifnot( nrow( z ) == before_nrow )
z
}
)
# # # # # # # # # # # # # # # # # # # # # #
# imputed unit offer regression creation #
# # # # # # # # # # # # # # # # # # # # # #
source( paste0( code_folder , "hiu offer regression creation.R" ) , echo = TRUE )
# # # # # # # # # # # # # # # # # # # #
# add medicaid eligibility thresholds #
# # # # # # # # # # # # # # # # # # # #
xl <- lapply( xl , add_medicaid_eligibility , entered_five_years_ago = efya , mcfpl = medicaid_fpl )
# # # # # # # # # # # # # # # # # #
# add chip eligibility thresholds #
# # # # # # # # # # # # # # # # # #
xl <- lapply( xl , add_chip_eligibility , entered_five_years_ago = efya , chfpl = chip_fpl )
# confirm that the maximum of schip + mchip + title xix
# produces the same result as caid_lvl for all children. no exceptions.
y <- subset( xl[[1]] , a_age %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 `xl` object
# because it does not have much utility for anything aside from chip-financing analyses
# # # # # # # # # # # # # # # # # # # #
# construct potential marketplace hiu #
# # # # # # # # # # # # # # # # # # # #
xl <- lapply( xl , potential_marketplace_hiu_construction_fun )
# # # # # # # # # # # # # # # # # # # # # # # # # # #
# add age factors and then premiums to each record #
# # # # # # # # # # # # # # # # # # # # # # # # # # #
xl <- lapply( xl , add_age_factors , age_factor_path = aca_age_factors_fn )
# # # # # # # # # # # # # # # # # #
# calculate hiu-wide slcspremiums #
# # # # # # # # # # # # # # # # # #
xl <- lapply( xl , aggregate_premiums )
# # # # # # # # # # # # # #
# calculate premium caps #
# # # # # # # # # # # # # #
xl <- lapply( xl , add_aca_premium_caps )
# # # # # # # # # # # # # # # # #
# calculate subsidy eligibility #
# # # # # # # # # # # # # # # # #
xl <-
lapply(
xl ,
function( z ){
transform(
z ,
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 #
# # # # # # # # # # # # # # #
xl <- lapply( xl , add_aca_eligibility , entered_five_years_ago = efya )
# # # # # # # # # # # # # # # # # #
# save the ten data.frame objects #
# # # # # # # # # # # # # # # # # #
saveRDS( xl , file = paste0( acs_output_folder , "/" , year , " cps asec imputed.rds" ) , compress = FALSE )