-
Notifications
You must be signed in to change notification settings - Fork 2
/
report.tex
3459 lines (3007 loc) · 196 KB
/
report.tex
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
% Options for packages loaded elsewhere
\PassOptionsToPackage{unicode}{hyperref}
\PassOptionsToPackage{hyphens}{url}
%
\documentclass[
]{article}
\title{Predicting Group Life Client Mortality during a Pandemic, Final
Report}
\author{Team Outliers}
\date{August 2, 2021}
\usepackage{amsmath,amssymb}
\usepackage{lmodern}
\usepackage{iftex}
\ifPDFTeX
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{textcomp} % provide euro and other symbols
\else % if luatex or xetex
\usepackage{unicode-math}
\defaultfontfeatures{Scale=MatchLowercase}
\defaultfontfeatures[\rmfamily]{Ligatures=TeX,Scale=1}
\fi
% Use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\IfFileExists{microtype.sty}{% use microtype if available
\usepackage[]{microtype}
\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\makeatletter
\@ifundefined{KOMAClassName}{% if non-KOMA class
\IfFileExists{parskip.sty}{%
\usepackage{parskip}
}{% else
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}}
}{% if KOMA class
\KOMAoptions{parskip=half}}
\makeatother
\usepackage{xcolor}
\IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available
\IfFileExists{bookmark.sty}{\usepackage{bookmark}}{\usepackage{hyperref}}
\hypersetup{
pdftitle={Predicting Group Life Client Mortality during a Pandemic, Final Report},
pdfauthor={Team Outliers},
hidelinks,
pdfcreator={LaTeX via pandoc}}
\urlstyle{same} % disable monospaced font for URLs
\usepackage[margin=1in]{geometry}
\usepackage{color}
\usepackage{fancyvrb}
\newcommand{\VerbBar}{|}
\newcommand{\VERB}{\Verb[commandchars=\\\{\}]}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
% Add ',fontsize=\small' for more characters per line
\usepackage{framed}
\definecolor{shadecolor}{RGB}{248,248,248}
\newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}}
\newcommand{\AlertTok}[1]{\textcolor[rgb]{0.94,0.16,0.16}{#1}}
\newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.77,0.63,0.00}{#1}}
\newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\BuiltInTok}[1]{#1}
\newcommand{\CharTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\CommentTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{#1}}
\newcommand{\DecValTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\ErrorTok}[1]{\textcolor[rgb]{0.64,0.00,0.00}{\textbf{#1}}}
\newcommand{\ExtensionTok}[1]{#1}
\newcommand{\FloatTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\ImportTok}[1]{#1}
\newcommand{\InformationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\NormalTok}[1]{#1}
\newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.81,0.36,0.00}{\textbf{#1}}}
\newcommand{\OtherTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{#1}}
\newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\RegionMarkerTok}[1]{#1}
\newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\StringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\VariableTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\WarningTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\usepackage{longtable,booktabs,array}
\usepackage{calc} % for calculating minipage widths
% Correct order of tables after \paragraph or \subparagraph
\usepackage{etoolbox}
\makeatletter
\patchcmd\longtable{\par}{\if@noskipsec\mbox{}\fi\par}{}{}
\makeatother
% Allow footnotes in longtable head/foot
\IfFileExists{footnotehyper.sty}{\usepackage{footnotehyper}}{\usepackage{footnote}}
\makesavenoteenv{longtable}
\usepackage{graphicx}
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
\makeatother
% Scale images if necessary, so that they will not overflow the page
% margins by default, and it is still possible to overwrite the defaults
% using explicit options in \includegraphics[width, height, ...]{}
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
% Set default figure placement to htbp
\makeatletter
\def\fps@figure{htbp}
\makeatother
\setlength{\emergencystretch}{3em} % prevent overfull lines
\providecommand{\tightlist}{%
\setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{-\maxdimen} % remove section numbering
\ifLuaTeX
\usepackage{selnolig} % disable illegal ligatures
\fi
\begin{document}
\maketitle
\hypertarget{final-report}{%
\section{Final report}\label{final-report}}
IMA Math-to-Industry Bootcamp, Securian Financial
Team members:
\begin{itemize}
\tightlist
\item
Marc Härkönen
\item
Samara Chamoun
\item
Shuxian Xu
\item
Abba Ramadan
\item
Lei Yang
\item
Yuchen Cao
\end{itemize}
Mentor:
\begin{itemize}
\tightlist
\item
Douglas Armstrong
\end{itemize}
Supervisors:
\begin{itemize}
\tightlist
\item
Daniel Spirn
\item
Thomas Höft
\end{itemize}
\hypertarget{executive-summary}{%
\section{Executive summary}\label{executive-summary}}
As Babe Ruth once said: ``Yesterday's home runs don't win today's
games!''
In March 2020, with the pandemic starting, the whole world fell into a
state of uncertainty about the future. Similarly to other businesses,
the insurance sector was affected by the COVID breakout as well and the
whole business landscape needs to address the changes that came along.
We are team Outliers from the Securian Financial Department of Data
Science and we think we have the resources, the expertise and the
determination to present the management team with a whole new set of
information that can help their decision making during a pandemic. With
Group Life Insurance being an important part of our company and for our
clients, there is no doubt that we should look closely at how it is
being affected by the recent events. For the past few weeks, we have
been working on a project that aims to predict Group Life mortality for
our clients during a pandemic.
One might ask how the pandemic is exactly affecting group life
insurance. As we know, life insurance guarantees payment of death
benefits. Since the COVID-19 breakout, our clients are experiencing a
higher mortality rate then usual, which has resulted in an unprecedented
increase in claims. Our primary function as data scientists is to
correctly forecast the mortality risk, and the way to do that is by
first tracking the claims performance. We classify clients as high-risk
and low-risk by using one of the most popular metrics to track claims
performance, the Actual-to-Expected ratio (AE). Observing the large
shift of the proportion of clients that are classified as high-risk from
2019 to 2020, we hypothesize that the pre-pandemic, historical AE of a
client is no longer a good predictor of the client's performance during
a pandemic.
We aim at replacing this historical AE with a predictive one that can
help our management and sales team have better insight on the
possibility that a client experiences an Adverse mortality event during
an outbreak. We collect data from the zip codes where the companies are
located: poverty percentage, education level, unemployment rate, etc. We
then combine this information with some characteristics of the companies
such as average age of employees and some pandemic-related resources. We
then apply several machine learning models, validate the results and
build the best possible insight for proper risk-management.
We provide our management team with two models: one is long-term and the
other is short-term. Each of these models serve different purposes and
bring valuable assets to the company. The long-term model can be used at
a specific time and uses the information of some clients to predict what
can happen to other clients in different zip codes. While working on
this model, our goal was to minimize the loss of money for Securian that
can be caused by long-term adverse mortality event such as a pandemic.
On one hand, we aim at minimizing the number of clients that were
adverse and predicted otherwise. We also wanted to prevent the company
from losing clients that will perform well, so we simultaneously focused
on minimizing the number of clients that are not adverse and predicted
to be so. The strength of this model lies in understanding the
contributions of different predictors in the performance of the clients.
The management team can have better insights and clarity regarding how
each predictor contributes positively or negatively into the
classification. It is worthy to note that adding the AE2019 to the list
of predictors for this model won't make any additional improvements.
As opposed to the long-term model, the short-term model integrates the
time factor and can react to changes during the pandemic. Not only can
the model predict the future performance of existing clients, it can
also do so for potential new clients.
Having these two models in the hands of the management team, the latter
can gain accurate and deep understanding of old and new clients
performance during a pandemic. They can use this enhanced understanding
to determine contract renewals, to negotiate with clients and most
importantly to better face the uncertainties of the future.
\hypertarget{data-wrangling}{%
\section{Data wrangling}\label{data-wrangling}}
In this section, we describe our data gathering and tidying process. We
will be making extensive use of the \texttt{tidyverse} family of
packages. A series of scripts are used to generate tibbles, which are
then saved in a \texttt{*.feather} for fast loading. A full list of
scripts with their dependencies can be viewed in the Appendix.
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{library}\NormalTok{(tidyverse)}
\FunctionTok{library}\NormalTok{(feather)}
\FunctionTok{library}\NormalTok{(lubridate)}
\end{Highlighting}
\end{Shaded}
\hypertarget{data-sources}{%
\subsection{Data sources}\label{data-sources}}
Our dataset consists of two parts: publicly obtained data and simulated
clients. Below we describe our publicly obtained datasets.
\begin{longtable}[]{@{}
>{\raggedright\arraybackslash}p{(\columnwidth - 4\tabcolsep) * \real{0.31}}
>{\raggedright\arraybackslash}p{(\columnwidth - 4\tabcolsep) * \real{0.28}}
>{\raggedright\arraybackslash}p{(\columnwidth - 4\tabcolsep) * \real{0.41}}@{}}
\toprule
\begin{minipage}[b]{\linewidth}\raggedright
Filename
\end{minipage} & \begin{minipage}[b]{\linewidth}\raggedright
Source
\end{minipage} & \begin{minipage}[b]{\linewidth}\raggedright
Description
\end{minipage} \\
\midrule
\endhead
\texttt{covid\_deaths\_usafacts.csv} &
\href{https://usafacts.org/visualizations/coronavirus-covid-19-spread-map/}{USAFacts}
& Cumulative weekly COVID-19 deaths by county \\
\texttt{soa\_base\_2017.csv} & (Sent by Douglas Armstrong) & \(q_x\)
values by gender, age, industry \\
\texttt{Population\_Estimates.csv} &
\href{https://www.ers.usda.gov/data-products/county-level-data-sets/download-data/}{USDA
ERS} & Population estimates of the U.S., states and counties, 2019 \\
\texttt{COVID-19\_Vaccinations...}
&
\href{https://catalog.data.gov/dataset/covid-19-vaccinations-in-the-united-statescounty-8204e}{CDC}
& Overall US COVID-19 Vaccine administration and vaccine equity data at
county level \\
\texttt{Education\_Estimates.csv} &
\href{https://www.ers.usda.gov/data-products/county-level-data-sets/download-data/}{USDA
ERS} & Educational attainment for adults age 25 and older for the U.S.,
states and counties, 2015-19 \\
\texttt{Poverty\_Estimates.csv} &
\href{https://www.ers.usda.gov/data-products/county-level-data-sets/download-data/}{USDA
ERS} & Poverty rates in the U.S., states and counties, 2019 \\
\texttt{Unemployment\_Estimates.csv} &
\href{https://www.ers.usda.gov/data-products/county-level-data-sets/download-data/}{USDA
ERS} & Unemployment rates, 2019 and 2020; median househould income,
2019. States and counties \\
\texttt{Vaccine\_Hesitancy...}
&
\href{https://catalog.data.gov/dataset/vaccine-hesitancy-for-covid-19-county-and-local-estimates}{CDC}
& Vaccine hesitancy estimates for COVID-19 \\
\texttt{countypres\_2000-2020.csv} &
\href{https://dataverse.harvard.edu/file.xhtml?fileId=4819117\&version=9.0}{MIT
Election Data + Science Lab} & Election data by county (only 2020
used) \\
\texttt{zcta\_county\_rel\_10.txt} &
\href{https://www.census.gov/geographies/reference-files/time-series/geo/relationship-files.2010.html\#par_textimage_674173622}{US
Census Bureau} & Zip code to county relationship file (2010) \\
\texttt{2020\_12\_23/reference...} &
\href{http://www.healthdata.org/node/8787}{IHME} & COVID-19 projections
\textbf{as of Dec 23 2020} \\
\texttt{state.txt} &
\href{https://www.census.gov/library/reference/code-lists/ansi.html}{US
Census Bureau} & State names and FIPS codes \\
\bottomrule
\end{longtable}
\hypertarget{us-census-bureau}{%
\subsubsection{US Census bureau}\label{us-census-bureau}}
We used the US Census Bureau's API to obtain the 2019 estimates for
population and density per county from the Census Bureau's Population
Estimates Program (PEP). The \texttt{censusapi} package provides an R
interface to the API. Using the API requires an API key, which can be
obtained from \href{https://api.census.gov/data/key_signup.html}{here}.
The following snippet fetches the data, and saves the tibble into a file
called \texttt{pop\_den.feather}. See also \texttt{data/census.R}.
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{library}\NormalTok{(}\StringTok{"censusapi"}\NormalTok{)}
\FunctionTok{Sys.setenv}\NormalTok{(}\AttributeTok{CENSUS\_KEY =} \StringTok{"YOUR\_KEY\_HERE"}\NormalTok{)}
\CommentTok{\# date\_code = 12 is an estimate for July 1, 2019}
\CommentTok{\# total population + density}
\NormalTok{pop }\OtherTok{\textless{}{-}} \FunctionTok{getCensus}\NormalTok{(}
\AttributeTok{name =} \StringTok{"pep/population"}\NormalTok{,}
\AttributeTok{vintage =} \DecValTok{2019}\NormalTok{,}
\AttributeTok{region =} \StringTok{"county:*"}\NormalTok{,}
\AttributeTok{vars =} \FunctionTok{c}\NormalTok{(}\StringTok{"POP"}\NormalTok{, }\StringTok{"DENSITY"}\NormalTok{),}
\AttributeTok{DATE\_CODE =} \DecValTok{12}\NormalTok{)}
\NormalTok{pop }\OtherTok{\textless{}{-}} \FunctionTok{tibble}\NormalTok{(pop) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{select}\NormalTok{(}\SpecialCharTok{{-}}\NormalTok{DATE\_CODE)}
\FunctionTok{write\_feather}\NormalTok{(pop, }\StringTok{"pop\_den.feather"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\hypertarget{county-to-zip3}{%
\subsection{County to zip3}\label{county-to-zip3}}
So far all of our public data is expressed by US county, but our
clients' location are given as a ZIP3 code (the first three digits of a
five-digit zip code). The conversion from county to ZIP3 is nontrivial,
as some zip codes span multiple counties and some counties span multiple
zip codes.
To convert data given by county to ZIP3, we first need a ZIP3 to county
relationship table.
The relationship table contains three columns: ZIP3, County, and
Population. Each row corresponds to a pair
\((\text{ZIP3}, \text{county})\), and the Population column contains the
population in the intersection \(\text{ZIP3} \cap \text{county}\). Then,
given county-level data, we compute the corresponding value for any
given ZIP3 by taking a weighted average of all counties intersecting
that ZIP3, and weighting by the population in
\(\text{ZIP3} \cap \text{county}\). This operation looks as follows in
code (suppose \texttt{A} contains some county-level data, e.g.~poverty
levels):
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{A }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{left\_join}\NormalTok{(zip3\_rel, }\AttributeTok{by =} \StringTok{"county"}\NormalTok{) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{group\_by}\NormalTok{(zip3) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{summarize}\NormalTok{(}\AttributeTok{poverty =} \FunctionTok{weighted.mean}\NormalTok{(poverty, population, }\AttributeTok{na.rm =} \ConstantTok{TRUE}\NormalTok{))}
\end{Highlighting}
\end{Shaded}
We note that in practice, the country is represented by a 5 digit FIPS
code. The first two digits indicate the state, and the last 3 digits
indicate the county.
The relationship table is generated by \texttt{zip3\_rel.R} and can be
loaded from \texttt{zip3\_rel.feather}. For an example of how it's used,
see \texttt{wrangling.Rmd} and \texttt{deaths.R}.
\hypertarget{weekly-deaths-ihme-forecasts}{%
\subsection{Weekly deaths \& IHME
forecasts}\label{weekly-deaths-ihme-forecasts}}
In some of our models we use weekly COVID deaths as a predictor. The
file \texttt{covid\_deaths\_usafacts.csv} contains this data for every
day and every county. We convert the county-level information to zip3 as
above, and convert the daily data to weekly. The library
\texttt{lubridate} doesn't contain a type for week; we use the last day
of the week instead (using
\texttt{lubridate::ceiling\_date(date,\ unit\ =\ "week")}).
We will also be using forecasts from the Institute for Health Metrics
and Evaluation (IHME) to assist our models. These forecasts are only
given by state, so we need to convert states to ZIP3. The file
\texttt{data/state.txt} contains the state FIPS code and state name.
Since some ZIP3 codes span several states, we assign a state to each
ZIP3 code by determining which state is most represented among counties
in the ZIP3.
See also \texttt{data/deaths.R} and \texttt{time.Rmd} (line 856
onwards).
\hypertarget{simulated-client-dataset}{%
\subsection{Simulated client dataset}\label{simulated-client-dataset}}
The clients we were tasked to study were simulated by Securian
Financial. The dataset consists of 20 files called
\texttt{data/simulation\_data/experience\_weekly\_\{n\}.RDS} and
\texttt{data/simulation\_data/person\_\{n\}.RDS} for
\(n = 1,\dotsc, 10\). In total, we have 500 clients and 1,382,321
individuals.
The \texttt{person\_\{n\}.RDS} files contain information such as
company, zip code, age, face amount, gender, and collar (blue or white,
but in this dataset every indivual was blue collar). The rows in
\texttt{experience\_weekly\_\{n\}.RDS} correspond to individuals and
weeks, and contains a flag \texttt{death} that becomes 1 on the week
they die. In total, these tables contain 170,025,483 rows, but the same
information can be conveyed in 1,382,231 rows by attaching to each
individual their death date (or \texttt{NA} if they don't die).
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{read\_data }\OtherTok{\textless{}{-}} \ControlFlowTok{function}\NormalTok{(n) \{}
\NormalTok{ exp\_name }\OtherTok{\textless{}{-}} \FunctionTok{str\_glue}\NormalTok{(}\StringTok{"simulation\_data/experience\_weekly\_\{n\}.RDS"}\NormalTok{)}
\NormalTok{ per\_name }\OtherTok{\textless{}{-}} \FunctionTok{str\_glue}\NormalTok{(}\StringTok{"simulation\_data/person\_\{n\}.RDS"}\NormalTok{)}
\NormalTok{ exp }\OtherTok{\textless{}{-}} \FunctionTok{read\_rds}\NormalTok{(exp\_name)}
\NormalTok{ per }\OtherTok{\textless{}{-}} \FunctionTok{read\_rds}\NormalTok{(per\_name)}
\NormalTok{ dies }\OtherTok{\textless{}{-}}
\NormalTok{ exp }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{filter}\NormalTok{(death }\SpecialCharTok{\textgreater{}} \DecValTok{0}\NormalTok{) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{select}\NormalTok{(client, participant, week, month, year)}
\NormalTok{ aug\_per }\OtherTok{\textless{}{-}}
\NormalTok{ per }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{left\_join}\NormalTok{(dies, }\AttributeTok{by =} \FunctionTok{c}\NormalTok{(}\StringTok{"client"}\NormalTok{, }\StringTok{"participant"}\NormalTok{))}
\NormalTok{ aug\_per}
\NormalTok{\}}
\NormalTok{all\_persons }\OtherTok{\textless{}{-}}\NormalTok{ (}\DecValTok{1}\SpecialCharTok{:}\DecValTok{10}\NormalTok{) }\SpecialCharTok{\%\textgreater{}\%} \FunctionTok{map\_dfr}\NormalTok{(read\_data)}
\end{Highlighting}
\end{Shaded}
We noticed that some individuals die more than once. The following
removes the multiple deaths.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{all\_persons }\OtherTok{\textless{}{-}}
\NormalTok{ all\_persons }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{group\_by}\NormalTok{(client, participant) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{arrange}\NormalTok{(year, week, }\AttributeTok{.by\_group =} \ConstantTok{TRUE}\NormalTok{) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{slice\_head}\NormalTok{()}
\end{Highlighting}
\end{Shaded}
We finally attach to each individual their yearly \(q_x\) value, and
save the resuilting tibble in
\texttt{data/simultation\_data/all\_persons.feather}.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{qx\_table }\OtherTok{\textless{}{-}} \FunctionTok{read\_csv}\NormalTok{(}\StringTok{"soa\_base\_2017.csv"}\NormalTok{)}
\NormalTok{all\_persons }\OtherTok{\textless{}{-}}
\NormalTok{ all\_persons }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{left\_join}\NormalTok{(qx\_table, }\AttributeTok{by =} \FunctionTok{c}\NormalTok{(}\StringTok{"Age"}\NormalTok{, }\StringTok{"Sex"}\NormalTok{, }\StringTok{"collar"}\NormalTok{)) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{relocate}\NormalTok{(qx, }\AttributeTok{.after =}\NormalTok{ collar)}
\FunctionTok{write\_feather}\NormalTok{(all\_persons }\SpecialCharTok{\%\textgreater{}\%} \FunctionTok{ungroup}\NormalTok{(), }\StringTok{"simulation\_data/all\_persons.feather"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
The individual-level dataset is then converted to a client-level
dataset. We summarize each client by taking their ZIP3, size (number of
individuals), volume (sum of face amounts), average qx, average age, and
expected amount of claims. We also compute the amount weekly total
amount of claims.
See also \texttt{data/all\_persons.r}.
\hypertarget{final-cleanup}{%
\subsection{Final cleanup}\label{final-cleanup}}
Some of our clients are located in ZIP3 codes that we cannot deal with
for various reasons. They correspond to the following areas
\begin{longtable}[]{@{}
>{\raggedright\arraybackslash}p{(\columnwidth - 2\tabcolsep) * \real{0.29}}
>{\raggedright\arraybackslash}p{(\columnwidth - 2\tabcolsep) * \real{0.71}}@{}}
\toprule
\begin{minipage}[b]{\linewidth}\raggedright
ZIP3
\end{minipage} & \begin{minipage}[b]{\linewidth}\raggedright
Area
\end{minipage} \\
\midrule
\endhead
969 & Guam, Palau, Federated States of Micronesia, Northern Mariana
Islands, Marshall Islands \\
093 & Military bases in Iraq and Afghanistan \\
732 & Not in use \\
872 & Not in use \\
004 & Not in use \\
202 & Washington DC, Government 1 \\
753 & Dallas, TX \\
772 & Houston, TX \\
\bottomrule
\end{longtable}
The final two are problematic since they contained no population in
2010: one is used exclusively by a hospital, and the other is used
exclusively by a mall. Additionally, election data is not available in
Washington D.C., so we remove clients located there. In the end, we have
a total of 492 clients to work with.
The data merging is done in the file \texttt{processed\_data.r} which
generates the file \texttt{data/processed\_data\_20\_12\_23.feather}.
The dependency tree is outlined in the Appendix.
After merging, this gives us a final dataset of 492 clients over 118
weeks ranging from Jan 1st 2019 to June 27th 2021. We make two separate
tibbles.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{weekly\_data }\OtherTok{\textless{}{-}}
\FunctionTok{read\_feather}\NormalTok{(}\StringTok{"data/processed\_data\_20\_12\_23.feather"}\NormalTok{) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{select}\NormalTok{(}\SpecialCharTok{{-}}\NormalTok{ae\_2021, }\SpecialCharTok{{-}}\NormalTok{ae\_2020, }\SpecialCharTok{{-}}\NormalTok{ae\_2019,}
\SpecialCharTok{{-}}\NormalTok{actual\_2021, }\SpecialCharTok{{-}}\NormalTok{actual\_2020, }\SpecialCharTok{{-}}\NormalTok{actual\_2019, }\SpecialCharTok{{-}}\NormalTok{adverse,}
\SpecialCharTok{{-}}\NormalTok{STATE\_NAME, }\SpecialCharTok{{-}}\NormalTok{shrinkage, }\SpecialCharTok{{-}}\NormalTok{dep\_var) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{arrange}\NormalTok{(client, date)}
\NormalTok{yearly\_data }\OtherTok{\textless{}{-}}
\FunctionTok{read\_feather}\NormalTok{(}\StringTok{"data/processed\_data\_20\_12\_23.feather"}\NormalTok{) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{group\_by}\NormalTok{(client) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{slice}\NormalTok{(}\DecValTok{1}\NormalTok{) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{select}\NormalTok{(}\SpecialCharTok{{-}}\NormalTok{date, }\SpecialCharTok{{-}}\NormalTok{claims, }\SpecialCharTok{{-}}\NormalTok{zip\_deaths, }\SpecialCharTok{{-}}\NormalTok{smoothed\_ae, }\SpecialCharTok{{-}}\NormalTok{shrunk\_ae,}
\SpecialCharTok{{-}}\NormalTok{class, }\SpecialCharTok{{-}}\NormalTok{smoothed\_deaths,}
\SpecialCharTok{{-}}\NormalTok{hes, }\SpecialCharTok{{-}}\NormalTok{hes\_uns, }\SpecialCharTok{{-}}\NormalTok{str\_hes, }\SpecialCharTok{{-}}\NormalTok{ae, }\SpecialCharTok{{-}}\NormalTok{dep\_var, }\SpecialCharTok{{-}}\NormalTok{shrinkage, }\SpecialCharTok{{-}}\NormalTok{STATE\_NAME, }\SpecialCharTok{{-}}\NormalTok{ihme\_deaths)}
\end{Highlighting}
\end{Shaded}
Each row in \texttt{yearly\_data} corresponds to a client, and it
contains the following variables
\begin{longtable}[]{@{}
>{\raggedright\arraybackslash}p{(\columnwidth - 2\tabcolsep) * \real{0.43}}
>{\raggedright\arraybackslash}p{(\columnwidth - 2\tabcolsep) * \real{0.57}}@{}}
\toprule
\begin{minipage}[b]{\linewidth}\raggedright
Variable
\end{minipage} & \begin{minipage}[b]{\linewidth}\raggedright
Description
\end{minipage} \\
\midrule
\endhead
\texttt{zip3} & ZIP3 code \\
\texttt{client} & client ID \\
\texttt{size} & number of individuals \\
\texttt{volume} & sum of face values \\
\texttt{avg\_qx} & average \(q_x\) \\
\texttt{avg\_age} & average age \\
\texttt{per\_male} & percentage of males \\
\texttt{per\_blue\_collar} & percentage of blue collar workers \\
\texttt{expected} & expected yearly amount of claims \\
\texttt{actual\_\{2021,\ 2020,\ 2019\}} & actual claims in \{2021, 2020,
2019\} \\
\texttt{ae\_\{2021,\ 2020,\ 2019\}} & actual claims / expected claims in
\{2021, 2020, 2019\} \\
\texttt{nohs} & percentage of zip residents without a high school
diploma \\
\texttt{hs} & percentage of zip residents with only a high school
diploma \\
\texttt{college} & percentage of zip residents with only a community
college or associates degree \\
\texttt{bachelor} & percentage of zip residents with a bachelor's
degree \\
\texttt{R\_birth} & birthrate in zip \\
\texttt{R\_death} & deathrate in zip (pre-covid) \\
\texttt{unemp} & unemployment in zip \\
\texttt{poverty} & percentage of zip residents living in poverty \\
\texttt{per\_dem} & percentage of zip residents who voted Democrat in
2020 \\
\texttt{svi} & Social Vulnerability Index \\
\texttt{cvac} & CVAC level of concern for vaccine rollout \\
\texttt{income} & median household income in zipcode \\
\texttt{POP} & population in zipcode \\
\texttt{density} & zipcode population density \\
\texttt{adverse} & whether or not ae\_2020 \textgreater{} 3 \\
\bottomrule
\end{longtable}
The tibble \texttt{weekly\_data} contain most of the above variables,
but also some that change weekly. Each row correspond to a pair
\((\text{client}, \text{week})\). We describe the ones not present above
\begin{longtable}[]{@{}
>{\raggedright\arraybackslash}p{(\columnwidth - 2\tabcolsep) * \real{0.43}}
>{\raggedright\arraybackslash}p{(\columnwidth - 2\tabcolsep) * \real{0.57}}@{}}
\toprule
\begin{minipage}[b]{\linewidth}\raggedright
Variable
\end{minipage} & \begin{minipage}[b]{\linewidth}\raggedright
Description
\end{minipage} \\
\midrule
\endhead
\texttt{date} & the last day of the week
(\texttt{lubridate::ceiling\_date(date,\ unit\ =\ "week")}) \\
\texttt{claims} & claims for that client on that week (\$) \\
\texttt{zip\_deaths} & number of deaths that week in the zipcode \\
\texttt{smoothed\_ae} & smoothed version of actual weekly AE (see the
section on long-term models) \\
\texttt{shrunk\_ae} & shrunk version of smoothed weekly AE (see the
section on long-term models) \\
\texttt{ae} & actual weekly AE \\
\texttt{ihme\_deaths} & IHME Covid death forecasts. \textbf{These are
only available until Apr 4th 2021, and are set to 0 after this date.} \\
\texttt{hes}, \texttt{hes\_uns}, \texttt{str\_hes} & percentage of the
zip population that are vaccine hesitant, hesitant or unsure, and
strongly hesistan respectively \\
\bottomrule
\end{longtable}
\hypertarget{data-exploration-and-motivation}{%
\section{Data exploration and
motivation}\label{data-exploration-and-motivation}}
Since the pandemic started, our clients' claims increased dramatically.
In normal times, we expect an Actual-to-Expected ratio close to 1. As we
can see below, this doesn't apply in times of pandemic.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{yearly\_data }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{ungroup}\NormalTok{() }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{transmute}\NormalTok{(}
\StringTok{\textasciigrave{}}\AttributeTok{2019}\StringTok{\textasciigrave{}} \OtherTok{=}\NormalTok{ ae\_2019 }\SpecialCharTok{\textgreater{}} \DecValTok{1}\NormalTok{,}
\StringTok{\textasciigrave{}}\AttributeTok{2020}\StringTok{\textasciigrave{}} \OtherTok{=}\NormalTok{ ae\_2020 }\SpecialCharTok{\textgreater{}} \DecValTok{1}\NormalTok{,}
\StringTok{\textasciigrave{}}\AttributeTok{2021}\StringTok{\textasciigrave{}} \OtherTok{=}\NormalTok{ ae\_2021 }\SpecialCharTok{\textgreater{}} \DecValTok{1}\NormalTok{) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{pivot\_longer}\NormalTok{(}\StringTok{\textasciigrave{}}\AttributeTok{2019}\StringTok{\textasciigrave{}}\SpecialCharTok{:}\StringTok{\textasciigrave{}}\AttributeTok{2021}\StringTok{\textasciigrave{}}\NormalTok{, }\AttributeTok{names\_to =} \StringTok{"year"}\NormalTok{, }\AttributeTok{values\_to =} \StringTok{"adverse"}\NormalTok{) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{mutate}\NormalTok{(}\AttributeTok{adverse =} \FunctionTok{fct\_rev}\NormalTok{(}\FunctionTok{fct\_recode}\NormalTok{(}\FunctionTok{factor}\NormalTok{(adverse), }\StringTok{\textasciigrave{}}\AttributeTok{AE \textgreater{} 1}\StringTok{\textasciigrave{}} \OtherTok{=} \StringTok{"TRUE"}\NormalTok{, }
\StringTok{\textasciigrave{}}\AttributeTok{AE \textless{} 1}\StringTok{\textasciigrave{}} \OtherTok{=} \StringTok{"FALSE"}\NormalTok{))) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{ggplot}\NormalTok{(}\FunctionTok{aes}\NormalTok{(}\AttributeTok{x =}\NormalTok{ year, }\AttributeTok{fill =}\NormalTok{ adverse)) }\SpecialCharTok{+} \FunctionTok{geom\_bar}\NormalTok{() }\SpecialCharTok{+}
\FunctionTok{labs}\NormalTok{(}\AttributeTok{x =} \StringTok{"Year"}\NormalTok{, }\AttributeTok{y =} \StringTok{"Count"}\NormalTok{, }\AttributeTok{fill =} \StringTok{"Class"}\NormalTok{, }
\AttributeTok{title =} \StringTok{"Number of clients experiencing adverse mortality"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\includegraphics{figures/report/fig-unnamed-chunk-9-1.pdf}
We plot the magnitude of claims. Each dot corresponds to a client. We
see that the expected claims look similar to the actual claims in 2019,
while things change dramatically in 2020 and 2021. Note that the
vertical axis is logarithmic! The change in the claims during a pandemic
differs by orders of magnitude compared to the expected ones.
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{library}\NormalTok{(ggbeeswarm)}
\FunctionTok{set.seed}\NormalTok{(}\DecValTok{92929292}\NormalTok{)}
\NormalTok{yearly\_data }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{ungroup}\NormalTok{() }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{select}\NormalTok{(expected, actual\_2019, actual\_2020, actual\_2021) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{rename}\NormalTok{(}\AttributeTok{actual\_Expected =}\NormalTok{ expected) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{pivot\_longer}\NormalTok{(}\FunctionTok{everything}\NormalTok{(), }\AttributeTok{names\_to =} \StringTok{"Year"}\NormalTok{, }\AttributeTok{values\_to =} \StringTok{"Claims"}\NormalTok{) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{mutate}\NormalTok{(}\AttributeTok{Year =} \FunctionTok{str\_sub}\NormalTok{(Year, }\DecValTok{8}\NormalTok{)) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{filter}\NormalTok{(Claims }\SpecialCharTok{\textgreater{}} \DecValTok{0}\NormalTok{) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{ggplot}\NormalTok{(}\FunctionTok{aes}\NormalTok{(Year, Claims, }\AttributeTok{color =}\NormalTok{ Year)) }\SpecialCharTok{+} \FunctionTok{scale\_y\_log10}\NormalTok{() }\SpecialCharTok{+}
\FunctionTok{geom\_beeswarm}\NormalTok{(}\AttributeTok{size =} \FloatTok{0.5}\NormalTok{, }\AttributeTok{priority =} \StringTok{"random"}\NormalTok{) }\SpecialCharTok{+}
\FunctionTok{guides}\NormalTok{(}\AttributeTok{color =} \StringTok{"none"}\NormalTok{) }\SpecialCharTok{+} \FunctionTok{labs}\NormalTok{(}\AttributeTok{title =} \StringTok{"Size of claims"}\NormalTok{) }\SpecialCharTok{+}
\FunctionTok{scale\_color\_manual}\NormalTok{(}\AttributeTok{values =} \FunctionTok{c}\NormalTok{(}\StringTok{"yellow3"}\NormalTok{, }\StringTok{"deepskyblue"}\NormalTok{, }\StringTok{"black"}\NormalTok{, }\StringTok{"red"}\NormalTok{))}
\end{Highlighting}
\end{Shaded}
\includegraphics{figures/report/fig-unnamed-chunk-10-1.pdf}
\hypertarget{long-term-model}{%
\section{Long-term model}\label{long-term-model}}
Our first goal was to create a simple model to classify clients between
high risk or low risk. In this first model, we determine client risk
based on AE in 2020, and we will use data available before the pandemic
as predictors.
Our first task is to determine what ``high risk'' and ``low risk'' mean.
To this extent, we define ``AE 2020 \textgreater{} 3'' as ``high risk'',
as this is close the the first quartile of the AE in 2020.
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{summary}\NormalTok{(yearly\_data }\SpecialCharTok{\%\textgreater{}\%} \FunctionTok{pull}\NormalTok{(ae\_2020))}
\end{Highlighting}
\end{Shaded}
\begin{verbatim}
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.000 2.896 6.342 14.961 13.595 229.937
\end{verbatim}
This threshold was used to create the column \texttt{adverse} in
\texttt{yearly\_data}.
Thoughout this and following sections, we will be using extensively the
\texttt{tidymodels} framework. We will explain the commands as they
appear.
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{library}\NormalTok{(tidymodels)}
\end{Highlighting}
\end{Shaded}
\hypertarget{feature-engineering}{%
\subsection{Feature engineering}\label{feature-engineering}}
Our mentor's hypothesis was that the AE for 2019 was not a good
predictor for client risk during a pandemic. To test this hypothesis, we
train and test a selection of models, some with 2019 AE as a predictor,
and some without.
We start with a recipe, which defines our model formulas and data
preprocessing steps. We remove all categorical predictors and all
variables that are not available before 2020. We also remove the
correlated variable \texttt{actual\_2019}. We then remove zero-variance
predictors and normalize all predictors.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{with2019 }\OtherTok{\textless{}{-}}
\FunctionTok{recipe}\NormalTok{(adverse }\SpecialCharTok{\textasciitilde{}}\NormalTok{ ., }\AttributeTok{data =}\NormalTok{ yearly\_data) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{step\_rm}\NormalTok{(}\FunctionTok{all\_nominal\_predictors}\NormalTok{()) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{step\_rm}\NormalTok{(ae\_2020, ae\_2021, actual\_2019, actual\_2020, actual\_2021) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{step\_zv}\NormalTok{(}\FunctionTok{all\_predictors}\NormalTok{()) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{step\_normalize}\NormalTok{(}\FunctionTok{all\_predictors}\NormalTok{())}
\NormalTok{no2019 }\OtherTok{\textless{}{-}}
\NormalTok{ with2019 }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{step\_rm}\NormalTok{(ae\_2019)}
\end{Highlighting}
\end{Shaded}
Next, we describe our models using \texttt{parsnip} model
specifications. We will try 8 different models: logistic regression,
penalized logistic regression (penalty value chosen by initial tuning),
random forest, tuned random forest, single layer neural network, RBF
support vector machine, polynomial support vector machine, and K nearest
neighbors.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{log\_spec }\OtherTok{\textless{}{-}}
\FunctionTok{logistic\_reg}\NormalTok{() }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{set\_engine}\NormalTok{(}\StringTok{"glm"}\NormalTok{) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{set\_mode}\NormalTok{(}\StringTok{"classification"}\NormalTok{)}
\NormalTok{tuned\_log\_spec }\OtherTok{\textless{}{-}}
\FunctionTok{logistic\_reg}\NormalTok{(}\AttributeTok{penalty =} \FloatTok{0.00118}\NormalTok{) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{set\_engine}\NormalTok{(}\StringTok{"glmnet"}\NormalTok{) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{set\_mode}\NormalTok{(}\StringTok{"classification"}\NormalTok{)}
\NormalTok{forest\_spec }\OtherTok{\textless{}{-}}
\FunctionTok{rand\_forest}\NormalTok{(}\AttributeTok{trees =} \DecValTok{1000}\NormalTok{) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{set\_mode}\NormalTok{(}\StringTok{"classification"}\NormalTok{) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{set\_engine}\NormalTok{(}\StringTok{"ranger"}\NormalTok{, }\AttributeTok{num.threads =} \DecValTok{8}\NormalTok{, }\AttributeTok{importance =} \StringTok{"impurity"}\NormalTok{, }\AttributeTok{seed =} \DecValTok{123}\NormalTok{)}
\NormalTok{tuned\_forest\_spec }\OtherTok{\textless{}{-}}
\FunctionTok{rand\_forest}\NormalTok{(}\AttributeTok{trees =} \DecValTok{1000}\NormalTok{, }\AttributeTok{mtry =} \DecValTok{12}\NormalTok{, }\AttributeTok{min\_n =} \DecValTok{21}\NormalTok{) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{set\_mode}\NormalTok{(}\StringTok{"classification"}\NormalTok{) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{set\_engine}\NormalTok{(}\StringTok{"ranger"}\NormalTok{, }\AttributeTok{num.threads =} \DecValTok{8}\NormalTok{, }\AttributeTok{importance =} \StringTok{"impurity"}\NormalTok{, }\AttributeTok{seed =} \DecValTok{123}\NormalTok{)}
\NormalTok{sln\_spec }\OtherTok{\textless{}{-}}
\FunctionTok{mlp}\NormalTok{() }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{set\_engine}\NormalTok{(}\StringTok{"nnet"}\NormalTok{) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{set\_mode}\NormalTok{(}\StringTok{"classification"}\NormalTok{)}
\NormalTok{svm\_rbf\_spec }\OtherTok{\textless{}{-}}
\FunctionTok{svm\_rbf}\NormalTok{() }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{set\_engine}\NormalTok{(}\StringTok{"kernlab"}\NormalTok{) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{set\_mode}\NormalTok{(}\StringTok{"classification"}\NormalTok{)}
\NormalTok{svm\_poly\_spec }\OtherTok{\textless{}{-}}
\FunctionTok{svm\_poly}\NormalTok{() }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{set\_engine}\NormalTok{(}\StringTok{"kernlab"}\NormalTok{) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{set\_mode}\NormalTok{(}\StringTok{"classification"}\NormalTok{)}
\NormalTok{knn\_spec }\OtherTok{\textless{}{-}}
\FunctionTok{nearest\_neighbor}\NormalTok{() }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{set\_engine}\NormalTok{(}\StringTok{"kknn"}\NormalTok{) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{set\_mode}\NormalTok{(}\StringTok{"classification"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
In \texttt{tidymodels}, the combination of a recipe and a model
specification is called a \textbf{workflow}. Training a workflow trains
both the recipe (i.e.~it will learn the scaling and translation
parameters for the normalization step) and the underlying model. When a
workflow is used to predict, the trained recipe will automatically be
applied to a new set of data, and passed on to the trained model. We can
also combine sets of models and recipes into a \texttt{workflowset}.
This will allow us to easily train and test our models on the same
dataset.
We first split our clients into training and testing sets.
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{set.seed}\NormalTok{(}\DecValTok{30308}\NormalTok{)}
\NormalTok{init }\OtherTok{\textless{}{-}} \FunctionTok{initial\_split}\NormalTok{(yearly\_data, }\AttributeTok{strata =}\NormalTok{ adverse)}
\end{Highlighting}
\end{Shaded}
All of our model selection, tuning, etc. will be done using 10-fold CV
on the training set.
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{set.seed}\NormalTok{(}\DecValTok{30308}\NormalTok{)}
\NormalTok{crossval }\OtherTok{\textless{}{-}} \FunctionTok{vfold\_cv}\NormalTok{(}\FunctionTok{training}\NormalTok{(init), }\AttributeTok{strata =}\NormalTok{ adverse)}
\end{Highlighting}
\end{Shaded}
Our workflowset will contain the 16 combinations of the 8 model
specifications and 2 recipes. We train each one on the 10
cross-validation splits, and assess the results using the area under the
ROC (\texttt{roc\_auc}).
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{models }\OtherTok{\textless{}{-}} \FunctionTok{list}\NormalTok{(}\AttributeTok{Logistic =}\NormalTok{ log\_spec,}
\StringTok{\textasciigrave{}}\AttributeTok{Penalized logistic}\StringTok{\textasciigrave{}} \OtherTok{=}\NormalTok{ tuned\_log\_spec,}
\StringTok{\textasciigrave{}}\AttributeTok{Random forest}\StringTok{\textasciigrave{}} \OtherTok{=}\NormalTok{ forest\_spec,}
\StringTok{\textasciigrave{}}\AttributeTok{Tuned random forest}\StringTok{\textasciigrave{}} \OtherTok{=}\NormalTok{ tuned\_forest\_spec,}
\StringTok{\textasciigrave{}}\AttributeTok{Neural net}\StringTok{\textasciigrave{}} \OtherTok{=}\NormalTok{ sln\_spec,}
\StringTok{\textasciigrave{}}\AttributeTok{RBF SVM}\StringTok{\textasciigrave{}} \OtherTok{=}\NormalTok{ svm\_rbf\_spec,}
\StringTok{\textasciigrave{}}\AttributeTok{Polynomial SVM}\StringTok{\textasciigrave{}} \OtherTok{=}\NormalTok{ svm\_poly\_spec,}
\StringTok{\textasciigrave{}}\AttributeTok{KNN}\StringTok{\textasciigrave{}} \OtherTok{=}\NormalTok{ knn\_spec)}
\NormalTok{recipes }\OtherTok{\textless{}{-}} \FunctionTok{list}\NormalTok{(}\StringTok{"with2019ae"} \OtherTok{=}\NormalTok{ with2019,}
\StringTok{"no2019ae"} \OtherTok{=}\NormalTok{ no2019)}
\NormalTok{wflows }\OtherTok{\textless{}{-}} \FunctionTok{workflow\_set}\NormalTok{(recipes, models)}
\NormalTok{fit\_wflows }\OtherTok{\textless{}{-}}
\NormalTok{ wflows }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{workflow\_map}\NormalTok{(}\AttributeTok{fn =} \StringTok{"fit\_resamples"}\NormalTok{,}
\AttributeTok{seed =} \DecValTok{30332}\NormalTok{,}
\AttributeTok{resamples =}\NormalTok{ crossval,}
\AttributeTok{control =} \FunctionTok{control\_resamples}\NormalTok{(}\AttributeTok{save\_pred =} \ConstantTok{TRUE}\NormalTok{),}
\AttributeTok{metrics =} \FunctionTok{metric\_set}\NormalTok{(roc\_auc, accuracy))}
\end{Highlighting}
\end{Shaded}
We now look at the results with and without the 2019 AE as a predictor
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{fit\_wflows }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{collect\_metrics}\NormalTok{() }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{separate}\NormalTok{(wflow\_id, }\AttributeTok{into =} \FunctionTok{c}\NormalTok{(}\StringTok{"rec"}\NormalTok{, }\StringTok{"mod"}\NormalTok{), }\AttributeTok{sep =} \StringTok{"\_"}\NormalTok{, }\AttributeTok{remove =} \ConstantTok{FALSE}\NormalTok{) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{ggplot}\NormalTok{(}\FunctionTok{aes}\NormalTok{(}\AttributeTok{x =}\NormalTok{ rec, }\AttributeTok{y =}\NormalTok{ mean, }\AttributeTok{color =}\NormalTok{ mod, }\AttributeTok{group =}\NormalTok{ mod)) }\SpecialCharTok{+}
\FunctionTok{geom\_point}\NormalTok{() }\SpecialCharTok{+} \FunctionTok{geom\_line}\NormalTok{() }\SpecialCharTok{+} \FunctionTok{facet\_wrap}\NormalTok{(}\SpecialCharTok{\textasciitilde{}} \FunctionTok{factor}\NormalTok{(.metric)) }\SpecialCharTok{+}
\FunctionTok{labs}\NormalTok{(}\AttributeTok{color =} \StringTok{"Model"}\NormalTok{, }\AttributeTok{x =} \ConstantTok{NULL}\NormalTok{, }\AttributeTok{y =} \StringTok{"Value"}\NormalTok{, }
\AttributeTok{title =} \StringTok{"Performance of models with/without 2019 data"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\includegraphics{figures/report/fig-unnamed-chunk-18-1.pdf}
The performance with 2019 AE as a predictor is equal or worse than not
using it. Thus in the following we use the recipe where 2019 AE is
removed. We note that the above analysis was done with models with
default hyperparameters. It is certainly possible that some methods
would have seen benefits from tuning.
\hypertarget{model-selection}{%
\subsection{Model selection}\label{model-selection}}
With our data preprocessing locked in, we turn to model selection next.
We will look at five models, each with 10 different hyperparameters.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{tune\_log\_spec }\OtherTok{\textless{}{-}}
\FunctionTok{logistic\_reg}\NormalTok{(}\AttributeTok{penalty =} \FunctionTok{tune}\NormalTok{()) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{set\_engine}\NormalTok{(}\StringTok{"glmnet"}\NormalTok{) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{set\_mode}\NormalTok{(}\StringTok{"classification"}\NormalTok{)}
\NormalTok{tune\_forest\_spec }\OtherTok{\textless{}{-}}
\FunctionTok{rand\_forest}\NormalTok{(}\AttributeTok{trees =} \DecValTok{1000}\NormalTok{, }\AttributeTok{mtry =} \FunctionTok{tune}\NormalTok{(), }\AttributeTok{min\_n =} \FunctionTok{tune}\NormalTok{()) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{set\_mode}\NormalTok{(}\StringTok{"classification"}\NormalTok{) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{set\_engine}\NormalTok{(}\StringTok{"ranger"}\NormalTok{, }\AttributeTok{num.threads =} \DecValTok{8}\NormalTok{, }\AttributeTok{importance =} \StringTok{"impurity"}\NormalTok{, }\AttributeTok{seed =} \DecValTok{123}\NormalTok{)}
\NormalTok{tune\_sln\_spec }\OtherTok{\textless{}{-}}
\FunctionTok{mlp}\NormalTok{(}\AttributeTok{hidden\_units =} \FunctionTok{tune}\NormalTok{(), }\AttributeTok{penalty =} \FunctionTok{tune}\NormalTok{(), }\AttributeTok{epochs =} \FunctionTok{tune}\NormalTok{()) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{set\_engine}\NormalTok{(}\StringTok{"nnet"}\NormalTok{) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{set\_mode}\NormalTok{(}\StringTok{"classification"}\NormalTok{)}
\NormalTok{tune\_svm\_rbf\_spec }\OtherTok{\textless{}{-}}
\FunctionTok{svm\_rbf}\NormalTok{(}\AttributeTok{cost =} \FunctionTok{tune}\NormalTok{(), }\AttributeTok{rbf\_sigma =} \FunctionTok{tune}\NormalTok{(), }\AttributeTok{margin =} \FunctionTok{tune}\NormalTok{()) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{set\_engine}\NormalTok{(}\StringTok{"kernlab"}\NormalTok{) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{set\_mode}\NormalTok{(}\StringTok{"classification"}\NormalTok{)}
\NormalTok{tune\_knn\_spec }\OtherTok{\textless{}{-}}
\FunctionTok{nearest\_neighbor}\NormalTok{(}\AttributeTok{neighbors =} \FunctionTok{tune}\NormalTok{(), }\AttributeTok{dist\_power =} \FunctionTok{tune}\NormalTok{()) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{set\_engine}\NormalTok{(}\StringTok{"kknn"}\NormalTok{) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{set\_mode}\NormalTok{(}\StringTok{"classification"}\NormalTok{)}
\NormalTok{models }\OtherTok{\textless{}{-}} \FunctionTok{list}\NormalTok{(}\StringTok{\textasciigrave{}}\AttributeTok{Logistic}\StringTok{\textasciigrave{}} \OtherTok{=}\NormalTok{ tune\_log\_spec,}
\StringTok{\textasciigrave{}}\AttributeTok{Random forest}\StringTok{\textasciigrave{}} \OtherTok{=}\NormalTok{ tune\_forest\_spec,}
\StringTok{\textasciigrave{}}\AttributeTok{Neural network}\StringTok{\textasciigrave{}} \OtherTok{=}\NormalTok{ tune\_sln\_spec,}
\StringTok{\textasciigrave{}}\AttributeTok{SVM RBF}\StringTok{\textasciigrave{}} \OtherTok{=}\NormalTok{ tune\_svm\_rbf\_spec,}
\StringTok{\textasciigrave{}}\AttributeTok{KNN}\StringTok{\textasciigrave{}} \OtherTok{=}\NormalTok{ tune\_knn\_spec)}
\NormalTok{recipes }\OtherTok{\textless{}{-}} \FunctionTok{list}\NormalTok{(no2019)}
\NormalTok{wflows }\OtherTok{\textless{}{-}} \FunctionTok{workflow\_set}\NormalTok{(recipes, models)}
\end{Highlighting}
\end{Shaded}
For each model, the 10 tuning parameters will be automatically selected
using a latin hypercube. See the documentation of
\texttt{dials::grid\_latin\_hypercube} for implementation details.
Again, performance will be evaluated by 10-fold crossvalidation.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{results }\OtherTok{\textless{}{-}}
\NormalTok{ wflows }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{workflow\_map}\NormalTok{(}\AttributeTok{resamples =}\NormalTok{ crossval,}
\AttributeTok{grid =} \DecValTok{10}\NormalTok{,}
\AttributeTok{metrics =} \FunctionTok{metric\_set}\NormalTok{(roc\_auc, accuracy),}
\AttributeTok{control =} \FunctionTok{control\_grid}\NormalTok{(}\AttributeTok{save\_pred =} \ConstantTok{TRUE}\NormalTok{),}
\AttributeTok{seed =} \DecValTok{828282}\NormalTok{)}
\DocumentationTok{\#\# i Creating pre{-}processing data to finalize unknown parameter: mtry}
\end{Highlighting}
\end{Shaded}
The results below suggest that the random forest is performing the best,
especially in terms of the area under the ROC. We will thus choose it
for further tuning.
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{autoplot}\NormalTok{(results)}
\end{Highlighting}
\end{Shaded}
\includegraphics{figures/report/fig-unnamed-chunk-20-1.pdf}
\hypertarget{tuning-a-random-forest}{%
\subsection{Tuning a random forest}\label{tuning-a-random-forest}}
Since we've chosen a random forest, we no longer need to normalize our
predictors. This will make model explanation easier later on. We wrap
the recipe and model specification into a workflow.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{forest\_rec }\OtherTok{\textless{}{-}}
\FunctionTok{recipe}\NormalTok{(adverse }\SpecialCharTok{\textasciitilde{}}\NormalTok{ ., }\AttributeTok{data =}\NormalTok{ yearly\_data) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{step\_rm}\NormalTok{(}\FunctionTok{all\_nominal\_predictors}\NormalTok{()) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{step\_rm}\NormalTok{(ae\_2020, ae\_2021, actual\_2019, actual\_2020, actual\_2021) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{step\_zv}\NormalTok{(}\FunctionTok{all\_predictors}\NormalTok{()) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{step\_rm}\NormalTok{(ae\_2019)}
\NormalTok{forest\_wflow }\OtherTok{\textless{}{-}}
\FunctionTok{workflow}\NormalTok{() }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{add\_model}\NormalTok{(tune\_forest\_spec) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{add\_recipe}\NormalTok{(forest\_rec)}
\end{Highlighting}
\end{Shaded}
We have two tunable hyperparameters: \texttt{min\_n}, the minimal number
of datapoints required for a node to split, and \texttt{mtry}, the
number of randomly selected predictors in each tree. We fix the number
of trees to 1000, and we set the tuning range of \texttt{mtry} to be
between 1 and 20. Tuning will happen on a regular, 10 x 10 grid.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{forest\_params }\OtherTok{\textless{}{-}}
\NormalTok{ forest\_wflow }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{parameters}\NormalTok{() }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{update}\NormalTok{(}\AttributeTok{mtry =} \FunctionTok{mtry}\NormalTok{(}\FunctionTok{c}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DecValTok{20}\NormalTok{)))}
\NormalTok{forest\_grid }\OtherTok{\textless{}{-}}
\FunctionTok{grid\_regular}\NormalTok{(forest\_params, }\AttributeTok{levels =} \DecValTok{10}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{forest\_tune }\OtherTok{\textless{}{-}}
\NormalTok{ forest\_wflow }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{tune\_grid}\NormalTok{(}
\AttributeTok{resamples =}\NormalTok{ crossval,}
\AttributeTok{grid =}\NormalTok{ forest\_grid,}
\AttributeTok{metrics =} \FunctionTok{metric\_set}\NormalTok{(roc\_auc, accuracy)}
\NormalTok{ )}
\end{Highlighting}
\end{Shaded}