/
ch_regr_mult_and_log.tex
3021 lines (2745 loc) 路 110 KB
/
ch_regr_mult_and_log.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
\begin{chapterpage}{Multiple and logistic regression}
\chaptertitle{Multiple and logistic \titlebreak{} regression}
\label{multipleRegressionAndANOVA}
\label{multipleAndLogisticRegression}
\label{ch_regr_mult_and_log}
\chaptersection{introductionToMultipleRegression}
\chaptersection{model_selection_section}
\chaptersection{multipleRegressionModelAssumptions}
\chaptersection{mario_kart_case_study}
\chaptersection{logisticRegression}
\end{chapterpage}
\renewcommand{\chapterfolder}{ch_regr_mult_and_log}
\chapterintro{The principles of simple linear regression
lay the foundation for more sophisticated regression
models used in a wide range of challenging settings.
In Chapter~\ref{multipleAndLogisticRegression},
we explore multiple regression, which introduces the
possibility of more than one predictor in a linear model,
and logistic regression,
a technique for predicting categorical
outcomes with two levels.}
\section{Introduction to multiple regression}
\label{introductionToMultipleRegression}
\index{multiple regression|seealso{regression}}
\index{regression!multiple|(}
\index{regression|(}
Multiple regression extends simple two-variable regression to the case that still has one response but many predictors (denoted $x_1$, $x_2$, $x_3$, ...). The method is motivated by scenarios where many variables may be simultaneously connected to an output.
\index{data!loans|(}
\newcommand{\loNcomma}{10,000}
\newcommand{\loN}{10000}
We will consider data about loans from the peer-to-peer lender,
Lending Club, which is a data set we first encountered in
Chapters~\ref{ch_intro_to_data}
and~\ref{ch_summarizing_data}.
The loan data includes terms of the loan as well as
information about the borrower.
The outcome variable we would like to better understand
is the interest rate assigned to the loan.
For instance, all other characteristics held constant,
does it matter how much debt someone already has?
Does it matter if their income has been verified?
Multiple regression will help us answer these and other questions.
The data set \data{loans} includes results from \loNcomma{} loans,
and we'll be looking at a subset of the available variables,
some of which will be new from those we saw in earlier chapters.
The first six observations in the data set are shown in
Figure~\ref{loansDataMatrix},
and descriptions for each variable are shown in
Figure~\ref{loansVariables}.
Notice that the past bankruptcy variable (\var{bankruptcy})
is an indicator variable\index{indicator variable},
where it takes the value 1 if the borrower had a past
bankruptcy in their record and 0 if not.
Using an indicator variable in place of a category name
allows for these variables to be directly used in regression.
Two of the other variables are
categorical\index{categorical variable}
(\var{income\us{}ver} and \var{issued}), each of which
can take one of a few different non-numerical values;
we'll discuss how these are handled in the model in
Section~\ref{ind_and_cat_vars_as_predictors}.
\begin{figure}[h]
\centering\footnotesize
\begin{tabular}{r ccc ccc cc}
\hline
& interest\us{}rate & income\us{}ver
& debt\us{}to\us{}income & credit\us{}util
& bankruptcy & term
& issued & credit\us{}checks \\
\hline
1 & 14.07 & verified & 18.01 & 0.55 & 0 & 60 & Mar2018 & 6 \\
2 & 12.61 & not & 5.04 & 0.15 & 1 & 36 & Feb2018 & 1 \\
3 & 17.09 & source\_only & 21.15 & 0.66 & 0 & 36 & Feb2018 & 4 \\
4 & 6.72 & not & 10.16 & 0.20 & 0 & 36 & Jan2018 & 0 \\
5 & 14.07 & verified & 57.96 & 0.75 & 0 & 36 & Mar2018 & 7 \\
6 & 6.72 & not & 6.46 & 0.09 & 0 & 36 & Jan2018 & 6 \\
$\vdots$ & $\vdots$ & $\vdots$ &
$\vdots$ & $\vdots$ & $\vdots$ &
$\vdots$ & $\vdots$ & $\vdots$ \\
\hline
\end{tabular}
\caption{First six rows from the \data{loans} data set.}
\label{loansDataMatrix}
\end{figure}
%library(openintro) # Run some example code from loans_full_schema
%library(xtable); xtable(rbind.data.frame(head(d[, c("interest_rate", co)], 6))) #, tail(d[, c("interest_rate", co)], 2)))
\begin{figure}[h]
\centering\small
\begin{tabular}{lp{11.5cm}}
\hline
{\bf variable} & {\bf description} \\
\hline
\var{interest\us{}rate} &
Interest rate for the loan. \\
\var{income\us{}ver} &
Categorical variable describing whether the borrower's
income source and amount have been verified,
with levels \resp{verified}, \resp{source\us{}only},
and \resp{not}. \\
\var{debt\us{}to\us{}income} &
Debt-to-income ratio, which is the percentage of total debt
of the borrower divided by their total income. \\
\var{credit\us{}util} &
Of all the credit available to the borrower,
what fraction are they utilizing.
For example, the credit utilization on a credit card would
be the card's balance divided by the card's credit limit. \\
\var{bankruptcy} &
An indicator variable for whether the borrower has a past
bankruptcy in her record. This variable takes a value of
\resp{1} if the answer is ``yes''
and \resp{0} if the answer is ``no''. \\
\var{term} &
The length of the loan, in months. \\
\var{issued} &
The month and year the loan was issued,
which for these loans is always during the first
quarter of 2018. \\
\var{credit\us{}checks} &
Number of credit checks in the last 12 months.
For example, when filing an application for a credit card,
it is common for the company receiving the application
to run a credit check. \\
\hline
\end{tabular}
\caption{Variables and their descriptions for the
\data{loans} data set.}
\label{loansVariables}
\end{figure}
\newpage
\subsection{Indicator and categorical variables as predictors}
\label{ind_and_cat_vars_as_predictors}
\newcommand{\pastbankrACoef}{0.74}
\newcommand{\pastbankrACoefSE}{0.15}
Let's start by fitting a linear regression model for
interest rate with a single predictor indicating whether
or not a person has a bankruptcy in their record:
\begin{align*}
\widehat{rate} &= 12.33 + \pastbankrACoef{} \times bankruptcy
\end{align*}
Results of this model are shown in
Figure~\ref{intRateVsPastBankrModel}.
%and a scatterplot for price
%versus game condition is shown in
%Figure~\ref{intRateVsPastBankrScatter}.
\begin{figure}[h]
\centering
\begin{tabular}{l rrr r}
\hline
\vspace{-3.7mm} & & & & \\
& Estimate & Std. Error & t value & Pr($>$$|$t$|$) \\
\hline
(Intercept) & 12.3380 & 0.0533 & 231.49 & $<$0.0001 \\
bankruptcy & 0.7368 & 0.1529 & 4.82 & $<$0.0001 \\
\hline
&&&\multicolumn{2}{r}{$df=9998$}
\end{tabular}
\caption{Summary of a linear model for predicting
interest rate based on whether the borrower has
a bankruptcy in their record.}
\label{intRateVsPastBankrModel}
\end{figure}
%\begin{figure}[h]
% \centering
% \Figures{0.45}{loansSingles}{intRateVsPastBankrScatter}
% \caption{Scatterplot of interest rate against
% the past bankruptcy indicator variable.
% The least squares line is also shown,
% representing a relatively small difference
% between the two bankruptcy groups.}
% \label{intRateVsPastBankrScatter}
%\end{figure}
%\begin{exercisewrap}
%\begin{nexercise}
%Examine Figure~\ref{intRateVsPastBankrScatter}.
%Are the conditions for a linear model reasonable?\footnotemark
%\end{nexercise}
%\end{exercisewrap}
%\footnotetext{Yes. Constant variability, nearly normal residuals, and linearity all appear reasonable.}
\begin{examplewrap}
\begin{nexample}{Interpret the coefficient for the
past bankruptcy variable in the model.
Is this coefficient significantly different from 0?}
The \var{bankruptcy} variable takes one of two values:
1 when the borrower has a bankruptcy
in their history and 0 otherwise.
A slope of \pastbankrACoef{} means that the model predicts a
\pastbankrACoef{}\% higher
interest rate for those borrowers with a bankruptcy in
their record.
(See Section~\ref{categoricalPredictorsWithTwoLevels}
for a review of the interpretation for two-level
categorical predictor variables.)
Examining the regression output in
Figure~\ref{intRateVsPastBankrModel},
we can see that the p-value for \var{bankruptcy}
is very close to zero, indicating there is strong evidence
the coefficient is different from zero when using this
simple one-predictor model.
\end{nexample}
\end{examplewrap}
Suppose we had fit a model using a 3-level categorical variable,
such as \var{income\us{}ver}.
The output from software is shown in
Figure~\ref{intRateVsVerIncomeModel}.
This regression output provides multiple
rows for the \var{income\us{}ver} variable.
Each row represents the relative difference for
each level of \var{income\us{}ver}.
However, we are missing one of the levels:
\resp{not} (for \emph{not verified}).
The missing level is called the \term{reference level},
and it represents the default level that
other levels are measured against.
%This will make more sense after we write out the equation.
\begin{figure}[h]
\centering
\begin{tabular}{l rrr r}
\hline
\vspace{-3.7mm} & & & & \\
& Estimate & Std. Error & t value & Pr($>$$|$t$|$) \\
\hline
(Intercept) &
11.0995 & 0.0809 & 137.18 & $<$0.0001 \\
income\us{}ver\lmlevel{source\us{}only} &
1.4160 & 0.1107 & 12.79 & $<$0.0001 \\
income\us{}ver\lmlevel{verified} &
3.2543 & 0.1297 & 25.09 & $<$0.0001 \\
\hline
&&&\multicolumn{2}{r}{$df=9998$}
\end{tabular}
\caption{Summary of a linear model for predicting
interest rate based on whether the borrower's
income source and amount has been verified.
This predictor has three levels, which results
in 2 rows in the regression output.}
\label{intRateVsVerIncomeModel}
\end{figure}
\begin{examplewrap}
\begin{nexample}{How would we write an equation for
this regression model?}
\label{verIncomeEquationExample}%
The equation for the regression model may be written as
a model with two predictors:
\begin{align*}
\widehat{rate} = 11.10 +
1.42 \times
\indfunc{income\us{}ver}{source\us{}only} +
3.25 \times
\indfunc{income\us{}ver}{verified}
\end{align*}
We use the notation $\indfunc{variable}{level}$
to represent indicator variables\index{indicator variable}
for when the categorical variable takes a particular value.
For example, $\indfunc{income\us{}ver}{source\us{}only}$
would take a value of 1 if \var{income\us{}ver} was
\resp{source\us{}only} for a loan,
and it would take a value of 0 otherwise.
Likewise, $\indfunc{income\us{}ver}{verified}$ would take
a value of 1 if \var{income\us{}ver} took a value
of \resp{verified} and 0 if it took any other value.
% In Example~\ref{}, we'll run through a few examples
% of how we can use the equation for the model.
\end{nexample}
\end{examplewrap}
The notation used in Example~\ref{verIncomeEquationExample}
may feel a bit confusing.
Let's figure out how to use the equation for each level
of the \var{income\us{}ver} variable.
\begin{examplewrap}
\begin{nexample}{Using the model from
Example~\ref{verIncomeEquationExample},
compute the average interest rate for borrowers
whose income source and amount are both unverified.}
When \var{income\us{}ver} takes a value of \resp{not},
then both indicator functions in the equation from
Example~\ref{verIncomeEquationExample}
are set to zero:
\begin{align*}
\widehat{rate} &= 11.10 +
1.42 \times 0 +
3.25 \times 0 \\
&= 11.10
\end{align*}
The average interest rate for these borrowers is 11.1\%.
Because the \resp{not} level does not have its own
coefficient and it is the reference value,
the indicators for the other levels for this variable
all drop out.
\end{nexample}
\end{examplewrap}
\begin{examplewrap}
\begin{nexample}{Using the model from
Example~\ref{verIncomeEquationExample},
compute the average interest rate for borrowers
whose income source is verified but the amount is not.}
When \var{income\us{}ver} takes a value of
\resp{source\us{}only},
then the corresponding variable takes a value of 1
while the other ($\indfunc{income\us{}ver}{verified}$) is 0:
\begin{align*}
\widehat{rate} &= 11.10 +
1.42 \times 1 +
3.25 \times 0 \\
&= 12.52
\end{align*}
The average interest rate for these borrowers is 12.52\%.
\end{nexample}
\end{examplewrap}
\begin{exercisewrap}
\begin{nexercise}
Compute the average interest rate for borrowers
whose income source and amount are both verified.\footnotemark
\end{nexercise}
\end{exercisewrap}
\footnotetext{When \var{income\us{}ver} takes a value of
\resp{verified},
then the corresponding variable takes a value of 1
while the other ($\indfunc{income\us{}ver}{source\us{}only}$)
is~0:
\begin{align*}
\widehat{rate} &= 11.10 +
1.42 \times 0 +
3.25 \times 1 \\
&= 14.35
\end{align*}
The average interest rate for these borrowers is 14.35\%.}
\begin{onebox}{Predictors with several categories}
When fitting a regression model with a categorical variable
that has $k$ levels where $k > 2$, software will provide
a coefficient for $k - 1$ of those levels.
For the last level that does not receive a coefficient,
this is the \term{reference level}, and the coefficients
listed for the other levels are all considered relative
to this reference level.
\end{onebox}
\D{\newpage}
\begin{exercisewrap}
\begin{nexercise}
Interpret the coefficients in the \var{income\us{}ver}
model.\footnotemark
\end{nexercise}
\end{exercisewrap}
\footnotetext{Each of the coefficients gives the
incremental interest rate for the corresponding level
relative to the \resp{not} level, which is the reference
level.
For example, for a borrower whose income source and
amount have been verified, the model predicts that
they will have a 3.25\% higher interest rate than
a borrower who has not had their income source or
amount verified.}
The higher interest rate for borrowers who have verified
their income source or amount is surprising.
Intuitively, we'd think that a loan would look \emph{less}
risky if the borrower's income has been verified.
However, note that the situation may be more complex,
and there may be confounding variables
that we didn't account for.
For example, perhaps lender require borrowers with
poor credit to verify their income.
That is, verifying income in our data set might be
a signal of some concerns about the borrower
rather than a reassurance that the borrower will pay
back the loan.
For this reason, the borrower could be deemed higher
risk, resulting in a higher interest rate.
(What other confounding variables might explain this
counter-intuitive relationship suggested by the model?)
\begin{exercisewrap}
\begin{nexercise}
How much larger of an interest rate would we expect for
a borrower who has verified their income source and amount
vs a borrower whose income source has only been
verified?\footnotemark
\end{nexercise}
\end{exercisewrap}
\footnotetext{Relative to the \resp{not} category,
the \resp{verified} category has an interest rate of
3.25\% higher, while the \resp{source\us{}only}
category is only 1.42\% higher.
Thus, \resp{verified} borrowers will tend to get
an interest rate about $3.25\% - 1.42\% = 1.83\%$
higher than \resp{source\us{}only} borrowers.}
\subsection{Including and assessing many variables in a model}
\label{includingAndAssessingManyVariablesInAModel}
The world is complex, and it can be helpful to
consider many factors at once in statistical modeling.
For example, we might like to use the full context of
borrower to predict the interest rate they receive
rather than using a single variable.
This is the strategy used in
\termsub{multiple regression}{regression!multiple}.
While we remain cautious about making any causal
interpretations using multiple regression
on observational data,
such models are a common first step in gaining insights
or providing some evidence of a causal connection.
We want to construct a model that accounts for not only
for any past bankruptcy or whether the borrower had
their income source or amount verified,
but simultaneously accounts for all the variables
in the data set:
\var{income\us{}ver},
\var{debt\us{}to\us{}income},
\var{credit\us{}util},
\var{bankruptcy},
\var{term},
\var{issued},
and \var{credit\us{}checks}.
\begin{align*}
\widehat{\var{rate}}
&= \beta_0 +
\beta_1\times \indfunc{income\us{}ver}{source\us{}only} +
\beta_2\times \indfunc{income\us{}ver}{verified} +
\beta_3\times \var{debt\us{}to\us{}income} \\
&\qquad\ +
\beta_4 \times \var{credit\us{}util} +
\beta_5 \times \var{bankruptcy} +
\beta_6 \times \var{term} \\
&\qquad\ +
\beta_7 \times \indfunc{issued}{Jan2018} +
\beta_8 \times \indfunc{issued}{Mar2018} +
\beta_9 \times \var{credit\us{}checks}
\end{align*}
This equation represents a holistic approach for modeling
all of the variables simultaneously.
Notice that there are two coefficients for \var{income\us{}ver}
and also two coefficients for \var{issued}, since both are
3-level categorical variables.
%\Comment{Work on this paragraph.}
%A multiple regression model may be missing important components or it might not precisely represent the relationship between the outcome and the available explanatory variables. While no model is perfect, we wish to explore the possibility that this one may fit the data reasonably well.
We estimate the parameters
$\beta_0$, $\beta_1$, $\beta_2$, ..., $\beta_9$
in the same way as we did in the case of a single predictor.
We select $b_0$, $b_1$, $b_2$, ..., $b_9$ that minimize the
sum of the squared residuals:
\begin{align}\label{sumOfSqResInMultRegr}
SSE = e_1^2 + e_2^2 + \dots + e_{\loN}^2
= \sum_{i=1}^{\loN} e_i^2
= \sum_{i=1}^{\loN} \left(y_i - \hat{y}_i\right)^2
\end{align}
where $y_i$ and $\hat{y}_i$ represent the observed
interest rates and their estimated values according to
the model, respectively.
\loNcomma{} residuals are calculated, one for each observation.
We typically use a computer to minimize the sum of squares
and compute point estimates, as shown in the sample output
in Figure~\ref{loansFullModelOutput}.
Using this output, we identify the point estimates $b_i$ of
each $\beta_i$, just as we did in the one-predictor case.
\newcommand{\pastbankrFullCoef}{0.39}
\newcommand{\pastbankrFullCoefSE}{0.13}
\begin{figure}[ht]
\centering
\begin{tabular}{rrrrr}
\hline
\vspace{-3.7mm} & & & & \\
& Estimate & Std. Error & t value & Pr($>$$|$t$|$) \\
\hline
\vspace{-3.8mm} & & & & \\
(Intercept) & 1.9251 & 0.2102 & 9.16 & $<$0.0001 \\
income\us{}ver\lmlevel{source\us{}only} &
0.9750 & 0.0991 & 9.83 & $<$0.0001 \\
income\us{}ver\lmlevel{verified} &
2.5374 & 0.1172 & 21.65 & $<$0.0001 \\
debt\us{}to\us{}income & 0.0211 & 0.0029 & 7.18 & $<$0.0001 \\
credit\us{}util & 4.8959 & 0.1619 & 30.24 & $<$0.0001 \\
bankruptcy & 0.3864 & 0.1324 & 2.92 & 0.0035 \\
term & 0.1537 & 0.0039 & 38.96 & $<$0.0001 \\
issued\lmlevel{Jan2018} & 0.0276 & 0.1081 & 0.26 & 0.7981 \\
issued\lmlevel{Mar2018} & -0.0397 & 0.1065 & -0.37 & 0.7093 \\
credit\us{}checks & 0.2282 & 0.0182 & 12.51 & $<$0.0001 \\
\hline
&&&\multicolumn{2}{r}{$df=9990$}
\end{tabular}
\caption{Output for the regression model, where
\var{interest\us{}rate} is the outcome and
the variables listed are the predictors.}
\label{loansFullModelOutput}
\end{figure}
\begin{onebox}{Multiple regression model}
A multiple regression model is a linear model
with many predictors.
In general, we write the model as
\begin{align*}
\hat{y} =
\beta_0 + \beta_1 x_1 + \beta_2 x_2 + \cdots + \beta_k x_k
\end{align*}
when there are $k$ predictors.
We always estimate the $\beta_i$ parameters using
statistical software.
\end{onebox}
\begin{examplewrap}
\begin{nexample}{Write out the regression model using
the point estimates from
Figure~\ref{loansFullModelOutput}.
How many predictors are there in this model?}
\label{loansFullModelEqWCoef}%
The fitted model for the interest rate is given by:
{\small\begin{align*}
\widehat{\var{rate}}
&= 1.925 +
0.975 \times \indfunc{income\us{}ver}{source\us{}only} +
2.537 \times \indfunc{income\us{}ver}{verified} +
0.021 \times \var{debt\us{}to\us{}income} \\
&\qquad\ +
4.896 \times \var{credit\us{}util} +
0.386 \times \var{bankruptcy} +
0.154 \times \var{term} \\
&\qquad\ +
0.028 \times \indfunc{issued}{Jan2018}
-0.040 \times \indfunc{issued}{Mar2018} +
0.228 \times \var{credit\us{}checks}
\end{align*}}%
If we count up the number of predictor coefficients,
we get the \emph{effective} number of predictors
in the model:~$k = 9$.
Notice that the \var{issued} categorical predictor
counts as two, once for the two levels shown in the model.
In general, a categorical predictor with $p$ different
levels will be represented by $p - 1$ terms in a multiple
regression model.
\end{nexample}
\end{examplewrap}
\begin{exercisewrap}
\begin{nexercise}
What does $\beta_4$, the coefficient of variable
\var{credit\us{}util}, represent?
What is the point estimate of~$\beta_4$?\footnotemark
\end{nexercise}
\end{exercisewrap}
\footnotetext{$\beta_4$ represents the change in
interest rate we would expect if someone's credit
utilization was 0 and went to 1,
all other factors held even.
The point estimate is $b_4 = 4.90\%$.}
\D{\newpage}
\begin{examplewrap}
\begin{nexample}{Compute the residual of the first observation
in Figure~\ref{loansDataMatrix} on
page~\pageref{loansDataMatrix} using the equation identified
in Guided Practice~\ref{loansFullModelEqWCoef}.}
To compute the residual, we first need the predicted value,
which we compute by plugging values into the equation from
Example~\ref{loansFullModelEqWCoef}.
For example, $\indfunc{income\us{}ver}{source\us{}only}$
takes a value of 0,
$\indfunc{income\us{}ver}{verified}$ takes a value of 1
(since the borrower's income source and amount were verified),
\var{debt\us{}to\us{}income} was 18.01, and so on.
This leads to a prediction of $\widehat{rate}_1 = 18.09$.
The observed interest rate was 14.07\%, which leads to
a residual of $e_1 = 14.07 - 18.09 = -4.02$.
\end{nexample}
\end{examplewrap}
% sum(model.matrix(m)[1, ] * round(m$coef, 3))
\begin{examplewrap}
\begin{nexample}{We estimated a coefficient for
\var{bankruptcy} in
Section~\ref{ind_and_cat_vars_as_predictors}
of $b_4 = \pastbankrACoef{}$ with a standard error
of $SE_{b_1} = \pastbankrACoefSE{}$ when using simple
linear regression.
Why is there a difference between that estimate
and the estimated coefficient of \pastbankrFullCoef{}
in the multiple regression setting?}
\label{pastBankrCoefDiffExplained}%
If we examined the data carefully, we would see that
some predictors are correlated.
For instance, when we estimated the connection of the
outcome \var{interest\us{}rate} and predictor
\var{bankruptcy} using simple linear regression,
we were unable to control for other variables like
whether the borrower had her income verified,
the borrower's debt-to-income ratio, and other variables.
That original model was constructed in a vacuum and did
not consider the full context.
When we include all of the variables,
underlying and unintentional
bias that was missed by these other variables is reduced
or eliminated.
Of course, bias can still exist from other confounding
variables.
\end{nexample}
\end{examplewrap}
Example~\ref{pastBankrCoefDiffExplained} describes a common
issue in multiple regression: correlation among predictor
variables.
We say the two predictor variables are \term{collinear}
(pronounced as \emph{co-linear}) when they are correlated,
and this collinearity complicates model estimation.
While it is impossible to prevent collinearity from arising
in observational data, experiments are usually designed to
prevent predictors from being collinear.
\begin{exercisewrap}
\begin{nexercise}
The estimated value of the intercept is 1.925, and one might
be tempted to make some interpretation of this coefficient,
such as, it is the model's predicted price when each of the
variables take value zero: income source is not verified,
the borrower has no debt (debt-to-income and credit
utilization are zero), and so on.
Is this reasonable?
Is there any value gained by making this
interpretation?\footnotemark
\end{nexercise}
\end{exercisewrap}
\footnotetext{Many of the variables do take a value 0
for at least one data point, and for those variables,
it is reasonable.
However, one variable never takes a value of zero:
\var{term}, which describes the length of the loan,
in months.
If \var{term} is set to zero, then the loan
must be paid back immediately; the borrower
must give the money back as soon as she receives it,
which means it is not a real loan.
Ultimately, the interpretation of the intercept in
this setting is not insightful.}
\D{\newpage}
\subsection[Adjusted $R^2$ as a better tool
for multiple regression]
{Adjusted $\pmb{R^2}$ as a better tool
for multiple regression}
\index{adjusted r squared@adjusted $R^2$ ($R_{adj}^2$)|(}
We first used $R^2$ in Section~\ref{fittingALineByLSR}
to determine the amount of variability in the response
that was explained by the model:
\begin{align*}
R^2 =
1 - \frac{\text{variability in residuals}}
{\text{variability in the outcome}}
= 1 - \frac{Var(e_i)}{Var(y_i)}
\end{align*}
where $e_i$ represents the residuals of the model and
$y_i$ the outcomes.
This equation remains valid in the multiple regression
framework, but a small enhancement can make it even
more informative when comparing models.
\begin{exercisewrap}
\begin{nexercise}
\label{computeUnadjR2ForFullLoansModel}%
The variance of the residuals for the model given in
Guided Practice~\ref{loansFullModelEqWCoef}
is 18.53, and the variance of the total price in all
the auctions is 25.01.
Calculate $R^2$ for this model.\footnotemark
\end{nexercise}
\end{exercisewrap}
\footnotetext{$R^2 = 1 - \frac{18.53}{25.01} = 0.2591$.}
This strategy for estimating $R^2$ is acceptable when there
is just a single variable.
However, it becomes less helpful when there are many
variables.
The regular $R^2$ is a biased estimate of the amount of
variability explained by the model
when applied to a new sample of data.
To get a better estimate, we use the adjusted $R^2$.
\begin{onebox}{Adjusted $\pmb{R^2}$ as a tool for
model assessment}
The \termsub{adjusted $\pmb{R^2}$}
{adjusted r squared@adjusted $R^2$ ($R_{adj}^2$)}
is computed as
\begin{align*}
R_{adj}^{2}
= 1 - \frac{s_{\text{residuals}}^2 / (n-k-1)}
{s_{\text{outcome}}^2 / (n-1)}
= 1 - \frac{s_{\text{residuals}}^2}{s_{\text{outcome}}^2}
\times \frac{n-1}{n-k-1}
\end{align*}
where $n$ is the number of cases used to fit the model
and $k$ is the number of predictor variables in the model.
Remember that a categorical predictor with $p$ levels will
contribute $p - 1$ to the number of variables in the model.
\end{onebox}
Because $k$ is never negative, the adjusted $R^2$ will be
smaller -- often times just a little smaller -- than the
unadjusted $R^2$.
The reasoning behind the adjusted $R^2$ lies in the
\termsub{degrees of freedom}{degrees of freedom (df)!regression}
associated with each variance,
which is equal to $n - k - 1$ for the multiple regression
context.
If we were to make predictions for \emph{new data}
using our current model, we would find that the unadjusted
$R^2$ would tend to be slightly overly optimistic, while
the adjusted $R^2$ formula helps correct this bias.
\begin{exercisewrap}
\begin{nexercise}
There were $n=10000$ auctions in the \data{loans} data set
and $k=9$ predictor variables in the model.
Use $n$, $k$, and the variances from
Guided Practice~\ref{computeUnadjR2ForFullLoansModel}
to calculate $R_{adj}^2$ for the interest rate
model.\footnotemark
\end{nexercise}
\end{exercisewrap}
\footnotetext{$R_{adj}^2
= 1 - \frac{18.53}{25.01}\times \frac{10000-1}{1000-9-1}
= 0.2584$.
While the difference is very small, it will be important
when we fine tune the model in the next section.}
\begin{exercisewrap}
\begin{nexercise}
Suppose you added another predictor to the model, but the
variance of the errors $Var(e_i)$ didn't go down.
What would happen to the~$R^2$?
What would happen to the
adjusted~$R^2$?\hspace{0.7mm}\footnotemark
\end{nexercise}
\end{exercisewrap}
\footnotetext{The unadjusted $R^2$ would stay the same
and the adjusted $R^2$ would go down.}
Adjusted $R^2$ could have been used in
Chapter~\ref{linRegrForTwoVar}.
However, when there is only $k = 1$ predictors,
adjusted $R^2$ is very close to regular $R^2$,
so this nuance isn't typically important when
the model has only one predictor.
\index{adjusted r squared@adjusted $R^2$ ($R_{adj}^2$)|)}
{\input{ch_regr_mult_and_log/TeX/introduction_to_multiple_regression.tex}}
%__________________
\section{Model selection}
\label{model_selection_section}
\label{modelSelection}
\index{model selection|(}
The best model is not always the most complicated.
Sometimes including variables that are not evidently
important can actually reduce the accuracy of predictions.
In this section, we discuss model selection strategies,
which will help us eliminate variables from the model that
are found to be less important.
It's common (and hip, at least in the statistical world)
to refer to models that have undergone such variable pruning
as \term{parsimonious}.
In practice, the model that includes all available explanatory
variables is often referred to as the \term{full model}.
The full model may not be the best model, and if it isn't,
we want to identify a smaller model that is preferable.
\subsection{Identifying variables in the model that may
not be helpful}
Adjusted $R^2$ describes the strength of a model fit,
and it is a useful tool for evaluating which predictors
are adding value to the model, where \emph{adding value}
means they are (likely) improving the accuracy in
predicting future outcomes.
Let's consider two models, which are shown in
Tables~\ref{loansFullModelModelSelectionSection}
and~\ref{loansModelAllButIssued}.
The first table summarizes the full model since it includes
all predictors, while the second does not include the
\var{issued} variable.
\begin{figure}[ht]
\centering
\begin{tabular}{rrrrr}
\hline
\vspace{-3.7mm} & & & & \\
& Estimate & Std. Error & t value & Pr($>$$|$t$|$) \\
\hline
\vspace{-3.8mm} & & & & \\
(Intercept) & 1.9251 & 0.2102 & 9.16 & $<$0.0001 \\
income\us{}ver\lmlevel{source\us{}only} &
0.9750 & 0.0991 & 9.83 & $<$0.0001 \\
income\us{}ver\lmlevel{verified} &
2.5374 & 0.1172 & 21.65 & $<$0.0001 \\
debt\us{}to\us{}income & 0.0211 & 0.0029 & 7.18 & $<$0.0001 \\
credit\us{}util & 4.8959 & 0.1619 & 30.24 & $<$0.0001 \\
bankruptcy & 0.3864 & 0.1324 & 2.92 & 0.0035 \\
term & 0.1537 & 0.0039 & 38.96 & $<$0.0001 \\
issued\lmlevel{Jan2018} & 0.0276 & 0.1081 & 0.26 & 0.7981 \\
issued\lmlevel{Mar2018} & -0.0397 & 0.1065 & -0.37 & 0.7093 \\
credit\us{}checks & 0.2282 & 0.0182 & 12.51 & $<$0.0001 \\
\hline
\multicolumn{3}{l}{$R_{adj}^2 = 0.25843$}&
\multicolumn{2}{r}{$df=9990$}
\end{tabular}
\caption{The fit for the full regression model,
including the adjusted $R^2$.}
\label{loansFullModelModelSelectionSection}
\end{figure}
\begin{figure}[ht]
\centering
\begin{tabular}{rrrrr}
\hline
\vspace{-3.7mm} & & & & \\
& Estimate & Std. Error & t value & Pr($>$$|$t$|$) \\
\hline
\vspace{-3.8mm} & & & & \\
(Intercept) & 1.9213 & 0.1982 & 9.69 & $<$0.0001 \\
income\us{}ver\lmlevel{source\us{}only} &
0.9740 & 0.0991 & 9.83 & $<$0.0001 \\
income\us{}ver\lmlevel{verified} &
2.5355 & 0.1172 & 21.64 & $<$0.0001 \\
debt\us{}to\us{}income & 0.0211 & 0.0029 & 7.19 & $<$0.0001 \\
credit\us{}util & 4.8958 & 0.1619 & 30.25 & $<$0.0001 \\
bankruptcy & 0.3869 & 0.1324 & 2.92 & 0.0035 \\
term & 0.1537 & 0.0039 & 38.97 & $<$0.0001 \\
credit\us{}checks & 0.2283 & 0.0182 & 12.51 & $<$0.0001 \\
\hline
\vspace{-3.6mm} & & & & \\
\multicolumn{3}{l}{$R_{adj}^2 = 0.25854$}&
\multicolumn{2}{r}{$df=9992$}
\end{tabular}
\caption{The fit for the regression model after dropping
the \var{issued} variable.} %, which represented 3 categories
% and 2 degrees of freedom.}
\label{loansModelAllButIssued}
\end{figure}
\begin{examplewrap}
\begin{nexample}{Which of the two models is better?}
We compare the adjusted $R^2$ of each model to determine
which to choose.
Since the first model has an $R^2_{adj}$ smaller than
the $R^2_{adj}$ of the second model, we prefer the second
model to the first.
\end{nexample}
\end{examplewrap}
Will the model without \var{issued} be better than the
model with \var{issued}?
We~cannot know for sure, but based on the adjusted $R^2$,
this is our best assessment.
\subsection{Two model selection strategies}
Two common strategies for adding or removing variables
in a multiple regression model are called
\emph{backward elimination} and \emph{forward selection}.
These techniques are often referred to as \term{stepwise}
model selection strategies, because they add or delete
one variable at a time as they ``step'' through the
candidate predictors.
\termsub{Backward elimination}{backward elimination}
starts with the model that includes all potential
predictor variables.
Variables are eliminated one-at-a-time from the model
until we cannot improve the adjusted $R^2$.
The strategy within each elimination step is to eliminate
the variable that leads to the largest improvement in
adjusted $R^2$.
\begin{examplewrap}
\begin{nexample}{Results corresponding to the \emph{full model}
for the \data{loans} data are shown in
Figure~\ref{loansFullModelModelSelectionSection}.
How should we proceed under the backward elimination
strategy?}
\label{loansBackwardElimEx}%
Our baseline adjusted $R^2$ from the full model is
$R^2_{adj} = 0.25843$, and we need to determine whether
dropping a predictor will improve the adjusted $R^2$.
To check, we fit models that each drop a different
predictor, and we record the adjusted $R^2$:
\begin{center}
\begin{tabular}{lllll}
Exclude ... &
\var{income\us{}ver} &
\var{debt\us{}to\us{}income} &
\var{credit\us{}util} &
\var{bankruptcy} \\
&
$R^2_{adj} = 0.22380$ &
$R^2_{adj} = 0.25468$ &
$R^2_{adj} = 0.19063$ &
$R^2_{adj} = 0.25787$ \\
\\
&
\var{term} &
\var{issued} &
\var{credit\us{}checks} \\
&
$R^2_{adj} = 0.14581$ &
$R^2_{adj} = 0.25854$ &
$R^2_{adj} = 0.24689$ \\
\end{tabular}
\end{center}
The model without \var{issued} has the highest adjusted $R^2$
of 0.25854, higher than the adjusted $R^2$ for the full model.
Because eliminating \var{issued} leads to a model with
a higher adjusted $R^2$, we drop \var{issued} from the model.
Since we eliminated a predictor from the model in the first step,
we see whether we should eliminate any additional predictors.
Our baseline adjusted $R^2$ is now $R^2_{adj} = 0.25854$.
We now fit new models, which consider eliminating each of the
remaining predictors in addition to \var{issued}:
\begin{center}
\begin{tabular}{llll}
Exclude \var{issued} and ... &
\var{income\us{}ver} &
\var{debt\us{}to\us{}income} &
\var{credit\us{}util} \\
&
$R^2_{adj} = 0.22395$ &
$R^2_{adj} = 0.25479$ &
$R^2_{adj} = 0.19074$ \\
\\
&
\var{bankruptcy} &
\var{term} &
\var{credit\us{}checks} \\
&
$R^2_{adj} = 0.25798$ &
$R^2_{adj} = 0.14592$ &
$R^2_{adj} = 0.24701$ \\
\end{tabular}
\end{center}
None of these models lead to an improvement in adjusted $R^2$,
so we do not eliminate any of the remaining predictors.
That is, after backward elimination, we are left with the
model that keeps all predictors except \var{issued},
which we can summarize using the coefficients from
Figure~\ref{loansModelAllButIssued}:
\begin{align*}