-
Notifications
You must be signed in to change notification settings - Fork 1
/
Analysis Main Clean.R
1874 lines (1430 loc) · 88.1 KB
/
Analysis Main Clean.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
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
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# The R-code provided on this repository falls under GNU
# GENERAL PUBLIC LICENSE (detailed below).
#
# GNU GENERAL PUBLIC LICENSE
# Version 3, 29 June 2007
#
# Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
# Everyone is permitted to copy and distribute verbatim copies
# of this license document, with due reference to the authors,
# but changing it is not allowed.
# for full authoritative version, see fsf.org
# make model formulas
formula.outcome <- c("outcome ~
## constants ##
momafb +
momnsib +
momorder +
famtype_1 +
# urban.urban +
urban.semi + urban.rural +
# famtype.married +
famtype.cohab + famtype.lone +
famtype.notwithpar +
pedu.pri + pedu.sec +
# pedu.tertlow +
pedu.terthi +
punempl +
phouse.owner +
phhinc.1 + phhinc.2 +
# phhinc.3 +
phhinc.4 + phhinc.5 +
## time-varying variables ##
# age.1619 +
(age.2023 + age.2427 +
age.2830 + age.3133) *
(l.edu.pri + l.edu.sec +
# l.edu.tertlow +
l.edu.terthi +
l.enrol +
l.enrolc) +
# age.1619 +
(age.2023 + age.2427 +
age.2830 + age.3133) * l.parent +
l.entryc +
# l.partner.single +
l.partner.cohab + l.partner.married +
l.empl +
l.cumu.empl +
l.unempl +
l.inc +
l.cumu.inc +
l.inc.res +
# l.house.parents +
l.house.owner + l.house.tenant +
l.house.other +
l.birth +
tsfb
")
# formula.edu is used for education outcomes
formula.edu <- c("outcome ~
## constants ##
momafb +
momnsib +
momorder +
famtype_1 +
# urban.urban +
urban.semi + urban.rural +
# famtype.married +
famtype.cohab + famtype.lone +
famtype.notwithpar +
pedu.pri + pedu.sec +
# pedu.tertlow +
pedu.terthi +
punempl +
phouse.owner +
phhinc.1 + phhinc.2 +
# phhinc.3 +
phhinc.4 + phhinc.5 +
## time-varying variables ##
# age.1619 +
(age.2023 + age.2427 +
age.2830 + age.3133) *
(l.edu.pri + l.edu.sec +
# l.edu.tertlow +
l.edu.terthi +
enrol +
l.enrol +
l.enrolc) +
# age.1619 +
(age.2023 + age.2427 +
age.2830 + age.3133) * l.parent +
l.entryc +
# l.partner.single +
l.partner.cohab + l.partner.married +
l.empl +
l.cumu.empl +
l.unempl +
l.inc +
l.cumu.inc +
l.inc.res +
# l.house.parents +
l.house.owner + l.house.tenant +
l.house.other +
l.birth +
tsfb
")
formula.birth <- as.formula(sub('outcome','birth',formula.outcome))
formula.edu.pri <- as.formula(sub('outcome','edu.pri',formula.edu))
formula.edu.sec <- as.formula(sub('outcome','edu.sec',formula.edu))
formula.edu.tertlowst <- as.formula(sub('outcome','edu.tertlowst',formula.edu))
formula.edu.tertlow <- as.formula(sub('outcome','edu.tertlow',formula.edu))
formula.edu.terthi <- as.formula(sub('outcome','edu.terthi',formula.edu))
formula.enrol <- as.formula(sub('outcome','enrol',formula.outcome))
formula.partner.single <- as.formula(sub('outcome','partner.single',formula.outcome))
formula.partner.cohab <- as.formula(sub('outcome','partner.cohab',formula.outcome))
formula.partner.married <- as.formula(sub('outcome','partner.married',formula.outcome))
formula.empl <- as.formula(sub('outcome','empl',formula.outcome))
formula.unempl <- as.formula(sub('outcome','unempl',formula.outcome))
formula.inc <- as.formula(sub('outcome','inc',formula.outcome))
formula.hhinc <- as.formula(sub('outcome','inc.res',formula.outcome))
formula.house.parents <- as.formula(sub('outcome','house.parents',formula.outcome))
formula.house.owner <- as.formula(sub('outcome','house.owner',formula.outcome))
formula.house.tenant <- as.formula(sub('outcome','house.tenant',formula.outcome))
formula.house.other <- as.formula(sub('outcome','house.other',formula.outcome))
# deterministic time varying values:
# age.cent # deterministically dependent on time
# enrolc # deterministically dependent on enrol
# parent # deterministically dependent on birth
# tsfb # deterministically dependent on birth and time
# entryc # deterministically dependent on enrol
# predict functions needed
multinomial.predict.5var <- function(predict.x1,predict.x2,predict.x3,
predict.x4,predict.x5,
dataset) {
x1 <- predict(predict.x1,dataset,type='response')
x2 <- predict(predict.x2,dataset,type='response')
x3 <- predict(predict.x3,dataset,type='response')
x4 <- predict(predict.x4,dataset,type='response')
x5 <- predict(predict.x5,dataset,type='response')
# find rescale value
rescaler <- 1/(x1+x2+x3+x4+x5)
# rescale constituent variables
x1r <- x1*rescaler
x2r <- x2*rescaler
x3r <- x3*rescaler
x4r <- x4*rescaler
# x5r does not need to be determined
# determine boundary values
x1b <- x1r
x2b <- x1r + x2r
x3b <- x1r + x2r + x3r
x4b <- x1r + x2r + x3r + x4r
# x5b does not need to be determined
# determine n
n2 <- length(x1)
# draw random variables
decider <- runif(n2,0,1)
# determine category
x1c <- ifelse(decider <= x1b,1,0)
x2c <- ifelse(decider > x1b & decider <= x2b,1,0)
x3c <- ifelse(decider > x2b & decider <= x3b,1,0)
x4c <- ifelse(decider > x3b & decider <= x4b,1,0)
x5c <- ifelse(decider > x4b,1,0)
return(cbind(x1c,x2c,x3c,x4c,x5c))
}
multinomial.predict.4var <- function(predict.x1,predict.x2,
predict.x3,predict.x4,
dataset) {
x1 <- predict(predict.x1,dataset,type='response')
x2 <- predict(predict.x2,dataset,type='response')
x3 <- predict(predict.x3,dataset,type='response')
x4 <- predict(predict.x4,dataset,type='response')
# find rescale value
rescaler <- 1/(x1+x2+x3+x4)
# rescale constituent variables
x1r <- x1*rescaler
x2r <- x2*rescaler
x3r <- x3*rescaler
# x4r does not need to be determined
# determine boundary values
x1b <- x1r
x2b <- x1r + x2r
x3b <- x1r + x2r + x3r
# x4b does not need to be determined
# determine n
n2 <- length(x1)
# draw random variables
decider <- runif(n2,0,1)
# determine category
x1c <- ifelse(decider <= x1b,1,0)
x2c <- ifelse(decider > x1b & decider <= x2b,1,0)
x3c <- ifelse(decider > x2b & decider <= x3b,1,0)
x4c <- ifelse(decider > x3b,1,0)
return(cbind(x1c,x2c,x3c,x4c))
}
multinomial.predict.3var <- function(predict.x1,predict.x2,predict.x3,
dataset) {
x1 <- predict(predict.x1,dataset,type='response')
x2 <- predict(predict.x2,dataset,type='response')
x3 <- predict(predict.x3,dataset,type='response')
# find rescale value
rescaler <- 1/(x1+x2+x3)
# rescale constituent variables
x1r <- x1*rescaler
x2r <- x2*rescaler
# x3r does not need to be determined
# determine boundary values
x1b <- x1r
x2b <- x1r + x2r
# x3b does not need to be determined
# determine n
n2 <- length(x1)
# draw random variables
decider <- runif(n2,0,1)
# determine category
x1c <- ifelse(decider <= x1b,1,0)
x2c <- ifelse(decider > x1b & decider <= x2b,1,0)
x3c <- ifelse(decider > x2b,1,0)
return(cbind(x1c,x2c,x3c))
}
predict.lm.withvar <- function(predict.x1,dataset) {
# determine how many new values have to be created
n2 <- dim(dataset)[1]
# determine randomness around the mean predicted value
# I choose normally distributed residuals
# but I could choose another distribution
# normal makes sense since LM also assumes normality
newres <- rnorm(n2,mean=0,sd(predict.x1$residuals))
# make mean predictions
preds <- predict(predict.x1,dataset,
interval='none',type='response')
# add randomness on top and return the values
return(preds + newres)
}
binomial.predict.1var <- function(predict.x1,dataset) {
a <- predict(predict.x1,dataset,link='response')
p <- exp(a)/(exp(a)+1)
n2 <- length(a)
return(rbinom(n2,1,p))
}
# longitudinal sampling function
long.sample <- function(originaldata, originaldataid) {
# select a bunch of IDs
IDs <- unique(originaldataid)
y <- sample(IDs,length(IDs),replace=T)
z <- table(table(y))
# from there, select a group once
selectID <- sample(IDs,size=z[1],replace=F)
newdata <- originaldata[which(originaldataid %in% selectID),]
if(length(z) > 1) {
for(i in 2:length(z)) {
# select a new group of IDs that was not yet selected
IDs2 <- setdiff(IDs,selectID)
# from there, randomly select a group of people of the right size
selectID2 <- sample(IDs2,size=z[i],replace=F)
selectID <- c(selectID,selectID2) # so we don't re-select the newly
# selected people either
for(j in 1:i) {
# copy the new dataset i number of times
newdata <- rbind(newdata,originaldata[which(originaldataid %in% selectID2),])
}
}
return(newdata)
}
}
# save column location info
col.index.from <- NULL
col.index.from[1] <- grep(paste0('^','birth','$'), colnames(par.dat))
col.index.from[2] <- grep(paste0('^','edu.pri','$'), colnames(par.dat))
col.index.from[3] <- grep(paste0('^','edu.sec','$'), colnames(par.dat))
col.index.from[4] <- grep(paste0('^','edu.tertlowst','$'), colnames(par.dat))
col.index.from[5] <- grep(paste0('^','edu.tertlow','$'), colnames(par.dat))
col.index.from[6] <- grep(paste0('^','edu.terthi','$'), colnames(par.dat))
col.index.from[7] <- grep(paste0('^','enrol','$'), colnames(par.dat))
col.index.from[8] <- grep(paste0('^','enrolc','$'), colnames(par.dat))
col.index.from[9] <- grep(paste0('^','partner.single','$'), colnames(par.dat))
col.index.from[10] <- grep(paste0('^','partner.cohab','$'), colnames(par.dat))
col.index.from[11] <- grep(paste0('^','partner.married','$'), colnames(par.dat))
col.index.from[12] <- grep(paste0('^','empl','$'), colnames(par.dat))
col.index.from[13] <- grep(paste0('^','unempl','$'), colnames(par.dat))
col.index.from[14] <- grep(paste0('^','inc','$'), colnames(par.dat))
col.index.from[15] <- grep(paste0('^','inc.res','$'), colnames(par.dat))
col.index.from[16] <- grep(paste0('^','house.parents','$'), colnames(par.dat))
col.index.from[17] <- grep(paste0('^','house.owner','$'), colnames(par.dat))
col.index.from[18] <- grep(paste0('^','house.tenant','$'), colnames(par.dat))
col.index.from[19] <- grep(paste0('^','house.other','$'), colnames(par.dat))
col.index.from[20] <- grep(paste0('^','parent','$'), colnames(par.dat))
col.index.from[21] <- grep(paste0('^','tsfb','$'), colnames(par.dat))
col.index.from[22] <- grep(paste0('^','postpone','$'), colnames(par.dat))
col.index.from[23] <- grep(paste0('^','entryc','$'), colnames(par.dat))
col.index.from[24] <- grep(paste0('^','cumu.empl','$'), colnames(par.dat))
col.index.from[25] <- grep(paste0('^','cumu.inc','$'), colnames(par.dat))
col.index.to <- NULL
col.index.to[1] <- grep(paste0('^','l.birth','$'), colnames(par.dat))
col.index.to[2] <- grep(paste0('^','l.edu.pri','$'), colnames(par.dat))
col.index.to[3] <- grep(paste0('^','l.edu.sec','$'), colnames(par.dat))
col.index.to[4] <- grep(paste0('^','l.edu.tertlowst','$'), colnames(par.dat))
col.index.to[5] <- grep(paste0('^','l.edu.tertlow','$'), colnames(par.dat))
col.index.to[6] <- grep(paste0('^','l.edu.terthi','$'), colnames(par.dat))
col.index.to[7] <- grep(paste0('^','l.enrol','$'), colnames(par.dat))
col.index.to[8] <- grep(paste0('^','l.enrolc','$'), colnames(par.dat))
col.index.to[9] <- grep(paste0('^','l.partner.single','$'), colnames(par.dat))
col.index.to[10] <- grep(paste0('^','l.partner.cohab','$'), colnames(par.dat))
col.index.to[11] <- grep(paste0('^','l.partner.married','$'), colnames(par.dat))
col.index.to[12] <- grep(paste0('^','l.empl','$'), colnames(par.dat))
col.index.to[13] <- grep(paste0('^','l.unempl','$'), colnames(par.dat))
col.index.to[14] <- grep(paste0('^','l.inc','$'), colnames(par.dat))
col.index.to[15] <- grep(paste0('^','l.inc.res','$'), colnames(par.dat))
col.index.to[16] <- grep(paste0('^','l.house.parents','$'), colnames(par.dat))
col.index.to[17] <- grep(paste0('^','l.house.owner','$'), colnames(par.dat))
col.index.to[18] <- grep(paste0('^','l.house.tenant','$'), colnames(par.dat))
col.index.to[19] <- grep(paste0('^','l.house.other','$'), colnames(par.dat))
col.index.to[20] <- grep(paste0('^','l.parent','$'), colnames(par.dat))
col.index.to[21] <- grep(paste0('^','l.tsfb','$'), colnames(par.dat))
col.index.to[22] <- grep(paste0('^','l.postpone','$'), colnames(par.dat))
col.index.to[23] <- grep(paste0('^','l.entryc','$'), colnames(par.dat))
col.index.to[24] <- grep(paste0('^','l.cumu.empl','$'), colnames(par.dat))
col.index.to[25] <- grep(paste0('^','l.cumu.inc','$'), colnames(par.dat))
# make indicator for counterfactual fut
par.dat$cf.fut <- 0
# the order of the above variables in the index.from and index.to
# must be the same!
# and of course, the length of both vectors must also be the same
length(col.index.from)
length(col.index.to)
# save all the operations up to now
# save.image("U:/Collaboration/Nisen/Data/line 862.RData")
# bootstrap size
bssize <- 100
# monte carlo error reduction iteration number
msize <- 10
# output array
output.arr.ATT <- rep(NA,bssize*19*18)
dim(output.arr.ATT) <- c(bssize,19,18)
output.arr.ATT.3 <- output.arr.ATT.2 <- output.arr.ATT
output.arr.PA.3 <- output.arr.PA.2 <- output.arr.PA <- output.arr.ATT
youngparent.output.arr <- rep(NA,bssize*19*18)
dim(youngparent.output.arr) <- c(bssize,19,18)
youngparent.output.arr.3 <- youngparent.output.arr.2 <- youngparent.output.arr.mediation <- youngparent.output.arr
# Monte Carlo Error reduction arrays
monte.arr.ATT <- rep(NA,msize*19*18)
dim(monte.arr.ATT) <- c(msize,19,18)
monte.arr.ATT.3 <- monte.arr.ATT.2 <- monte.arr.ATT
monte.arr.PA <- monte.arr.PA.2 <- monte.arr.PA.3 <- monte.arr.ATT
youngparent.monte.arr <- rep(NA,msize*19*18)
dim(youngparent.monte.arr) <- c(msize,19,18)
youngparent.monte.arr.mediation <- youngparent.monte.arr
youngparent.monte.timetest <- matrix(rep(NA,msize*19),ncol=19)
youngparent.timetest <- matrix(rep(NA,bssize*19),ncol=19)
youngparent.timetest.mediation <- youngparent.timetest
youngparent.monte.timetest.mediation <- youngparent.monte.timetest
# perform the analysis for women or for men?
par.dat <- par.dat[par.dat$female==1,] # 0 = men, 1 = women
t1 <- Sys.time()
for(bs in 1:bssize) {
# sample individuals from par.dat
par.sample <- long.sample(par.dat,par.dat$id)
# (re)fit models to par.sample
# bsf = bootstrap fit
# estimate relations
fit.bsf.birth <- glm(formula.birth, family=binomial,subset=par.dat$tsfb==0, data=par.sample)
fit.bsf.edu.pri <- glm(formula.edu.pri, family=binomial, data=par.sample)
fit.bsf.edu.sec <- glm(formula.edu.sec, family=binomial, data=par.sample)
fit.bsf.edu.tertlow <- glm(formula.edu.tertlow, family=binomial, data=par.sample)
fit.bsf.edu.terthi <- glm(formula.edu.terthi, family=binomial, data=par.sample)
fit.bsf.enrol <- glm(formula.enrol, family=binomial, data=par.sample)
fit.bsf.partner.single <- glm(formula.partner.single, family=binomial, data=par.sample)
fit.bsf.partner.cohab <- glm(formula.partner.cohab, family=binomial, data=par.sample)
fit.bsf.partner.married <- glm(formula.partner.married, family=binomial, data=par.sample)
fit.bsf.empl <- glm(formula.empl, family=binomial, data=par.sample)
fit.bsf.unempl <- glm(formula.unempl, family=binomial, data=par.sample)
fit.bsf.inc <- lm(formula.inc, data=par.sample)
fit.bsf.hhinc <- lm(formula.hhinc, data=par.sample)
fit.bsf.house.parents <- glm(formula.house.parents, family=binomial, data=par.sample)
fit.bsf.house.owner <- glm(formula.house.owner, family=binomial, data=par.sample)
fit.bsf.house.tenant <- glm(formula.house.tenant, family=binomial, data=par.sample)
fit.bsf.house.other <- glm(formula.house.other, family=binomial, data=par.sample)
# take individuals at fut==1
# they get a special dataset here since we exclude people later
par.sample.fut1 <- par.sample[par.sample$fut==1,]
for(m in 1:msize) {
## this is the Monte Carlo Error reduction loop ##
# I take the average over a few iterations
# since each iteration has variation that is not due to
# sampling variability, but due to probablistic predictions
# and we are not interested in that type of error
# take individuals at fut==1
par.sample <- par.sample.fut1
# give each individual a new ID, since
# otherwise the ordering below will go wrong when
# individuals are ordered by ID and time (since
# due to resampling with replacement, multiple individuals
# can have the same ID)
par.sample$idnr <- 1:length(par.sample$id)
# and the same file, but then for the intervention loop
par.sample.3 <- par.sample.2 <- par.sample
par.sample$group <- 0
## NATURAL LOOP ##
# start a loop that moves through the follow-up time units
for(t in 2:17) {
par.sample.temp <- par.sample[par.sample$fut==(t-1) & par.sample$cens==0,]
par.sample.temp$fut <- par.sample.temp$fut+1
par.sample.temp$year <- par.sample.temp$year+1
par.sample.temp$age <- par.sample.temp$age+1
par.sample.temp$age.cent <- par.sample.temp$age.cent+1
# lag values: since the new values for t have not yet been produced
# I can actually take the column information from a row at t
# and put it in the same row at t but then in a column meant for
# lagged values
# the first are the 'from' columns, and the second the 'to' columns
# so: take values from the the 'from' column and put them in the 'to' column
par.sample.temp[,col.index.to] <- par.sample.temp[,col.index.from]
# put together with entire dataset
par.sample <- rbind(par.sample,par.sample.temp)
rm(par.sample.temp)
# order by ID variable (needed for lags below)
par.sample <- par.sample[order(par.sample$idnr,par.sample$year),]
# update categorical age
par.sample$age.1619 <- ifelse(par.sample$age <= 19,1,0)
par.sample$age.2023 <- ifelse(par.sample$age >= 20 & par.sample$age <= 23,1,0)
par.sample$age.2427 <- ifelse(par.sample$age >= 24 & par.sample$age <= 27,1,0)
par.sample$age.2830 <- ifelse(par.sample$age >= 28 & par.sample$age <= 30,1,0)
par.sample$age.3133 <- ifelse(par.sample$age >= 31,1,0)
## predict functions here
par.sample <- par.sample[!is.na(par.sample$year),]
# predict birth
# (only allowed if individual hasn't had a birth yet
# since technically this is 'first live birth')
# so that's why tsfb==0
# (it is on purpose that I use parent==0 and not tsfb==0
# since tsfb is updated later)
par.sample$birth[par.sample$fut==t & par.sample$parent==0] <-
binomial.predict.1var(fit.bsf.birth, par.sample[par.sample$fut==t & par.sample$parent==0,])
# since birth is only predicted for those who have not yet had
# a birth, I set birth to 0 for those who already had a birth
par.sample$birth[par.sample$fut==t & par.sample$parent==1] <- 0
# get rid of those who still create NA stuff
# because some predictor is missing
par.sample <- par.sample[!is.na(par.sample$birth),]
# update tsfb (= 0 when birth happens but 1 when l.birth=1)
# and a +1 higher score for every year after that
# this makes use of l.parent, so no problem that parent is updated after this
par.sample$tsfb[par.sample$fut==t & par.sample$l.birth==1] <- 1
par.sample$tsfb[par.sample$fut==t & par.sample$l.birth==0 &
par.sample$l.parent==1] <- par.sample$l.tsfb[par.sample$fut==t & par.sample$l.birth==0 &
par.sample$l.parent==1] + 1
# set parenthood based on birth history
# namely if they had a birth this year
# or if tsfb > 0 (indicating they had a
# birth some time in the past)
par.sample$parent[par.sample$fut==t] <- ifelse(par.sample$birth[par.sample$fut==t]==1 |
par.sample$tsfb[par.sample$fut==t] > 0,
1,0)
# predict enrolment
par.sample$enrol[par.sample$fut==t] <- binomial.predict.1var(fit.bsf.enrol, par.sample[par.sample$fut==t,])
# update enrolc
par.sample$enrolc[par.sample$fut==t] <- par.sample$l.enrolc[par.sample$fut==t] + par.sample$enrol[par.sample$fut==t]
# update entryc
par.sample$entryc[par.sample$fut==t] <- ifelse(par.sample$enrol[par.sample$fut==t]==1 & par.sample$l.enrol[par.sample$fut==t]==0,
par.sample$entryc[par.sample$fut==t]+1,par.sample$entryc[par.sample$fut==t])
# predict education
x <- multinomial.predict.4var(fit.bsf.edu.pri,fit.bsf.edu.sec,
fit.bsf.edu.tertlow,fit.bsf.edu.terthi,
par.sample[par.sample$fut==t,])
par.sample$edu.pri[par.sample$fut==t] <- x[,1]
par.sample$edu.sec[par.sample$fut==t] <- x[,2]
par.sample$edu.tertlow[par.sample$fut==t] <- x[,3]
par.sample$edu.terthi[par.sample$fut==t] <- x[,4]
# predict partnership
x <- multinomial.predict.3var(fit.bsf.partner.single,fit.bsf.partner.cohab,fit.bsf.partner.married,
par.sample[par.sample$fut==t,])
par.sample$partner.single[par.sample$fut==t] <- x[,1]
par.sample$partner.cohab[par.sample$fut==t] <- x[,2]
par.sample$partner.married[par.sample$fut==t] <- x[,3]
# predict employment
par.sample$empl[par.sample$fut==t] <- binomial.predict.1var(fit.bsf.empl, par.sample[par.sample$fut==t,])
# update cumulative employment
par.sample$cumu.empl[par.sample$fut==t] <- par.sample$l.cumu.empl[par.sample$fut==t] + par.sample$empl[par.sample$fut==t]
# predict unemployment
par.sample$unempl[par.sample$fut==t] <- binomial.predict.1var(fit.bsf.unempl, par.sample[par.sample$fut==t,])
# predict income
par.sample$inc[par.sample$fut==t] <- predict.lm.withvar(fit.bsf.inc,par.sample[par.sample$fut==t,])
# update cumulative income
par.sample$cumu.inc[par.sample$fut==t] <- par.sample$l.cumu.inc[par.sample$fut==t] + par.sample$inc[par.sample$fut==t]
# predict household income
par.sample$inc.res[par.sample$fut==t] <- predict.lm.withvar(fit.bsf.hhinc,par.sample[par.sample$fut==t,])
# predict house
x <- multinomial.predict.4var(fit.bsf.house.parents,fit.bsf.house.owner,
fit.bsf.house.tenant,fit.bsf.house.other,
par.sample[par.sample$fut==t,])
par.sample$house.parents[par.sample$fut==t] <- x[,1]
par.sample$house.owner[par.sample$fut==t] <- x[,2]
par.sample$house.tenant[par.sample$fut==t] <- x[,3]
par.sample$house.other[par.sample$fut==t] <- x[,4]
# end loop
}
## intervention loop ##
par.sample.2$group <- 1
# start a loop that moves through the follow-up time units
# for those who had a birth in par.sample
# we can take their data and introduce the counterfactual
# and let the rest be random
# for those who don't, we could just take their data and
# do no prediction whatsoever
# this saves computational time
all.idnr <- unique(par.sample$idnr)
parent.idnr <- unique(par.sample$idnr[par.sample$birth==1])
notparent.idnr <- setdiff(all.idnr,parent.idnr)
par.sample.2 <- par.sample[which(par.sample$idnr %in% parent.idnr),]
# the data of those who don't get a baby in the ns will be added at
# the end of the counterfactual loop
# shift birth by 3 years
for(id in parent.idnr) {
par.sample.2.temp <- par.sample.2[par.sample.2$idnr==id,]
birthfut <- par.sample.2.temp$fut[par.sample.2.temp$birth==1]
par.sample.2.temp$birth <- 0
if(birthfut < 15) {
par.sample.2.temp$birth[par.sample.2.temp$fut==(birthfut+3)] <- 1
}
# keep original tsfb saved for later
par.sample.2.temp$tsfb.original <- par.sample.2.temp$tsfb
par.sample.2.temp$parent.original <- par.sample.2.temp$parent
# update tsfb
par.sample.2.temp$tsfb <- par.sample.2.temp$tsfb-3
par.sample.2.temp$tsfb[par.sample.2.temp$tsfb < 0] <- 0
par.sample.2.temp$l.tsfb <- lag1.func(par.sample.2.temp$tsfb,par.sample.2.temp$idnr)
# update parent
par.sample.2.temp$parent <- ifelse(par.sample.2.temp$fut >= birthfut+3,1,0)
par.sample.2.temp$l.parent <- lag1.func(par.sample.2.temp$parent,par.sample.2.temp$idnr)
# make indicator for when counterfactual happens
# which indicates when we can start making predictions again
# in a dynamic way in the intervention loop below
par.sample.2.temp$cf.fut <- ifelse(par.sample.2.temp$fut > birthfut,1,0)
# I use 'larger than' because we only use
# lagged effects in predictions
# otherwise it would have been 'equal to or larger than'
# and of course, it is birthfut and not the postponed birth fut
# because postponement can have an effect on e.g. education
# before the new birth occurs
# put back in larger dataset
par.sample.2[par.sample.2$idnr==id,] <- par.sample.2.temp
}
# put this dataset into par.sample.3 as well
# since these are the people we will intervene on
# while keeping labour and income constant
par.sample.3 <- par.sample.2
for(t in sort(unique(par.sample.2$fut[par.sample.2$cf.fut==1]))) {
## I won't remove the rows from after birth
## I simply re-estimate the values in those rows
# I will only re-estimate values for people
# at fut==tsfb-2
# I choose that value, because the postponement of birth
# means that since they didn't have a birth yet
# they can now at tsfb-2 have other events happen to them
# at fut < tsfb-2 I won't re-estimate since that is before
# the counterfactual situation and so things should by definition be
# exactly the same
# lag values: new values for t have been produced
# so I take the column information from a row at t-1
# and put it in the columns meant for lagged values at t
# the first are the 'from' columns, and the second the 'to' columns
# so: take values from the the 'from' column and put them in the 'to' column
par.sample.2[par.sample.2$fut==t,col.index.to] <- par.sample.2[par.sample.2$fut==t-1,col.index.from]
# order by ID variable (needed for lags below)
par.sample.2 <- par.sample.2[order(par.sample.2$idnr,par.sample.2$year),]
## predict functions here
# predict birth no longer needed
# and derived variables neither
# predict enrolment
par.sample.2$enrol[par.sample.2$fut==t & par.sample.2$cf.fut==1] <- binomial.predict.1var(fit.bsf.enrol, par.sample.2[par.sample.2$fut==t & par.sample.2$cf.fut==1,])
# update enrolc
par.sample.2$enrolc[par.sample.2$fut==t & par.sample.2$cf.fut==1] <- par.sample.2$l.enrolc[par.sample.2$fut==t & par.sample.2$cf.fut==1] + par.sample.2$enrol[par.sample.2$fut==t & par.sample.2$cf.fut==1]
# update entryc
par.sample.2$entryc[par.sample.2$fut==t & par.sample.2$cf.fut==1] <- ifelse(par.sample.2$enrol[par.sample.2$fut==t & par.sample.2$cf.fut==1]==1 & par.sample.2$l.enrol[par.sample.2$fut==t & par.sample.2$cf.fut==1]==0,
par.sample.2$entryc[par.sample.2$fut==t & par.sample.2$cf.fut==1]+1,par.sample.2$entryc[par.sample.2$fut==t & par.sample.2$cf.fut==1])
# predict education
x <- multinomial.predict.4var(fit.bsf.edu.pri,fit.bsf.edu.sec,
fit.bsf.edu.tertlow,fit.bsf.edu.terthi,
par.sample.2[par.sample.2$fut==t & par.sample.2$cf.fut==1,])
par.sample.2$edu.pri[par.sample.2$fut==t & par.sample.2$cf.fut==1] <- x[,1]
par.sample.2$edu.sec[par.sample.2$fut==t & par.sample.2$cf.fut==1] <- x[,2]
par.sample.2$edu.tertlow[par.sample.2$fut==t & par.sample.2$cf.fut==1] <- x[,3]
par.sample.2$edu.terthi[par.sample.2$fut==t & par.sample.2$cf.fut==1] <- x[,4]
# predict partnership
x <- multinomial.predict.3var(fit.bsf.partner.single,fit.bsf.partner.cohab,fit.bsf.partner.married,
par.sample.2[par.sample.2$fut==t & par.sample.2$cf.fut==1,])
par.sample.2$partner.single[par.sample.2$fut==t & par.sample.2$cf.fut==1] <- x[,1]
par.sample.2$partner.cohab[par.sample.2$fut==t & par.sample.2$cf.fut==1] <- x[,2]
par.sample.2$partner.married[par.sample.2$fut==t & par.sample.2$cf.fut==1] <- x[,3]
# predict employment
par.sample.2$empl[par.sample.2$fut==t & par.sample.2$cf.fut==1] <- binomial.predict.1var(fit.bsf.empl, par.sample.2[par.sample.2$fut==t & par.sample.2$cf.fut==1,])
# update cumulative employment
par.sample.2$cumu.empl[par.sample.2$fut==t] <- par.sample.2$l.cumu.empl[par.sample.2$fut==t] + par.sample.2$empl[par.sample.2$fut==t]
# predict unemployment
par.sample.2$unempl[par.sample.2$fut==t & par.sample.2$cf.fut==1] <- binomial.predict.1var(fit.bsf.unempl, par.sample.2[par.sample.2$fut==t & par.sample.2$cf.fut==1,])
# predict income
par.sample.2$inc[par.sample.2$fut==t & par.sample.2$cf.fut==1] <- predict.lm.withvar(fit.bsf.inc,par.sample.2[par.sample.2$fut==t & par.sample.2$cf.fut==1,])
# update cumulative income
par.sample.2$cumu.inc[par.sample.2$fut==t] <- par.sample.2$l.cumu.inc[par.sample.2$fut==t] + par.sample.2$inc[par.sample.2$fut==t]
# predict household income
par.sample.2$inc.res[par.sample.2$fut==t & par.sample.2$cf.fut==1] <- predict.lm.withvar(fit.bsf.hhinc,par.sample.2[par.sample.2$fut==t & par.sample.2$cf.fut==1,])
# predict house
x <- multinomial.predict.4var(fit.bsf.house.parents,fit.bsf.house.owner,
fit.bsf.house.tenant,fit.bsf.house.other,
par.sample.2[par.sample.2$fut==t & par.sample.2$cf.fut==1,])
par.sample.2$house.parents[par.sample.2$fut==t & par.sample.2$cf.fut==1] <- x[,1]
par.sample.2$house.owner[par.sample.2$fut==t & par.sample.2$cf.fut==1] <- x[,2]
par.sample.2$house.tenant[par.sample.2$fut==t & par.sample.2$cf.fut==1] <- x[,3]
par.sample.2$house.other[par.sample.2$fut==t & par.sample.2$cf.fut==1] <- x[,4]
# end loop
}
par.sample.temp <- par.sample[which(par.sample$idnr %in% parent.idnr),]
for(t in sort(unique(par.sample.3$fut[par.sample.3$cf.fut==1]))) {
## I won't remove the rows from after birth
## I simply re-estimate the values in those rows
# I will only re-estimate values for people
# at fut==tsfb-2
# I choose that value, because the postponement of birth
# means that since they didn't have a birth yet
# they can now at tsfb-2 have other events happen to them
# at fut < tsfb-2 I won't re-estimate since that is before
# the counterfactual situation and so things should by definition be
# exactly the same
# lag values: new values for t have been produced
# so I take the column information from a row at t-1
# and put it in the columns meant for lagged values at t
# the first are the 'from' columns, and the second the 'to' columns
# so: take values from the the 'from' column and put them in the 'to' column
par.sample.3[par.sample.3$fut==t,col.index.to] <- par.sample.3[par.sample.3$fut==t-1,col.index.from]
# order by ID variable (needed for lags below)
par.sample.3 <- par.sample.3[order(par.sample.3$idnr,par.sample.3$year),]
# since we keep the labour market variables constant, we will
# draw them from the natural course
# we can draw this deterministically, since we don't model censoring or competing risks
# draw employment
par.sample.3$empl[par.sample.3$fut==t] <- par.sample.temp$empl[par.sample.temp$fut==t]
# draw cumulative employment
par.sample.3$cumu.empl[par.sample.3$fut==t] <- par.sample.temp$cumu.empl[par.sample.temp$fut==t]
# draw unemployment
par.sample.3$unempl[par.sample.3$fut==t] <- par.sample.temp$unempl[par.sample.temp$fut==t]
# draw income
par.sample.3$inc[par.sample.3$fut==t] <- par.sample.temp$inc[par.sample.temp$fut==t]
# draw cumulative income
par.sample.3$cumu.inc[par.sample.3$fut==t] <- par.sample.temp$cumu.inc[par.sample.temp$fut==t]
## predict functions here
# predict birth no longer needed
# and derived variables neither
# predict enrolment
par.sample.3$enrol[par.sample.3$fut==t & par.sample.3$cf.fut==1] <- binomial.predict.1var(fit.bsf.enrol, par.sample.3[par.sample.3$fut==t & par.sample.3$cf.fut==1,])
# update enrolc
par.sample.3$enrolc[par.sample.3$fut==t & par.sample.3$cf.fut==1] <- par.sample.3$l.enrolc[par.sample.3$fut==t & par.sample.3$cf.fut==1] + par.sample.3$enrol[par.sample.3$fut==t & par.sample.3$cf.fut==1]
# update entryc
par.sample.3$entryc[par.sample.3$fut==t & par.sample.3$cf.fut==1] <- ifelse(par.sample.3$enrol[par.sample.3$fut==t & par.sample.3$cf.fut==1]==1 & par.sample.3$l.enrol[par.sample.3$fut==t & par.sample.3$cf.fut==1]==0,
par.sample.3$entryc[par.sample.3$fut==t & par.sample.3$cf.fut==1]+1,par.sample.3$entryc[par.sample.3$fut==t & par.sample.3$cf.fut==1])
# predict education
x <- multinomial.predict.4var(fit.bsf.edu.pri,fit.bsf.edu.sec,
fit.bsf.edu.tertlow,fit.bsf.edu.terthi,
par.sample.3[par.sample.3$fut==t & par.sample.3$cf.fut==1,])
par.sample.3$edu.pri[par.sample.3$fut==t & par.sample.3$cf.fut==1] <- x[,1]
par.sample.3$edu.sec[par.sample.3$fut==t & par.sample.3$cf.fut==1] <- x[,2]
par.sample.3$edu.tertlow[par.sample.3$fut==t & par.sample.3$cf.fut==1] <- x[,3]
par.sample.3$edu.terthi[par.sample.3$fut==t & par.sample.3$cf.fut==1] <- x[,4]
# predict partnership
x <- multinomial.predict.3var(fit.bsf.partner.single,fit.bsf.partner.cohab,fit.bsf.partner.married,
par.sample.3[par.sample.3$fut==t & par.sample.3$cf.fut==1,])
par.sample.3$partner.single[par.sample.3$fut==t & par.sample.3$cf.fut==1] <- x[,1]
par.sample.3$partner.cohab[par.sample.3$fut==t & par.sample.3$cf.fut==1] <- x[,2]
par.sample.3$partner.married[par.sample.3$fut==t & par.sample.3$cf.fut==1] <- x[,3]
# predict house
x <- multinomial.predict.4var(fit.bsf.house.parents,fit.bsf.house.owner,
fit.bsf.house.tenant,fit.bsf.house.other,
par.sample.3[par.sample.3$fut==t & par.sample.3$cf.fut==1,])
par.sample.3$house.parents[par.sample.3$fut==t & par.sample.3$cf.fut==1] <- x[,1]
par.sample.3$house.owner[par.sample.3$fut==t & par.sample.3$cf.fut==1] <- x[,2]
par.sample.3$house.tenant[par.sample.3$fut==t & par.sample.3$cf.fut==1] <- x[,3]
par.sample.3$house.other[par.sample.3$fut==t & par.sample.3$cf.fut==1] <- x[,4]
# predict household income
par.sample.3$inc.res[par.sample.3$fut==t & par.sample.3$cf.fut==1] <- predict.lm.withvar(fit.bsf.hhinc,par.sample.3[par.sample.3$fut==t & par.sample.3$cf.fut==1,])
# end loop
}
rm(par.sample.temp)
# save covariate information
# in the natural course and the intervention scenario
# by fut and monte carlo iteration
## only keep parents in both datasets
# first let's save the non-parents information
notpar.sample <- par.sample[which(par.sample$idnr %in% notparent.idnr),]
# we already filtered out the non-parents in par.sample.2 and par.sample.3
# so let's filter out the ones in par.sample as well
par.sample <- par.sample[which(par.sample$idnr %in% parent.idnr),]
## calculate tfsb for parents from par.sample.2 and par.sample.3
# simply put it back based on original tsfb
par.sample.2$tsfb <- par.sample.2$tsfb.original
par.sample.2$parent <- par.sample.2$parent.original
par.sample.3$tsfb <- par.sample.3$tsfb.original
par.sample.3$parent <- par.sample.3$parent.original
## add parent to both tsfbs
# so that the moment of birth is also included
# but not the tsfb==0 when a birth is not happening
par.sample$tsfb <- par.sample$tsfb + par.sample$parent
par.sample.2$tsfb <- par.sample.2$tsfb + par.sample.2$parent
par.sample.3$tsfb <- par.sample.3$tsfb + par.sample.3$parent
# note that the meaning of TSFB in the initial graphs is therefore
# different than what you might expect!
# save IDs for subgroup comparison
# namely: make a snapshot of the ids that people have
# when they have their first child at the appropriate age
# which is now at tsfb==1 because of adding parent to tsfb calc above
youngparent.idnr <- unique(par.sample$idnr[par.sample$age <= 29 & par.sample$tsfb==1])
# make subsets based on that
youngparent.par.sample <- par.sample[which(par.sample$idnr %in% youngparent.idnr),]
youngparent.par.sample.2 <- par.sample.2[which(par.sample.2$idnr %in% youngparent.idnr),]
youngparent.par.sample.3 <- par.sample.3[which(par.sample.3$idnr %in% youngparent.idnr),]
# take individual-level differences for youngparents
# make sure they are ordered the same
youngparent.par.sample <- youngparent.par.sample[order(youngparent.par.sample$idnr,youngparent.par.sample$fut),]
youngparent.par.sample.2 <- youngparent.par.sample.2[order(youngparent.par.sample.2$idnr,youngparent.par.sample.2$fut),]
youngparent.par.sample.3 <- youngparent.par.sample.3[order(youngparent.par.sample.3$idnr,youngparent.par.sample.3$fut),]
youngparent.par.diff <- youngparent.par.sample.2[,col.index.from[1:19]] - youngparent.par.sample[,col.index.from[1:19]]
youngparent.par.diff$tsfb <- youngparent.par.sample$tsfb
youngparent.par.diff.mediation <- youngparent.par.sample.3[,col.index.from[1:19]] - youngparent.par.sample[,col.index.from[1:19]]
youngparent.par.diff.mediation$tsfb <- youngparent.par.sample$tsfb
youngparent.monte.timetest[m,] <- apply(youngparent.par.sample.2[youngparent.par.sample.2$tsfb==4,col.index.from[1:19]] -
youngparent.par.sample[youngparent.par.sample.2$tsfb==1,col.index.from[1:19]],
MARGIN=2,mean,na.rm=T)
youngparent.monte.timetest.mediation[m,] <- apply(youngparent.par.sample.3[youngparent.par.sample.3$tsfb==4,col.index.from[1:19]] -
youngparent.par.sample[youngparent.par.sample.3$tsfb==1,col.index.from[1:19]],
MARGIN=2,mean,na.rm=T)
for(a in sort(unique(par.sample$tsfb))[-1]) {
# subset analysis: young parents (IATT)
youngparent.monte.arr[m,,a] <- apply(youngparent.par.diff[youngparent.par.diff$tsfb==a,],MARGIN=2,FUN=mean,na.rm=T)[1:19]
# subset analysis: young parents mediation (IATT)
youngparent.monte.arr.mediation[m,,a] <- apply(youngparent.par.diff.mediation[youngparent.par.diff.mediation$tsfb==a,],MARGIN=2,FUN=mean,na.rm=T)[1:19]
}
# treatment effect for the treated by age
for(a in sort(unique(par.sample$age))) {
# sample 1: natural course (ATT)
monte.arr.ATT[m,,a-15] <- apply(par.sample[par.sample$age==a,][,col.index.from[1:19]],MARGIN=2,FUN=mean,na.rm=T)
# sample 2: intervention scenario (ATT)
monte.arr.ATT.2[m,,a-15] <- apply(par.sample.2[par.sample.2$age==a,][,col.index.from[1:19]],MARGIN=2,FUN=mean,na.rm=T)
# sample 2: intervention scenario with education drawn from the natural course (ATT)
monte.arr.ATT.3[m,,a-15] <- apply(par.sample.3[par.sample.3$age==a,][,col.index.from[1:19]],MARGIN=2,FUN=mean,na.rm=T)
}
## put parents back in both datasets and save pop averaged effect
# for everyone by age
par.sample <- rbind(par.sample,notpar.sample)
par.sample.2 <- rbind(par.sample.2,notpar.sample)
par.sample.3 <- rbind(par.sample.3,notpar.sample)
for(a in sort(unique(par.sample$age))) {