-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodeling_college_ed.qmd
1136 lines (884 loc) · 37.4 KB
/
modeling_college_ed.qmd
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
---
title: "Modeling College Education"
format:
html:
embed-resources: true
editor: visual
execute:
echo: false
warning: false
fig-cap-location: top
toc: true
toc-expand: true
---
```{r}
library(Hmisc)
library(tidyverse)
library(margins)
library(scales)
library(forecast)
library(fpp)
library(ggridges)
library(modelsummary)
library(urbnthemes)
set_urbn_defaults(style = "print")
# In regressions, treat ordered factors as unordered
defcontrasts=options('contrasts')
options(contrasts=c("contr.treatment", "contr.treatment"))
```
# Introduction
The main purpose of this document is to define a new model of college education that is sensitive to parents' wealth and income.
## Current Model
The model of college education in the current version of DYNASIM consists of four models that simulate four separate college-related events each year:
1. Enrollment, for those who graduated from high school,
2. Returning to college, for those who are enrolled in college but not yet in their senior year,
3. Failing the senior year and remaining enrolled, for those who reach senior year, and
4. Dropping out of college, for those in senior year.
Those who reach the senior year, don't fail it, and don't drop out graduate from college.
Likelihoods of these events, which are implemented as a set of transition-probability look-up tables, are modeled as a function of:
- gender,
- parents' education (less than high school, high-school diploma, some college, or college diploma or more), and
- race/ethnicity (Black Non-Hispanic, Hispanic, and White Non-Hispanic).
Parents' education is the education of the head of a family. In two-parent households, in which parents are heterosexual couples, the father is assigned as the head.
This is a simplified model, as it assumes that each year spent in college, before the senior year, is successfully completed. All students who get enrolled in college are assumed to complete first year. In subsequent years, the retention model simulates their decision to return to school and continue their education.
## Logistic Hazard Model
The new model of college education will use the logistic hazard model rather than tables of transitional probabilities. This section introduces some concepts and notation.
### Survival and Hazard Functions
Assuming discrete time $t=1,2,...$ and a random variable $T$ that indicates the timing of an event (e.g., college enrollment), we define a *survival function* $S(.)$ at the end of time $t$ as: $$Pr(T>t) = 1-F(t) = S(t) = S_t$$ Similarly, at the beginning of $t$, the survival function is $Pr(T>t-1) = 1-F(t-1) = S(t-1)$. The (unconditional) probability of exit during the interval $t$ is $Pr(t-1<T<t) = S_{t-1}-S_t$. The conditional probability of exit during time $t$ conditional on surviving until $t-1$ is *interval hazard rate*: \begin{align}
h_t &= Pr(t-1<T<t|T>t-1) \\
&= \frac{Pr(t-1<T<t)}{Pr(T>t-1)} \\
&= \frac{S_{t-1}-S_t}{S_{t-1}}
\end{align}
The probability of survival until the end of interval $t$ is the product of probabilities of not experiencing an event in each of the intervals ${1,...,t}$:
$$S_t = \Pi_{k=1}^{t} (1-h_k)$$
### Proportional Odds and Logistic Hazard Model
The proportional odds model assumes that the odds ratio for a person with a vector of individual characteristics $X$ at time $t$ is proportional to the odds ratio at $t$ that is common to all individuals and an individual-specific scaling factor:
$$\frac{h(t,X)}{1-h(t,X)} = \frac{h_0(t)}{1-h_0(t)} \exp (\beta' X)$$
Taking into account the definition of the logit function $\text{logit}(x) = \log(x/(1-x))$ and denoting $\text{logit}(h_0(t))=\alpha_t$, yields the *logistic hazard model*:
$$h(t,X) = \frac{1}{1+\exp(-\alpha_t -\beta' X)}$$ This model can be estimated by the standard logistic regression. The data set has to have one observation per person per time period. The dependent variable is 0 for time periods without an event (i.e., survived periods) and 1 for the time period in which an event occurs. If an event occurs for a person, the period in which it occurs is the last period for that person.
Note that the only time-variable term is $\alpha_t$. This term can be modeled as a function of time (e.g., linear, logarithmic) or with time dummies.
# Proposed Model
The proposed model of college education consists of four models:
1. College enrollment, for those who graduated from high school,
2. College re-enrollment, for those who dropped out of college,
3. Dropping out of college, for those who are enrolled in college,
4. Graduating from college, for those who reach their senior year.
## Data and Settings
```{r}
#| echo: true
# These covariates are used in all regressions
model_covars = c("yearf", "asian", "pnetworth", "asian*pnetworth", "pared", "asian*pared")
# Linear transformation of wealth (divided by average annual earnings)
opt_wealth_xform = 'linear'
# Parents' education is encoded as number of parents with a college diploma
opt_pared_code = 'college'
# Coefficients' significance level
opt_sig_level = .1
```
```{r}
#| label: make-basedf
source(here::here("NLSY/nlsy_lib.R"))
source(here::here("dynasim_lib.R"))
# Create a base frame
basedf0 = nlsy_get_base_df() |>
select(id, sex, race, hisp, pnetworth, wt) |>
filter(!is.na(race) & !is.na(hisp) & race!='No information') |>
mutate(
pnetworth_na = as.integer(is.na(pnetworth)),
pnetworth = if_else(is.na(pnetworth), 0, pnetworth),
race = nlsy_recode_race_and_eth5(race, hisp)
) |>
# There are not enough Asians in the dataset for a separate model
# We group them with whites and use a dummy variable
mutate(
asian = as.integer(race=="Asian"),
race = if_else(race=="Asian", "White", race)
) |>
filter(race != 'Other')
basedf = nlsy_get_base_df() |>
select(id, sex, race, hisp, wt) |>
filter(!is.na(race) & !is.na(hisp) & race!='No information') |>
left_join(nlsy_get_imputed()) |>
mutate(
race = nlsy_recode_race_and_eth5(race, hisp),
pared= nlsy_encode_parents_educ(mom_educ, dad_educ, encoding=opt_pared_code),
# Remove retirement savings because we don't have them in FEH
pnetworth = pnetworth-retsav
) |>
# There are not enough Asians in the dataset for a separate model
# We group them with whites and use a dummy variable
mutate(
asian = as.integer(race=="Asian"),
race = if_else(race=="Asian", "White", race)
) |>
filter(race != 'Other') |>
mutate(
pnworth_cat = cut(
pnetworth,
breaks=Hmisc::wtd.quantile(pnetworth, wt, probs=seq(0,1,.2)),
labels=c('1st','2nd','3rd','4th','5th'),
include.lowest = TRUE)
)
```
```{r}
#| label: fig-pnetworth-density
#| fig-cap: "Distribution of parent's networth (divided by the average wage index) by race"
basedf |>
select(id, pnetworth, pincome, race, wt) |>
mutate(pnetworth = dyn_wealth_xform(pnetworth, type='linear')) |>
filter(pnetworth>-20) |>
ggplot() +
geom_density(aes(x=pnetworth, y=after_stat(density), color=race, weight=wt), fill=NA) +
xlab("Net worth / AWI") + ylab("Density")
```
```{r}
#| label: fig-ever-college-by-race-pnetworth
#| fig-cap: "The share of NLSY participants who attended college by race and quintile of parents' net worth"
nlsy_get_col_stat_annual_df() |>
group_by(id) |>
summarise(evercol = max(colenr)) |>
right_join(
basedf,
by='id'
) |>
filter(!is.na(pnworth_cat)) |>
group_by(race, pnworth_cat) |>
mutate(
`College Attendance`=weighted.mean(evercol, wt),
se = sqrt(`College Attendance`*(1-`College Attendance`)/n()),
ci_l = `College Attendance`-1.96*se,
ci_h = `College Attendance`+1.96*se) |>
ggplot() +
geom_col(aes(x=race,y=`College Attendance`,fill=pnworth_cat), position="dodge") +
# geom_errorbar(aes(x=race, ymin=ci_l, ymax=ci_h, group=pnworth_cat), color='red', width = .2, position=position_dodge(.7)) +
xlab("Race")
```
## Enrollment
### Enrollment After Graduating from High School
The college enrollment model simulates enrollment into college for high-school graduates. To allow high-school graduates to take a break before enrolling into college, we need to model enrollment as a *survival model*, in which all high-school graduate who have not enrolled in college are at risk of enrolling in each year after graduating from high school for some number of years that we call *enrollment window*. The enrollment window in the current model is seven years.
The data for estimating this model should have one observation per person per year. The time should be measured in years since the high-school graduation. The dependent variable should have value zero in years in which a person was not enrolled in college, and one in the year in which the person enrolled into college. For a person who enrolled in college, the year of enrollment would be the last year for that person in the dataset. A person who did not enroll during the enrollment window would have a zero for each year during that window.
Note that a simpler way to implement this model would be as a one-shot logistic regression model that selects high-school graduates who enroll into college. Those who are not selected don't get another opportunity. However, this model does not represent reality well because many people don't enroll into college immediately after their high-school graduation (@fig-enrol-hazard). If this kind of model was calibrated to match the total enrollment, it would result in the average college graduation age that is younger than that in the population.
Another interesting thing to note on @fig-enrol-hazard is that the college-enrollment hazard for Black women in years 2 to 7 remains significantly higher than the hazard for other groups.
```{r}
#| label: make-colenrdf
##| cache: true
##| cache-vars: colenrdf
colenrdf = nlsy_make_spell_df('enroll') |>
filter(!is.na(spEnroll)) |>
select(id, timeEnroll, tEnroll, spEnroll) |>
inner_join(
basedf,
by='id'
)
```
```{r}
#| label: fig-enrol-hazard
#| fig-cap: "Hazard function for college enrollment by race, sex and year since the graduation from high school."
plotdf = colenrdf |>
group_by(timeEnroll, spEnroll, race, sex) |>
summarise(tEnroll=weighted.mean(tEnroll, wt)) |>
filter(spEnroll==1)
ggplot(plotdf) +
geom_line(aes(x=timeEnroll, y=tEnroll, color=race)) +
facet_grid(~sex) +
xlab('Years') +
ylab('Likelihood of college enrollment')
```
Even though some people make long breaks between high school and college, majority enrolls soon after high school: 90 percent enrolls within four years, 95 percent enrolls within 7 years, and 99 percent enrolls within 13 years (@fig-cum-share-gap-years).
```{r}
#| label: fig-cum-share-gap-years
#| fig-cap: "Cummulative share of years between high-school graduation and college enrollment."
plotdf = nlsy_make_spell_df() |>
filter(yrEnroll>0) |>
mutate(gap_years=yrEnroll-hs_grad_year) |>
group_by(id) |>
summarise(gap_years=min(gap_years)) |>
inner_join(basedf, by='id') |>
ungroup() |>
mutate(totwt=sum(wt)) |>
group_by(gap_years) |>
summarise(
totwt=max(totwt),
share=sum(wt)/totwt
) |>
mutate(F=cumsum(share))
plotdf |>
ggplot() +
geom_line(aes(x=gap_years, y=F)) +
xlab("Years between high-school graduation and college enrollment") +
ylab("Cumulative share")
```
```{r}
#| label: fig-enrol-hazard-by-pnetworth
#| fig-cap: "Hazard function for college enrollment by quintile of parents net-worth and year since the graduation from high school."
plotdf = colenrdf |>
group_by(timeEnroll, spEnroll, pnworth_cat) |>
summarise(tEnroll=weighted.mean(tEnroll, wt)) |>
filter(spEnroll==1)
ggplot(plotdf) +
geom_line(aes(x=timeEnroll, y=tEnroll, color=pnworth_cat)) +
xlab('Years') +
ylab('Likelihood of college enrollment')
```
```{r}
#| label: fig-college-completion-tiles
#| fig-width: 7
#| fig-height: 10
#| fig-cap: "College years completed in every year since the graduation from high school."
#| eval: false
make_first_enr_year_df = function()
{
# Make a college enrollment frame with:
# col1 (boolean): enrolled first time
# cumenr (int): cumulative years of enrollment,
# years_since_hs (int): number of years since the high-school graduation
#
# Make a frame with college enrollment and highest grade completed
colstdf = left_join(
nlsy_get_col_stat_fall_df(),
nlsy_get_highest_grade_completed_df(),
by=c('id', 'year')
)
data = colstdf |>
filter(year>=hs_grad_year) |>
mutate(
cumenr = cumsum(enrolled),
col1 = as.integer(enrolled==TRUE & cumenr==1),
year_since_hs = year-hs_grad_year
)
# Test that nobody has col1==1 in more than one year
stopifnot(max((data |> group_by(id) |>
summarise(col1 = sum(col1)))$col1) == 1)
# Add the year of college enrollment
# colenryr (int): the year when first enrolled in college
#
data = data |>
group_by(id) |>
mutate(
colenryr = case_when(
col1 == 1 ~ year,
TRUE ~ 0
),
colenryr = max(colenryr)
)
return(data)
}
plotdf = make_first_enr_year_df() |>
ungroup() |>
select(id, year_since_hs, hcyc) |>
pivot_wider(id_cols=id, names_from=year_since_hs, values_from = hcyc, names_prefix="y") |>
arrange(y0,y1,y2,y3,y4,y5,y6,y7,y8,y9,y10,y11,y12,y13,y14,y15,y16,y17,y18,y19,y20,y21,y22) |>
mutate(id2=row_number()) |>
pivot_longer(
starts_with('y'),
names_prefix='y',
values_to = 'hcyc',
names_to='year_since_hs') |>
mutate(
year_since_hs=as.integer(year_since_hs)
)
plotdf |>
ggplot() +
geom_tile(aes(x=year_since_hs, y=id2, fill=hcyc)) +
theme() +
scale_fill_gradientn() +
theme(legend.position = "right",
legend.direction = "vertical",
axis.line.x = element_blank(),
axis.title.y=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank(),
panel.grid.major.y = element_blank()) +
remove_ticks() +
xlab("Years Since High School")
```
We estimate a hazard model of college enrollment that regresses the college-enrollment indicator on a set of indicators for years after the high school graduation and a set of variables representing individual characteristics for race and sex and their interactions, $x_i$, one for each combination of sex and race:
$$ \text{logit}(y_{it}^{(j)}) = \sum_{k=0}^T \alpha_k^{(j)} I(k=t) + \beta' x_i, j=1,...,6$$
@tbl-enrol-simple-mod-est shows the estimated coefficients.
```{r}
#| label: est-simple-models
marginal_effects = function(model, data, variables)
{
return((summary(margins(model, data=data, variables=variables)))$AME)
}
# Create a factor year variable to use it as dummmies in regressions
estdf = colenrdf |>
filter(spEnroll==1) |>
mutate(
yearf=factor(pmin(timeEnroll,8)),
pnetworth=dyn_wealth_xform(pnetworth, type=opt_wealth_xform)
)
model_formula = as.formula(paste("tEnroll ~ ", paste(model_covars, collapse = " + ")))
# Estimate one model for each sex-race combination
estdf = estdf |>
group_by(race, sex) |>
nest() |>
mutate(
mods = map(data, \(x) glm(
model_formula,
data=x,
family=binomial)
)
) |>
mutate(pred = map(mods, \(x) predict(x, type='response'))) |>
mutate(marg = map2(mods, data, \(x,y) marginal_effects(x, y, variables=c("pnetworth"))))
```
```{r}
#| label: tbl-enrol-simple-mod-est
#| tbl-cap: "Estimated coefficients for the model of college enrollment"
# Create a named list of models
estdf = estdf |> arrange(desc(sex), race)
models = estdf$mods
names(models) = dyn_get_sexrace_names(estdf)
# Create a regression table
modelsummary(models,
estimate="{estimate}{stars}",
statistic=NULL,
stars=c('*'=.1, '**'=.05, '***'=.01),
coef_rename=coef_rename
# coef_rename=\(x) gsub('yearf(.*)', 'Year\\1', x)
)
# Dataframe with coefficients
res_colenr = dyn_coef_to_df(models, sig_level=opt_sig_level)
coefs = list()
coefs[['colenr']] = list(
df = dyn_coef_to_df(models, sig_level=opt_sig_level),
filename = "coefs/coef_col_enr.csv",
description = 'Coefficients for college enrollment model.'
)
```
@tbl-enroll-marginal shows average marginal effects of parental networth on the likelihood of enrollment. The numbers in the table can be interpreted as the effect that an increase in parental wealth by the amount equal to the average earnings in 1997 ($27,426) has on the likelihood of enrollment in college.
```{r}
#| label: tbl-enroll-marginal
#| tbl-cap: "Marginal effects of parental networth"
df = data.frame(estdf$marg)
colnames(df) = dyn_get_sexrace_names(estdf)
knitr::kable(df, digits=5)
```
@fig-enr-simple-mod-pred shows hazard rates by sex and race predicted by the model, as well as the actual hazard rates from @fig-enrol-hazard.
```{r}
#| label: fig-enr-simple-mod-pred
#| fig-cap: 'Hazard rates for college enrollment after graduating from high school by sex and race, actual and predicted.'
estdf |>
select(sex, race, data, pred) |>
unnest(cols=c(data, pred)) |>
group_by(timeEnroll, race, sex) |>
summarise(
tEnroll = weighted.mean(tEnroll, wt),
pred = weighted.mean(pred, wt)
) |>
rename(
`Actual` = tEnroll,
`Predicted` = pred
) |>
pivot_longer(cols=c('Actual', 'Predicted')) |>
ggplot() +
geom_line(aes(x=timeEnroll, y=value, color=name)) +
facet_grid(rows=vars(sex), cols=vars(race)) +
xlab('Years since high-school graduation') +
ylab('Likelihood of college enrollment')
```
### Enrollment After Graduating from High School, an Alternative
In this variation, we use the logarithmic transformation for parents' networth and we separate it in a positive and a negative term. For positive values of networth, the positive term equals the natural logarithm of networth and the negative term is zero. For negative values of networth, the positive term is zero and the negative term equals the natural logarithm of negative networth. @tbl-enrol2-simple-mod-est shows the estimated coefficients.
```{r}
#| label: est-enroll2-models
# Create a factor year variable to use it as dummmies in regressions
estdf = colenrdf |>
filter(spEnroll==1) |>
mutate(
yearf = factor(pmin(timeEnroll,8)),
pwealthpos = dyn_wealth_xform(pnetworth, type='log+'),
pwealthneg = dyn_wealth_xform(pnetworth, type='log-')
)
model_covars2 = gsub("pnetworth","pwealthpos", model_covars)
model_covars2 = c(model_covars2, "pwealthneg", "asian*pwealthneg")
model_formula = as.formula(paste("tEnroll ~ ", paste(model_covars2, collapse = " + ")))
# Estimate one model for each sex-race combination
estdf = estdf |>
group_by(race, sex) |>
nest() |>
mutate(
mods = map(data, \(x) glm(
model_formula,
data=x,
family=binomial)
)
) |>
mutate(pred = map(mods, \(x) predict(x, type='response'))) |>
mutate(
margpos = map2(mods, data, \(x,y) marginal_effects(x, y, variables=c("pwealthpos"))),
margneg = map2(mods, data, \(x,y) marginal_effects(x, y, variables=c("pwealthneg"))),
)
```
```{r}
#| label: tbl-enrol2-simple-mod-est
#| tbl-cap: "Estimated coefficients for the model of college enrollment with parental networth split into negative an dpositive terms"
# Create a named list of models
estdf = estdf |> arrange(desc(sex), race)
models = estdf$mods
names(models) = dyn_get_sexrace_names(estdf)
# Create a regression table
modelsummary(models,
estimate="{estimate}{stars}",
statistic=NULL,
stars=c('*'=.1, '**'=.05, '***'=.01),
coef_rename=\(x) gsub('yearf(.*)', 'Year\\1', x)
)
```
@tbl-enroll2-marginal shows average marginal effects of parental networth on the likelihood of enrollment. The numbers in the table can be interpreted as the effect that one percent increase in parental wealth has on the likelihood of enrollment in college.
```{r}
#| label: tbl-enroll2-marginal
#| tbl-cap: "Marginal effects of parental networth separate for negative and positive"
df1 = data.frame(estdf$margpos)
df2 = data.frame(estdf$margneg)
colnames(df1) = dyn_get_sexrace_names(estdf)
colnames(df2) = dyn_get_sexrace_names(estdf)
df = bind_rows(df1, df2)
colnames(df) = dyn_get_sexrace_names(estdf)
knitr::kable(df, digits=5)
```
### Enrollment After Dropping out of College
Many students who drop out of college return and re-enroll. Some do this multiple times. @fig-enrol-hazard-spells shows enrollment hazard rates for the first two spells of non-enrollment after dropping out of college for all students who drop out of college. @fig-enrol-hazard-two-spells-sex-race shows enrollment hazard rates for the first two spells of non-enrollment after dropping out of college by sex and race. The higher-order spells have few observations, which makes their hazard rates very noisy.
```{r}
#| label: fig-enrol-hazard-spells
#| fig-cap: "Hazard function for college enrollment by enrollment spell. X-axes represents years since the graduation from high school, for the first spell of non-enrollment, and years, since dropping out of college for subsequent spells."
plotdf = colenrdf |>
group_by(timeEnroll, spEnroll) |>
summarise(tEnroll=weighted.mean(tEnroll, wt)) |>
filter(spEnroll %in% 2:4) |>
mutate(spEnroll = factor(spEnroll))
ggplot(plotdf) +
geom_line(aes(x=timeEnroll, y=tEnroll, color=spEnroll)) +
xlab('Years') +
ylab('Likelihood of college enrollment')
```
```{r}
#| label: fig-enrol-hazard-two-spells-sex-race
#| fig-cap: "Hazard function for college enrollment by enrollment spell, sex, and race. X-axes represents years since dropping out of college."
plotdf = colenrdf |>
group_by(timeEnroll, spEnroll, race, sex) |>
summarise(tEnroll=weighted.mean(tEnroll, wt)) |>
filter(spEnroll %in% 2:3) |>
filter(tEnroll < .5) |>
mutate(spEnroll = factor(spEnroll))
ggplot(plotdf) +
geom_line(aes(x=timeEnroll, y=tEnroll, color=spEnroll)) +
facet_grid(rows=vars(sex), cols=vars(race)) +
xlab('Years') +
ylab('Likelihood of college enrollment')
```
Because hazard rates for the first two spells are so close to each other and higher-order spells have few observations, we can merge all the spells together and estimate a single hazard rate for returning to college after dropping out.
```{r}
#| label: fig-enrol-hazard-all-spells-sex-race
#| fig-cap: "Hazard function for college enrollment, sex, and race. X-axes represents years since dropping out of college."
plotdf = colenrdf |>
filter(spEnroll %in% 2:7) |>
group_by(timeEnroll, race, sex) |>
summarise(tEnroll=weighted.mean(tEnroll, wt)) |>
filter(tEnroll < .5)
ggplot(plotdf) +
geom_line(aes(x=timeEnroll, y=tEnroll)) +
facet_grid(rows=vars(sex), cols=vars(race)) +
xlab('Years') +
ylab('Likelihood of college enrollment')
```
```{r}
#| label: est-reenrol-models
# Create a factor year variable to use it as dummmies in regressions
estdf = colenrdf |>
filter(spEnroll==2) |>
mutate(
yearf=factor(pmin(timeEnroll,5)),
pnetworth=dyn_wealth_xform(pnetworth, type=opt_wealth_xform)
)
model_formula = as.formula(paste("tEnroll ~ ", paste(model_covars, collapse = " + ")))
# Estimate one model for each sex-race combination
estdf = estdf |>
group_by(race, sex) |>
nest() |>
mutate(
mods = map(data, \(x) glm(
model_formula,
data=x,
family=binomial)
)
) |>
mutate(pred = map(mods, \(x) predict(x, type='response'))) |>
mutate(marg = map2(mods, data, \(x,y) marginal_effects(x, y, variables=c("pnetworth"))))
```
```{r}
#| label: tbl-reenrol-mod-est
#| tbl-cap: "Estimated coefficients of the re-enrollment model"
# Create a named list of models
estdf = estdf |> arrange(desc(sex), race)
models = estdf$mods
names(models) = dyn_get_sexrace_names(estdf)
# Create a regression table
modelsummary(models,
estimate="{estimate}{stars}",
statistic=NULL,
stars=c('*'=.1, '**'=.05, '***'=.01),
coef_rename=\(x) gsub('yearf(.*)', 'Year\\1', x)
)
# Dataframe with coefficients
res_colreenr = dyn_coef_to_df(models)
coefs[['colreenr']] = list(
df = dyn_coef_to_df(models, sig_level=opt_sig_level),
filename = "coefs/coef_col_reenr.csv",
description = 'Coefficients for the model of re-enrolling into college after dropping out.'
)
```
@tbl-reenroll-marginal shows average marginal effects of parental networth on the likelihood of enrollment in college after having dropped out. The numbers in the table can be interpreted as the effect that an increase in parental wealth by the amount equal to the average earnings in 1997 ($27,426) has on the likelihood of re-enrollment in college.
```{r}
#| label: tbl-reenroll-marginal
#| tbl-cap: "Marginal effects of parental networth on re-enrollment in college"
df = data.frame(estdf$marg)
colnames(df) = dyn_get_sexrace_names(estdf)
knitr::kable(df, digits=5)
```
```{r}
#| label: fig-enr-return-simple-mod-pred
#| fig-cap: 'Hazard rates for college enrollment after dropping out of college by sex and race, actual and predicted.'
estdf |>
select(sex, race, data, pred) |>
unnest(cols=c(data, pred)) |>
group_by(timeEnroll, race, sex) |>
summarise(
tEnroll = weighted.mean(tEnroll, wt),
pred = weighted.mean(pred, wt)
) |>
rename(
`Actual` = tEnroll,
`Predicted` = pred
) |>
pivot_longer(cols=c('Actual', 'Predicted')) |>
ggplot() +
geom_line(aes(x=timeEnroll, y=value, color=name)) +
facet_grid(rows=vars(sex), cols=vars(race)) +
xlab('Years') +
ylab('Likelihood of college enrollment')
```
## Dropping out of College
```{r}
rm(m1, m2models, models, plotdf)
```
The drop-out model simulates the likelihood of dropping out of college for students who are enrolled in college. This is a survival model where the dependent variable is zero if a student is enrolled in college and one in the year the student drops out. For students who graduate from college, the variable is zero in all years.
**Multiple Spells.** A problem with modeling dropping out of college as a terminal event is that some students who drop out of college return later. Some drop out and return multiple times, i.e., have multiple spells of college enrollment. @fig-college-spells-number shows the distribution of number of spells in the NLSY sample for all individuals who were enrolled in college and @fig-college-spells-shares shows this distribution by race and sex. It appears that about one-third of students have more than one spell, and some have as many as seven. Which is why we do not treat dropping out of college as a terminal event. If a student drops out before graduation, they would be at risk of re-enrolling in subsequent years.
```{r}
#| label: make-drop-df
#| cache: true
#| cache-vars: dropdf
dropdf = nlsy_make_spell_df() |>
filter(!is.na(spDrop))
```
```{r}
#| label: fig-college-spells-number
#| fig-cap: "Number of college enrollment spells"
nspellsdf = dropdf |>
group_by(id) |>
summarise(nspells = max(spDrop)) |>
inner_join(
basedf,
by='id'
)
nspellsdf |>
ggplot() +
geom_bar(aes(x=nspells)) +
xlab("Number of spells") +
ylab("Number of students")
```
```{r}
#| label: fig-college-spells-shares
#| fig-cap: "Shares of students by the number of college enrollment spells by race and sex"
nspellsdf |>
group_by(race, sex) |>
mutate(total=n()) |>
ungroup() |>
group_by(race, sex, nspells) |>
summarise(
spellrat = n()/max(total),
nspells = max(nspells)
) |>
ggplot() +
geom_col(aes(x=nspells, y=spellrat)) +
facet_grid(rows=vars(sex), cols=vars(race)) +
xlab("Number of spells") +
ylab("Share of students")
# nspellsdf |>
# filter(colgradyr>0 & colenryr>0) |>
# mutate(coltime = colgradyr-colenryr) |>
# group_by(nspells, coltime) |>
# tally() |>
# spread(nspells, n)
#
```
```{r}
plotdf = dropdf |>
filter(timeDrop>0) |>
inner_join(basedf, by='id') |>
group_by(spDrop, timeDrop) |>
summarise(tDrop = weighted.mean(tDrop, wt))
plotdf |>
mutate(spell=factor(spDrop)) |>
ggplot(aes(x=timeDrop, y=tDrop, color=spell)) +
geom_line() +
xlab("Year since the beginning of a spell") +
ylab("Share dropped")
```
@fig-dropout-hazard shows estimated hazard functions of dropping out of college in every year after the enrollment. Because some students drop out and re-enroll, these hazards are estimated for each enrollment spell separately (@fig-dropout-hazard-1). Hazard functions for spells two and three track each other closely over first seven years. Because higher numbered spells have few observations, we combine them and estimate a hazard function for spells 3-6 (@fig-dropout-hazard-2), spells 2-6 (@fig-dropout-hazard-3), and spells 1-6 (@fig-dropout-hazard-4).
```{r}
#| label: fig-dropout-hazard
#| fig-cap: "Drop-out hazard functions"
#| fig-subcap:
#| - "All six spells"
#| - "Combine spells 3-6"
#| - "Combine spells 2-6"
#| - "Combine all spells"
#| layout-nrow: 2
#| layout-ncol: 2
plotdf = dropdf |>
filter(timeDrop>0) |>
inner_join(basedf, by='id') |>
group_by(spDrop, timeDrop) |>
summarise(tDrop = weighted.mean(tDrop, wt))
# Plot all 6 spells
plotdf |>
mutate(spell=factor(spDrop)) |>
ggplot(aes(x=timeDrop, y=tDrop, color=spell)) +
geom_line() +
xlab("Year since the beginning of a spell") +
ylab("Share dropped")
# Merge spells 3 and above
plotdf = dropdf |>
filter(timeDrop>0) |>
inner_join(basedf, by='id') |>
mutate(spDrop = pmin(spDrop, 3)) |>
group_by(spDrop, timeDrop) |>
summarise(tDrop = weighted.mean(tDrop, wt))
plotdf |>
mutate(spell=factor(spDrop)) |>
ggplot(aes(x=timeDrop, y=tDrop, color=spell)) +
geom_line() +
xlab("Year since the beginning of a spell") +
ylab("Share dropped")
# Merge spells 2 and above
plotdf = dropdf |>
filter(timeDrop>0) |>
inner_join(basedf, by='id') |>
mutate(spDrop = pmin(spDrop, 2)) |>
group_by(spDrop, timeDrop) |>
summarise(tDrop = weighted.mean(tDrop, wt))
plotdf |>
mutate(spell=factor(spDrop)) |>
ggplot(aes(x=timeDrop, y=tDrop, color=spell)) +
geom_line() +
xlab("Year since the beginning of a spell") +
ylab("Share dropped")
# Merge all spells
plotdf = dropdf |>
filter(timeDrop>0) |>
inner_join(basedf, by='id') |>
group_by(timeDrop) |>
summarise(tDrop = weighted.mean(tDrop, wt))
plotdf |>
ggplot(aes(x=timeDrop, y=tDrop)) +
geom_line() +
xlab("Year since the beginning of a spell") +
ylab("Share dropped")
```
```{r}
#| label: est-simple-drop-models
# Create a factor year variable to use it as dummmies in regressions
estdf = dropdf |>
filter(timeDrop>0) |>
inner_join(
basedf,
by='id'
) |>
# filter(spDrop==1) |>
mutate(
yearf=factor(pmin(timeDrop,5)),
pnetworth=dyn_wealth_xform(pnetworth, type=opt_wealth_xform)
)
model_formula = as.formula(paste("tDrop ~ ", paste(model_covars, collapse = " + ")))
# Estimate one model for each sex-race combination
estdf = estdf |>
group_by(race, sex) |>
nest() |>
mutate(
mods = map(data, \(x) glm(
model_formula,
data=x,
family=binomial)
)
) |>
mutate(pred = map(mods, \(x) predict(x, type='response'))) |>
mutate(marg = map2(mods, data, \(x,y) marginal_effects(x, y, variables=c("pnetworth"))))
```
```{r}
#| label: tbl-drop-simple-mod-est
#| tbl-cap: "Estimated coefficients of the model of dropping out of college"
# Create a named list of models
estdf = estdf |> arrange(desc(sex), race)
models = estdf$mods
names(models) = dyn_get_sexrace_names(estdf)
# Create a regression table
modelsummary(models,
estimate="{estimate}{stars}",
statistic=NULL,
stars=c('*'=.1, '**'=.05, '***'=.01),
coef_rename=\(x) gsub('yearf(.*)', 'Year\\1', x))
# Dataframe with coefficients
res_coldrop = dyn_coef_to_df(models)
coefs[['coldrop']] = list(
df = dyn_coef_to_df(models, sig_level=opt_sig_level),
filename = "coefs/coef_col_drop.csv",
description = 'Coefficients for the model of dropping out of college.'
)
```
@tbl-drop-marginal shows average marginal effects of parental networth on the likelihood of dropping out of college. The numbers in the table can be interpreted as the effect that an increase in parental wealth by the amount equal to the average earnings in 1997 ($27,426) has on the likelihood of dropping out of college.
```{r}
#| label: tbl-drop-marginal
#| tbl-cap: "Marginal effects of parental networth on dropping out of college"
df = data.frame(estdf$marg)
colnames(df) = dyn_get_sexrace_names(estdf)
knitr::kable(df, digits=5)
```
```{r}
#| label: fig-drop-simple-mod-pred
#| fig-cap: 'Hazard rates for dropping out of college by sex and race, actual and predicted.'
estdf |>
select(sex, race, data, pred) |>
unnest(cols=c(data, pred)) |>
group_by(timeDrop, race, sex) |>
summarise(
tDrop = weighted.mean(tDrop, wt),
pred = weighted.mean(pred, wt)
) |>
rename(
`Actual` = tDrop,
`Predicted` = pred
) |>
pivot_longer(cols=c('Actual', 'Predicted')) |>
filter(timeDrop<=10) |>
ggplot() +
geom_line(aes(x=timeDrop, y=value, color=name)) +
scale_x_continuous(breaks=seq(2,10,2)) +
facet_grid(rows=vars(sex), cols=vars(race)) +
xlab('Years since college enrollment') +
ylab('Likelihood of dropping out of college')
```
## Graduating from College
```{r}
#| label: make-grad-df
#| cache: true
#| cache-vars: graddf
graddf = nlsy_make_spell_df() |>