multiple linear regression - statistical sciencercs46/lectures_2017/03-lr/03-mlr.pdf ·...

49
Multiple Linear Regression Rebecca C. Steorts, Duke University STA 325, Chapter 3 ISL 1 / 49

Upload: others

Post on 07-Aug-2020

12 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Multiple Linear Regression - Statistical Sciencercs46/lectures_2017/03-lr/03-mlr.pdf · HistogramsofBostondata crim Boston[, i] Density 0 20 40 60 80 0.00 0.06 zn Boston[, i] Density

Multiple Linear Regression

Rebecca C. Steorts, Duke University

STA 325, Chapter 3 ISL

1 / 49

Page 2: Multiple Linear Regression - Statistical Sciencercs46/lectures_2017/03-lr/03-mlr.pdf · HistogramsofBostondata crim Boston[, i] Density 0 20 40 60 80 0.00 0.06 zn Boston[, i] Density

Agenda

I How to extend beyond a SLRI Multiple Linear Regression (MLR)I Relationship Between the Response and PredictorsI Model Selection: Forward SelectionI Model FitI PredictionsI ApplicationI Qualitative Predictors with More than Two LevelsI Additivity, Interactions, Polynomial RegressionI Other Issues

2 / 49

Page 3: Multiple Linear Regression - Statistical Sciencercs46/lectures_2017/03-lr/03-mlr.pdf · HistogramsofBostondata crim Boston[, i] Density 0 20 40 60 80 0.00 0.06 zn Boston[, i] Density

Multiple Linear Regression (MLR)

I SLR is a useful approach for predicting a response on the basisof a single predictor variable.

I However, in practice we often have more than one predictor.

3 / 49

Page 4: Multiple Linear Regression - Statistical Sciencercs46/lectures_2017/03-lr/03-mlr.pdf · HistogramsofBostondata crim Boston[, i] Density 0 20 40 60 80 0.00 0.06 zn Boston[, i] Density

Advertising data set

I In the Advertising data, we have examined the relationshipbetween sales and TV advertising.

I We also have data for the amount of money spent advertisingon the radio and in newspapers.

I How can we extend our analysis of the advertising data in orderto accommodate these two additional predictors?

4 / 49

Page 5: Multiple Linear Regression - Statistical Sciencercs46/lectures_2017/03-lr/03-mlr.pdf · HistogramsofBostondata crim Boston[, i] Density 0 20 40 60 80 0.00 0.06 zn Boston[, i] Density

Run many SLR’s!

We could run three SLRs. Not a good idea! Why?

1. It is unclear how to make a single prediction of sales givenlevels of the three advertising media budgets, since each of thebudgets is associated with a separate regression equation.

2. Each of the three regression equations ignores the other twomedia in forming estimates for the regression coefficients.

5 / 49

Page 6: Multiple Linear Regression - Statistical Sciencercs46/lectures_2017/03-lr/03-mlr.pdf · HistogramsofBostondata crim Boston[, i] Density 0 20 40 60 80 0.00 0.06 zn Boston[, i] Density

MLR

Suppose we have p preditors. Then the MLR takes the form

Y = βo + β1X1 + · · ·βpXp + ε, (1)

where

I Xj represents the jth predictorI βj quantifies the association between that predictor and the

responseI We interpret βj as the average effect on Y of a one unit

increase in Xj , holding all other predictors fixed.

6 / 49

Page 7: Multiple Linear Regression - Statistical Sciencercs46/lectures_2017/03-lr/03-mlr.pdf · HistogramsofBostondata crim Boston[, i] Density 0 20 40 60 80 0.00 0.06 zn Boston[, i] Density

Advertising

In the advertising example, the MLR becomes

sales = βo + β1 × TV + β2 × radio + β3 × newspaper + ε

7 / 49

Page 8: Multiple Linear Regression - Statistical Sciencercs46/lectures_2017/03-lr/03-mlr.pdf · HistogramsofBostondata crim Boston[, i] Density 0 20 40 60 80 0.00 0.06 zn Boston[, i] Density

Estimating the Regression Coefficients

As was the case in SLR, βo, β1, . . . , βp are unknown and must beestimated by βo, β1, . . . , βp.

Given the estimated coefficients (found by minimizing the RSS), wecan make predictions using

y = βo + β1X1 + · · · + βpXp.

Solving for the LSE is beyond the scope of this class given thecalculations are quite tedious and require matrix algebra.

8 / 49

Page 9: Multiple Linear Regression - Statistical Sciencercs46/lectures_2017/03-lr/03-mlr.pdf · HistogramsofBostondata crim Boston[, i] Density 0 20 40 60 80 0.00 0.06 zn Boston[, i] Density

Important Questions to Keep in Mind

1. Is at least one of the predictors (X) useful in predicting theresponse?

2. Do all the predictors help to explain Y, or is only a subset ofthe predictors useful?

3. How well does the model fit the data?4. Given a set of predictor values, what response value should we

predict, and how accurate is our prediction?

We address these now, point by point.

9 / 49

Page 10: Multiple Linear Regression - Statistical Sciencercs46/lectures_2017/03-lr/03-mlr.pdf · HistogramsofBostondata crim Boston[, i] Density 0 20 40 60 80 0.00 0.06 zn Boston[, i] Density

Relationship Between the Response and PredictorsI In the SLR setting, to determine whether there is a relationship

between the response and the predictor we simply checkwhether β1 = 0.

I Are all of the regression coefficients zero?

Ho : βo = β1 = · · · = βp = 0 (2)Ha : at least one βj is non-zero. (3)

The hypothesis test if performed by computing the F-statistic:

F = (TSS − RSS)/pRSS/(n − p − 1) .

Look at p-value and then make a conclusion.Remark: Suppose p > n, there are more coefficients βj to estimatethan observations from which to estimate them. One simpleapproach to overcome this issue is forward selection. This highdimensional setting is discussed more in Chapter 6. 10 / 49

Page 11: Multiple Linear Regression - Statistical Sciencercs46/lectures_2017/03-lr/03-mlr.pdf · HistogramsofBostondata crim Boston[, i] Density 0 20 40 60 80 0.00 0.06 zn Boston[, i] Density

Deciding on Important Variables

The first step in a multiple regression analysis is to compute theF-statistic and to examine the associated p-value.

If we conclude on the basis of that p-value that at least one of thepredictors is related to the response, then it is natural to wonderwhich are the guilty ones!

The task of determining which predictors are associated with theresponse, in order to fit a single model involving only thosepredictors, is referred to as variable selection. (See Chapter 6 fora more in depth discussion).

11 / 49

Page 12: Multiple Linear Regression - Statistical Sciencercs46/lectures_2017/03-lr/03-mlr.pdf · HistogramsofBostondata crim Boston[, i] Density 0 20 40 60 80 0.00 0.06 zn Boston[, i] Density

Model selection

It is infeasible to look at all possible models since there are 2p

models that contain subsets of p features.

For example, if p = 30, then we must consider 230 = 1,073,741,824models!

We will focus on forward selection since it can be used in highdimensional settings.

12 / 49

Page 13: Multiple Linear Regression - Statistical Sciencercs46/lectures_2017/03-lr/03-mlr.pdf · HistogramsofBostondata crim Boston[, i] Density 0 20 40 60 80 0.00 0.06 zn Boston[, i] Density

Forward selection

1. We begin with the null model. This is the that contains anintercept but no predictors.

2. We then fit p simple linear regressions and add to the nullmodel the variable that results in the lowest RSS.

3. We then add to that model the variable that results in thelowest RSS for the new two-variable model.

4. This approach is continued until some stopping rule is satisfied.

Remark: Forward selection is a greedy approach, and mightinclude variables early that later become redundant.

13 / 49

Page 14: Multiple Linear Regression - Statistical Sciencercs46/lectures_2017/03-lr/03-mlr.pdf · HistogramsofBostondata crim Boston[, i] Density 0 20 40 60 80 0.00 0.06 zn Boston[, i] Density

Model Fit

Two of the most common numerical measures of model fit are theRSE and R2, the fraction of variance explained.

These quantities are computed and interpreted in the same fashionas for simple linear regression.

Remark: It turns out that R2 will always increase when morevariables are added to the model, even if those variables are onlyweakly associated with the response.

I This is due to the fact that adding another variable to the leastsquares equations must allow us to fit the training data(though not necessarily the testing data) more accurately.

14 / 49

Page 15: Multiple Linear Regression - Statistical Sciencercs46/lectures_2017/03-lr/03-mlr.pdf · HistogramsofBostondata crim Boston[, i] Density 0 20 40 60 80 0.00 0.06 zn Boston[, i] Density

Predictions

Predictions are simple to make. But what is the uncertainty aboutour predictions?

1. The inaccuracy in the coefficient estimates is related to thereducible error from Chapter 2. We can computer a confidenceinterval in order to see how close y is to f (X )

2. Assume that f (X ) is a linear model is almost never correct, sothere is an additional source of reducible error here, calledmodel bias.

3. We can calculate prediction intervals (recall these are alwayswider than confidence intervals).

15 / 49

Page 16: Multiple Linear Regression - Statistical Sciencercs46/lectures_2017/03-lr/03-mlr.pdf · HistogramsofBostondata crim Boston[, i] Density 0 20 40 60 80 0.00 0.06 zn Boston[, i] Density

MRL for Advertising data

I In order to fit a MLR using least squares we again use the lm()function.

I The syntax lm(y ∼ x1 + x2 + x3) is used to fit a model withthree predictors.

I The summary()function outputs the regression coefficients forthe predictors.

16 / 49

Page 17: Multiple Linear Regression - Statistical Sciencercs46/lectures_2017/03-lr/03-mlr.pdf · HistogramsofBostondata crim Boston[, i] Density 0 20 40 60 80 0.00 0.06 zn Boston[, i] Density

MRL for Advertising data

attach(Boston)names(Boston)

## [1] "crim" "zn" "indus" "chas" "nox" "rm" "age"## [8] "dis" "rad" "tax" "ptratio" "black" "lstat" "medv"

dim(Boston)

## [1] 506 14

17 / 49

Page 18: Multiple Linear Regression - Statistical Sciencercs46/lectures_2017/03-lr/03-mlr.pdf · HistogramsofBostondata crim Boston[, i] Density 0 20 40 60 80 0.00 0.06 zn Boston[, i] Density

Histograms of Boston datacrim

Boston[, i]

Den

sity

0 20 40 60 80

0.00

0.06

zn

Boston[, i]

Den

sity

0 20 40 60 80 100

0.00

0.06

indus

Boston[, i]

Den

sity

0 5 10 15 20 25

0.00

0.10

chas

Boston[, i]

Den

sity

0.0 0.2 0.4 0.6 0.8 1.0

04

8

nox

Boston[, i]

Den

sity

0.4 0.5 0.6 0.7 0.8 0.9

02

4

rm

Boston[, i]

Den

sity

4 5 6 7 8 9

0.0

0.4

age

Boston[, i]

Den

sity

0 20 40 60 80 100

0.00

00.

025

dis

Boston[, i]

Den

sity

2 4 6 8 10 12

0.00

0.20

rad

Boston[, i]

Den

sity

0 5 10 15 20

0.00

0.10

tax

Boston[, i]

Den

sity

200 400 600

0.00

00.

004

ptratio

Boston[, i]D

ensi

ty

12 14 16 18 20 22

0.0

0.2

black

Boston[, i]

Den

sity

0 100 200 300 400

0.00

00.

015

lstat

Boston[, i]

Den

sity

0 10 20 30 40

0.00

0.04

medv

Boston[, i]

Den

sity

10 20 30 40 50

0.00

0.05

18 / 49

Page 19: Multiple Linear Regression - Statistical Sciencercs46/lectures_2017/03-lr/03-mlr.pdf · HistogramsofBostondata crim Boston[, i] Density 0 20 40 60 80 0.00 0.06 zn Boston[, i] Density

MRL for Advertising datalm.fit <- lm(medv~., data=Boston)summary(lm.fit)

#### Call:## lm(formula = medv ~ ., data = Boston)#### Residuals:## Min 1Q Median 3Q Max## -15.595 -2.730 -0.518 1.777 26.199#### Coefficients:## Estimate Std. Error t value Pr(>|t|)## (Intercept) 3.646e+01 5.103e+00 7.144 3.28e-12 ***## crim -1.080e-01 3.286e-02 -3.287 0.001087 **## zn 4.642e-02 1.373e-02 3.382 0.000778 ***## indus 2.056e-02 6.150e-02 0.334 0.738288## chas 2.687e+00 8.616e-01 3.118 0.001925 **## nox -1.777e+01 3.820e+00 -4.651 4.25e-06 ***## rm 3.810e+00 4.179e-01 9.116 < 2e-16 ***## age 6.922e-04 1.321e-02 0.052 0.958229## dis -1.476e+00 1.995e-01 -7.398 6.01e-13 ***## rad 3.060e-01 6.635e-02 4.613 5.07e-06 ***## tax -1.233e-02 3.760e-03 -3.280 0.001112 **## ptratio -9.527e-01 1.308e-01 -7.283 1.31e-12 ***## black 9.312e-03 2.686e-03 3.467 0.000573 ***## lstat -5.248e-01 5.072e-02 -10.347 < 2e-16 ***## ---## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1#### Residual standard error: 4.745 on 492 degrees of freedom## Multiple R-squared: 0.7406, Adjusted R-squared: 0.7338## F-statistic: 108.1 on 13 and 492 DF, p-value: < 2.2e-16

19 / 49

Page 20: Multiple Linear Regression - Statistical Sciencercs46/lectures_2017/03-lr/03-mlr.pdf · HistogramsofBostondata crim Boston[, i] Density 0 20 40 60 80 0.00 0.06 zn Boston[, i] Density

MRL for Advertising dataWe can access each part of summary by typing ?summary.lm to seewhat is available#r2summary(lm.fit)$r.sq

## [1] 0.7406427

#RMSEsummary(lm.fit)$sigma

## [1] 4.745298

summary(lm.fit)$residuals

## 1 2 3 4 5## -6.003843377 -3.425562379 4.132403281 4.792963511 8.256475767## 6 7 8 9 10## 3.443715538 -0.101808268 7.564011571 4.976363147 -0.020262107## 11 12 13 14 15## -3.999496511 -2.686795681 0.793478472 0.847097189 -1.083482050## 16 17 18 19 20## 0.602516792 2.572490209 0.588598653 4.021988943 -0.206136033## 21 22 23 24 25## 1.076142473 1.928963305 -0.632881292 0.693714654 -0.078338315## 26 27 28 29 30## 0.513314391 1.136023454 0.091525719 -1.147372851 0.123571798## 31 32 33 34 35## 1.244882410 -3.559232946 4.388942638 -1.182758141 -0.206758913## 36 37 38 39 40## -4.914635265 -2.341937076 -2.108911425 1.784973884 -0.557625688## 41 42 43 44 45## 0.684897746 -1.420564139 0.096133720 0.090207275 -1.741491757## 46 47 48 49 50## -2.796698175 -0.423200323 -1.436550884 5.293446228 2.193922486## 51 52 53 54 55## -1.581525353 -3.472222849 -2.655850802 -0.649018091 3.538152299## 56 57 58 59 60## 4.247350534 -0.156869782 -1.509198062 1.524620129 -1.484935551## 61 62 63 64 65## 0.827419603 -2.511102080 -1.787428565 2.445911311 9.626913558## 66 67 68 69 70## -6.861483581 -6.130565115 0.886614358 -0.021537857 0.115163673## 71 72 73 74 75## -1.001488594 -0.042657705 -1.757449572 -0.642957120 -1.404997164## 76 77 78 79 80## -2.566930200 -2.945454031 -2.556998184 -0.061982662 -2.128173732## 81 82 83 84 85## -0.405769682 -3.094860862 -1.235762968 -2.158734819 -0.884566738## 86 87 88 89 90## -1.190491951 0.331465769 -3.692764153 -7.074618272 -2.131106227## 91 92 93 94 95## -4.519019396 -5.412667341 -6.041227624 -4.081055463 -6.439773649## 96 97 98 99 100## -0.224599485 -3.327449776 2.918404819 8.685454127 0.948971987## 101 102 103 104 105## 2.919779811 0.905865254 -1.190136835 -1.011671288 -1.334825911## 106 107 108 109 110## 0.960059918 2.312440082 -0.350490259 -2.848291146 -0.372036695## 111 112 113 114 115## 1.050341357 -3.725867438 -1.973236377 -2.015483150 -6.672088813## 116 117 118 119 120## -2.130255909 -2.177246257 -4.490432606 0.064216357 -1.491808732## 121 122 123 124 125## 0.083679289 -2.171077767 -0.057385558 0.933380226 -1.760998192## 126 127 128 129 130## -1.081784462 1.082933672 1.021233160 -0.938685924 0.244267149## 131 132 133 134 135## -0.835273989 0.189865979 2.938084338 2.641923269 2.343547618## 136 137 138 139 140## 0.837222650 1.521581166 -2.261639540 -0.514838967 1.351185252## 141 142 143 144 145## 0.428580681 10.411144917 -1.194954780 3.451185197 3.071776377## 146 147 148 149 150## 1.764146573 -0.220820580 6.085009797 8.081558607 0.595486263## 151 152 153 154 155## 0.661418471 1.298988306 -4.822825579 2.113981064 -5.366002281## 156 157 158 159 160## -4.503759227 -0.521258907 8.040173031 -4.730172680 -2.267527694## 161 162 163 164 165## -5.708276658 13.225329854 9.442341555 8.152718323 -2.088673791## 166 167 168 169 170## -0.378892378 12.796525454 0.712512527 -2.602739553 -4.353821137## 171 172 173 174 175## -5.155146605 -5.190828118 0.123427810 -5.471943077 -3.921943396## 176 177 178 179 180## -1.322090555 -2.416693069 -4.537409794 -1.535719677 4.277684320## 181 182 183 184 185## 5.075595356 8.434478893 4.012126787 1.507619640 3.681799919## 186 187 188 189 190## 4.833521901 14.115027738 -1.424767220 -2.611991466 0.384900507## 191 192 193 194 195## 6.238905148 0.210658593 3.480812857 -1.012607714 -2.458710043## 196 197 198 199 200## 9.154442786 -2.827700792 -2.369208100 -0.104691164 4.806548382## 201 202 203 204 205## 2.256060935 -5.187195014 5.228516080 6.468068762 6.810501563## 206 207 208 209 210## -0.090347964 0.717152885 4.645527863 0.905710080 2.994122821## 211 212 213 214 215## -0.692510964 2.239572462 -0.338929215 2.880574461 12.580832628## 216 217 218 219 220## 0.489508521 -3.303347748 0.344812873 -3.415254636 -6.686527678## 221 222 223 224 225## -6.484197456 -2.074566560 -4.640519579 0.354180104 6.428975473## 226 227 228 229 230## 10.185381330 0.013942450 -0.799532503 11.243347580 0.265884883## 231 232 233 234 235## -0.184492269 -1.588372916 3.651895196 11.136713685 -2.713835225## 236 237 238 239 240## -1.267055713 -5.000107449 -1.219871558 -4.727170566 -5.129406781## 241 242 243 244 245## -5.293759384 -3.642624783 -1.920078909 -3.702084138 1.271424399## 246 247 248 249 250## 5.101087387 4.283612248 0.638155722 3.211686902 2.120108504## 251 252 253 254 255## 0.193664530 -0.242158213 4.680359930 12.854366255 -2.072283165## 256 257 258 259 260## -0.795808866 6.488907610 6.697609573 -0.483614206 -4.889885936## 261 262 263 264 265## -1.012115081 5.933686692 7.810714991 -3.446340893 0.666024531## 266 267 268 269 270## -5.445742999 -0.526735926 9.160442504 4.182076069 -5.008179052## 271 272 273 274 275## -1.202955330 -2.003409719 -4.111694720 -0.276765982 -3.706391643## 276 277 278 279 280## -1.796682735 -2.410858579 -1.739933819 -1.251926558 -0.209807009## 281 282 283 284 285## 6.602430340 1.068768140 5.660369252 5.326916613 0.603109145## 286 287 288 289 290## -5.356592298 -0.001741541 -3.842066736 -4.913645804 -2.113958394## 291 292 293 294 295## -4.935633106 2.896503670 -3.933398201 -1.917832369 -2.729823483## 296 297 298 299 300## 0.142356632 -0.262669985 0.760712419 -6.613098441 -2.910546108## 301 302 303 304 305## -5.971594485 -6.942758713 -2.481910224 0.301127681 2.890945441## 306 307 308 309 310## -2.368317923 -2.162268570 -4.509051237 -5.842442366 -3.289658271## 311 312 313 314 315## -2.442668968 -4.778898429 -3.881339788 -3.945802461 -1.681200571## 316 317 318 319 320## -4.339099011 0.184274272 1.424183136 -1.190702769 -0.325290392## 321 322 323 324 325## -1.086822441 -1.769372815 -2.469524474 -0.951237906 -0.117834011## 326 327 328 329 330## -0.067869132 -0.680761771 2.859103837 -1.874181054 -1.652490735## 331 332 333 334 335## -1.792608939 -2.884466054 -3.938879998 0.059393075 -0.855099290## 336 337 338 339 340## 0.481270932 -0.660971763 -0.784903873 -1.566723204 -2.249657744## 341 342 343 344 345## -2.729393054 2.372112040 -5.547349751 -3.806479125 2.652058832## 346 347 348 349 350## 0.954988786 2.416403590 -2.173800817 -3.042051174 4.451624376## 351 352 353 354 355## 2.440559053 3.553945773 1.719361734 4.697464942 3.875133681## 356 357 358 359 360## 4.005115383 -1.837046913 -1.018066075 0.497811132 3.394519426## 361 362 363 364 365## 2.333838949 0.968073818 2.571531957 -3.431508114 -15.594473900## 366 367 368 369 370## 13.218092659 6.357137517 12.268376759 26.199270978 17.355926399## 371 372 373 374 375## 15.393159576 25.056686675 24.000190899 7.673675004 13.022201940## 376 377 378 379 380## -10.307130642 -3.840610645 -6.932744140 -2.733313008 -6.635125872## 381 382 383 384 385## -3.969948255 -7.576828331 -2.127682805 -0.761775118 5.520818839## 386 387 388 389 390## -0.860221708 4.371578036 1.781351900 3.748014302 -2.707647352## 391 392 393 394 395## -2.112251831 5.901127348 -0.191166432 -6.421241935 -5.241811753## 396 397 398 399 400## -7.204457827 -6.795590755 -7.836327793 -1.551623191 -4.590167781## 401 402 403 404 405## -6.281458707 -10.611745068 -6.161265872 -4.679487807 1.121836393## 406 407 408 409 410## -3.211158614 3.833738070 7.917052140 3.492436308 7.647315455## 411 412 413 414 415## -0.223082977 0.239280190 16.181481932 4.494216128 11.281310709## 416 417 418 419 420## -2.383767369 -5.866608110 3.504376374 2.652201480 -6.206617942## 421 422 423 424 425## -2.900026702 -3.924274759 2.278228678 0.224713895 -2.926176242## 426 427 428 429 430## -1.623749760 -6.145906467 -3.175194255 -3.257562426 -3.542347875## 431 432 433 434 435## -3.659556934 -4.595543541 -5.427283002 -2.731418609 -4.260904353## 436 437 438 439 440## 0.038583893 -4.920793839 -0.119760054 3.532488977 -0.265913130## 441 442 443 444 445## -2.206096993 -0.195580592 -0.340485002 -2.659010295 -0.714746834## 446 447 448 449 450## -0.174003587 -2.783446185 -5.526952390 -3.418346504 -4.227425073## 451 452 453 454 455## -3.122716313 -4.212910951 -2.482152362 -4.689447908 -0.380001330## 456 457 458 459 460## -1.720893352 0.012744187 0.623662098 -2.286685309 1.487523907## 461 462 463 464 465## -2.648605332 -2.472089273 -0.274073197 -2.229407679 1.080881457## 466 467 468 469 470## 2.013837468 4.625214772 2.152231493 2.115942378 1.511616032## 471 472 473 474 475## -0.267194411 -3.377180317 0.744192736 4.221753735 -2.591476316## 476 477 478 479 480## -2.811462804 -3.834815996 0.457272618 -4.604963045 -0.462763906## 481 482 483 484 485## -0.468788663 -3.398873155 -3.569943017 0.716012169 1.144838044## 486 487 488 489 490## -1.022259141 -0.555919608 -0.725361044 3.344162834 -1.223866868## 491 492 493 494 495## 4.436003280 -0.159085382 4.168814549 1.173379456 3.887505860## 496 497 498 499 500## 6.214580361 5.686792129 -0.808541439 -0.098051744 -0.954988409## 501 502 503 504 505## -3.668708470 -1.133340547 -1.775718920 -3.727426095 -4.127966807## 506## -10.444212293

20 / 49

Page 21: Multiple Linear Regression - Statistical Sciencercs46/lectures_2017/03-lr/03-mlr.pdf · HistogramsofBostondata crim Boston[, i] Density 0 20 40 60 80 0.00 0.06 zn Boston[, i] Density

MRL for Advertising data

summary(lm.fit)$coefficients

## Estimate Std. Error t value Pr(>|t|)## (Intercept) 3.645949e+01 5.103458811 7.14407419 3.283438e-12## crim -1.080114e-01 0.032864994 -3.28651687 1.086810e-03## zn 4.642046e-02 0.013727462 3.38157628 7.781097e-04## indus 2.055863e-02 0.061495689 0.33431004 7.382881e-01## chas 2.686734e+00 0.861579756 3.11838086 1.925030e-03## nox -1.776661e+01 3.819743707 -4.65125741 4.245644e-06## rm 3.809865e+00 0.417925254 9.11614020 1.979441e-18## age 6.922246e-04 0.013209782 0.05240243 9.582293e-01## dis -1.475567e+00 0.199454735 -7.39800360 6.013491e-13## rad 3.060495e-01 0.066346440 4.61289977 5.070529e-06## tax -1.233459e-02 0.003760536 -3.28000914 1.111637e-03## ptratio -9.527472e-01 0.130826756 -7.28251056 1.308835e-12## black 9.311683e-03 0.002685965 3.46679256 5.728592e-04## lstat -5.247584e-01 0.050715278 -10.34714580 7.776912e-23

21 / 49

Page 22: Multiple Linear Regression - Statistical Sciencercs46/lectures_2017/03-lr/03-mlr.pdf · HistogramsofBostondata crim Boston[, i] Density 0 20 40 60 80 0.00 0.06 zn Boston[, i] Density

MRL for Advertising dataNote that age has by far a very high p-value.How do we re-run the regression, omitting age?lm.fit1 <- lm(medv~. -age, data=Boston)summary(lm.fit1)

#### Call:## lm(formula = medv ~ . - age, data = Boston)#### Residuals:## Min 1Q Median 3Q Max## -15.6054 -2.7313 -0.5188 1.7601 26.2243#### Coefficients:## Estimate Std. Error t value Pr(>|t|)## (Intercept) 36.436927 5.080119 7.172 2.72e-12 ***## crim -0.108006 0.032832 -3.290 0.001075 **## zn 0.046334 0.013613 3.404 0.000719 ***## indus 0.020562 0.061433 0.335 0.737989## chas 2.689026 0.859598 3.128 0.001863 **## nox -17.713540 3.679308 -4.814 1.97e-06 ***## rm 3.814394 0.408480 9.338 < 2e-16 ***## dis -1.478612 0.190611 -7.757 5.03e-14 ***## rad 0.305786 0.066089 4.627 4.75e-06 ***## tax -0.012329 0.003755 -3.283 0.001099 **## ptratio -0.952211 0.130294 -7.308 1.10e-12 ***## black 0.009321 0.002678 3.481 0.000544 ***## lstat -0.523852 0.047625 -10.999 < 2e-16 ***## ---## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1#### Residual standard error: 4.74 on 493 degrees of freedom## Multiple R-squared: 0.7406, Adjusted R-squared: 0.7343## F-statistic: 117.3 on 12 and 493 DF, p-value: < 2.2e-16

22 / 49

Page 23: Multiple Linear Regression - Statistical Sciencercs46/lectures_2017/03-lr/03-mlr.pdf · HistogramsofBostondata crim Boston[, i] Density 0 20 40 60 80 0.00 0.06 zn Boston[, i] Density

Qualitative Predictors

So far, we have only considered quantitative predictors.

What if the predictors are qualitative?

As an example, the Credit data set records balance (average creditcard debt for individuals) for

I quantiative predictors: age, cards, education, ratingI qualitative predictors: gender, student (status), status

(marital), and ethnicity.

23 / 49

Page 24: Multiple Linear Regression - Statistical Sciencercs46/lectures_2017/03-lr/03-mlr.pdf · HistogramsofBostondata crim Boston[, i] Density 0 20 40 60 80 0.00 0.06 zn Boston[, i] Density

Qualitative Predictors with Two Levels

If the qualitative predictor (factor) has to levels, we can create anindicator or dummy variable.

Suppose we are dealing with gender (male, female)

Then we create a new variable, where

xi ={1, if the ith person is female0, otherwise

24 / 49

Page 25: Multiple Linear Regression - Statistical Sciencercs46/lectures_2017/03-lr/03-mlr.pdf · HistogramsofBostondata crim Boston[, i] Density 0 20 40 60 80 0.00 0.06 zn Boston[, i] Density

Qualitative Predictors with Two LevelsUsing our indicator variable

xi ={1, if the ith person is female0, otherwise,

we can then use this in our regression model:

yi = β0 + β1xi + εi (4)

={β0 + β1 + εi , if the ith person is femaleβ0 + εi , otherwise

(5)

I Now β0 can be interpreted as the overall average credit cardbalance (ignoring the gender effect), and

I β1 is the amount that females are above the average and malesare below the average.

25 / 49

Page 26: Multiple Linear Regression - Statistical Sciencercs46/lectures_2017/03-lr/03-mlr.pdf · HistogramsofBostondata crim Boston[, i] Density 0 20 40 60 80 0.00 0.06 zn Boston[, i] Density

Qualitative Predictors with More than Two Levels

When a qualitative predictor has more than two levels, a singledummy variable cannot represent all possible values. In thissituation, we can create additional dummy variables.

For example, for the ethnicity variable we create two dummyvariables.

xi1 ={1, if the ith person is Asian0, if the ith person is not Asian

and

xi2 ={1, if the ith person is Caucasian0, if the ith person is not Caucasian

26 / 49

Page 27: Multiple Linear Regression - Statistical Sciencercs46/lectures_2017/03-lr/03-mlr.pdf · HistogramsofBostondata crim Boston[, i] Density 0 20 40 60 80 0.00 0.06 zn Boston[, i] Density

Qualitative Predictors with More than Two Levels

Then both of these variables can be used in the regression equation,in order to obtain the model

yi = β0 + β1xi1 + β2xi2 + εi (6)

=

β0 + β1 + εi , if the ith person is Asianβ0 + β2 + εi , if the ith person is Caucasianβ0 + εi , if the ith person is African American

(7)

27 / 49

Page 28: Multiple Linear Regression - Statistical Sciencercs46/lectures_2017/03-lr/03-mlr.pdf · HistogramsofBostondata crim Boston[, i] Density 0 20 40 60 80 0.00 0.06 zn Boston[, i] Density

Qualitative Predictors with More than Two Levels

I Now β0 can be interpreted as the average credit card balancefor African Americans,

I β1 can be interpreted as the difference in the average balancebetween the Asian and African American categories, and

I β2 can be interpreted as the difference in the average balancebetween the Caucasian and African American categories.

I There will always be one fewer dummy variable than thenumber of levels.

I The level with no dummy variable — African American in thisexample — is known as the baseline.

28 / 49

Page 29: Multiple Linear Regression - Statistical Sciencercs46/lectures_2017/03-lr/03-mlr.pdf · HistogramsofBostondata crim Boston[, i] Density 0 20 40 60 80 0.00 0.06 zn Boston[, i] Density

Extensions of the Linear Model

We discuss removing two of the most important assumptions of thelinear model: additivity and linearity.

29 / 49

Page 30: Multiple Linear Regression - Statistical Sciencercs46/lectures_2017/03-lr/03-mlr.pdf · HistogramsofBostondata crim Boston[, i] Density 0 20 40 60 80 0.00 0.06 zn Boston[, i] Density

Additivity

The additive assumption means that the effect of changes in apredictor Xj on the response Y is independent of the values of theother predictors.

30 / 49

Page 31: Multiple Linear Regression - Statistical Sciencercs46/lectures_2017/03-lr/03-mlr.pdf · HistogramsofBostondata crim Boston[, i] Density 0 20 40 60 80 0.00 0.06 zn Boston[, i] Density

Removing the Additive Effect

Suppose that spending money on radio advertising actually increasesthe effectiveness of TV ad- vertising, so that the slope term for TVshould increase as radio increases.

Given a fixed budget of $100,000, spending half on radio and halfon TV may increase sales more than allocating the entire amount toeither TV or to radio. This is referred to as an interaction effect.

31 / 49

Page 32: Multiple Linear Regression - Statistical Sciencercs46/lectures_2017/03-lr/03-mlr.pdf · HistogramsofBostondata crim Boston[, i] Density 0 20 40 60 80 0.00 0.06 zn Boston[, i] Density

Interaction EffectOne way of extending this model to allow for interaction effects is toinclude a third predictor, called an interaction term, which isconstructed by computing the product of X1 and X2, which results in

Y = β0 + β1X1 + β2X2 + β3X1X2 + ε. (8)

How does inclusion of this interaction term relax the additiveassumption?Equation 8 can be rewritten as

Y = β0 + (β1 + β3X2)X1 + β2X2 + ε (9)= β0 + β1X1 + β2X2 + ε (10)

(11)

where β1 = β1 + β3X2.32 / 49

Page 33: Multiple Linear Regression - Statistical Sciencercs46/lectures_2017/03-lr/03-mlr.pdf · HistogramsofBostondata crim Boston[, i] Density 0 20 40 60 80 0.00 0.06 zn Boston[, i] Density

Interaction Effect

Since β1 changes with X2, the effect of X1 on Y is no longerconstant: adjusting X2 will change the impact of X1 on Y .

33 / 49

Page 34: Multiple Linear Regression - Statistical Sciencercs46/lectures_2017/03-lr/03-mlr.pdf · HistogramsofBostondata crim Boston[, i] Density 0 20 40 60 80 0.00 0.06 zn Boston[, i] Density

Linear Assumption

The linear assumption means that the change in the response Y dueto a one-unit change in Xj is constant, regardless of the value of Xj .

In our previous analysis of the Advertising data, we concluded thatboth TV and radio seem to be associated with sales.

For example, the linear model states that the average effect on salesof a one-unit increase in TV is always β1, regardless of the amountspent on radio.

However, the simple model may be incorrect.

34 / 49

Page 35: Multiple Linear Regression - Statistical Sciencercs46/lectures_2017/03-lr/03-mlr.pdf · HistogramsofBostondata crim Boston[, i] Density 0 20 40 60 80 0.00 0.06 zn Boston[, i] Density

Polynomial regression

We present a very simple way to directly extend the linear model toaccommodate non-linear relationships, using polynomialregression

35 / 49

Page 36: Multiple Linear Regression - Statistical Sciencercs46/lectures_2017/03-lr/03-mlr.pdf · HistogramsofBostondata crim Boston[, i] Density 0 20 40 60 80 0.00 0.06 zn Boston[, i] Density

Polynomial regressionLet us consider a motivating example from the Auto data set.attach(Auto)plot(horsepower, mpg)

50 100 150 200

1020

3040

horsepower

mpg

36 / 49

Page 37: Multiple Linear Regression - Statistical Sciencercs46/lectures_2017/03-lr/03-mlr.pdf · HistogramsofBostondata crim Boston[, i] Density 0 20 40 60 80 0.00 0.06 zn Boston[, i] Density

Polynomial regressionATTN: I can’t get the polynomail onto the regression line!## Warning in abline(lm.fit.poly2, lwd = 3, col = "brown"): only using the## first two of 3 regression coefficients

50 100 150 200

1020

3040

horsepower

mpg

37 / 49

Page 38: Multiple Linear Regression - Statistical Sciencercs46/lectures_2017/03-lr/03-mlr.pdf · HistogramsofBostondata crim Boston[, i] Density 0 20 40 60 80 0.00 0.06 zn Boston[, i] Density

Linear regression

5 10 15 20 25 30

−15

−10

−5

05

1015

20

Fitted values

Res

idua

ls

lm(mpg ~ horsepower)

Residuals vs Fitted

323

330

334

−3 −2 −1 0 1 2 3

−3

−2

−1

01

23

4

Theoretical Quantiles

Sta

ndar

dize

d re

sidu

als

lm(mpg ~ horsepower)

Normal Q−Q

323

330

334

5 10 15 20 25 30

0.0

0.5

1.0

1.5

Fitted values

Sta

ndar

dize

d re

sidu

als

lm(mpg ~ horsepower)

Scale−Location

323

330

334

0.000 0.005 0.010 0.015 0.020 0.025 0.030

−2

02

4

Leverage

Sta

ndar

dize

d re

sidu

als

lm(mpg ~ horsepower)

Cook's distance

Residuals vs Leverage

117

914

38 / 49

Page 39: Multiple Linear Regression - Statistical Sciencercs46/lectures_2017/03-lr/03-mlr.pdf · HistogramsofBostondata crim Boston[, i] Density 0 20 40 60 80 0.00 0.06 zn Boston[, i] Density

Polynomial regression

15 20 25 30 35

−15

−10

−5

05

1015

Fitted values

Res

idua

ls

lm(mpg ~ poly(horsepower, 2))

Residuals vs Fitted

331321

153

−3 −2 −1 0 1 2 3

−2

02

4

Theoretical Quantiles

Sta

ndar

dize

d re

sidu

als

lm(mpg ~ poly(horsepower, 2))

Normal Q−Q

331321

153

15 20 25 30 35

0.0

0.5

1.0

1.5

Fitted values

Sta

ndar

dize

d re

sidu

als

lm(mpg ~ poly(horsepower, 2))

Scale−Location

331321153

0.00 0.02 0.04 0.06 0.08

−4

−2

02

4

Leverage

Sta

ndar

dize

d re

sidu

als

lm(mpg ~ poly(horsepower, 2))

Cook's distance0.5

0.5

Residuals vs Leverage

2010259

39 / 49

Page 40: Multiple Linear Regression - Statistical Sciencercs46/lectures_2017/03-lr/03-mlr.pdf · HistogramsofBostondata crim Boston[, i] Density 0 20 40 60 80 0.00 0.06 zn Boston[, i] Density

Potential Problems

There are many issues that can arise in linear regression since it is asimple method.

40 / 49

Page 41: Multiple Linear Regression - Statistical Sciencercs46/lectures_2017/03-lr/03-mlr.pdf · HistogramsofBostondata crim Boston[, i] Density 0 20 40 60 80 0.00 0.06 zn Boston[, i] Density

Non-linearity of the response-predictor relationships

This can be checked using residual plots to identify for non-linearity.

That is plot the residuals ei versus xi .

In the MLR setting, plot the residual versus the predicted (fitted)values yi .

An ideal plot will have no pattern!

41 / 49

Page 42: Multiple Linear Regression - Statistical Sciencercs46/lectures_2017/03-lr/03-mlr.pdf · HistogramsofBostondata crim Boston[, i] Density 0 20 40 60 80 0.00 0.06 zn Boston[, i] Density

Correlation of Error Terms

An important assumption of the linear model is that the error termsare uncorrelated.

If there is correlation in the error terms, then the estimationstandard errors will tend to understimate the true standard errors.

As a result, confidence and prediction intervals will be more narrowthan they should be.

Why might we have correlated errors? We could have time seriesdata, where such data points that appear are correlated by the timestructure.

42 / 49

Page 43: Multiple Linear Regression - Statistical Sciencercs46/lectures_2017/03-lr/03-mlr.pdf · HistogramsofBostondata crim Boston[, i] Density 0 20 40 60 80 0.00 0.06 zn Boston[, i] Density

Non-constant Variance of Error Terms

Another important assumption is that the linear regresion model isthat the error terms have constant variance (σ2).

Often, the variance of the error terms are non-constant.

One can identify non-constant errors (or heterscedarcity) from thepresence of a funnel shape in the residual plot.

One easy solution is transforming the response using a concavefunction such as log or sqrt.

Then recheck the residual plot.

43 / 49

Page 44: Multiple Linear Regression - Statistical Sciencercs46/lectures_2017/03-lr/03-mlr.pdf · HistogramsofBostondata crim Boston[, i] Density 0 20 40 60 80 0.00 0.06 zn Boston[, i] Density

Outliers

An outlier is a point for which yi is far from the value predicted bythe model.

Outliers can arise for a variety of reasons, such as incorrectrecording of an observation during data collection.

Residual (studentized) plots can be used to identify outliers.

44 / 49

Page 45: Multiple Linear Regression - Statistical Sciencercs46/lectures_2017/03-lr/03-mlr.pdf · HistogramsofBostondata crim Boston[, i] Density 0 20 40 60 80 0.00 0.06 zn Boston[, i] Density

High Leverage PointsWe just saw that outliers are observations for which the response yiis unusual given the predictor xi .

In contrast, observations with high leverage high leverage have anunusual value for xi .

High leverage observations tend to have a sizable impact on theestimated regression line.

For this reason, it is important to identify high leverage observations.

In order to quantify an observation’s leverage, we compute theleverage statistic. A large value of this statistic indicates anobservation with high leverage leverage.

For SLR,hi = 1

n + (xi − x)∑nj=1(xj − x .

45 / 49

Page 46: Multiple Linear Regression - Statistical Sciencercs46/lectures_2017/03-lr/03-mlr.pdf · HistogramsofBostondata crim Boston[, i] Density 0 20 40 60 80 0.00 0.06 zn Boston[, i] Density

Collinearity

Collinearity refers to the situation in which two or more predictorvariables are closely related to one another.

46 / 49

Page 47: Multiple Linear Regression - Statistical Sciencercs46/lectures_2017/03-lr/03-mlr.pdf · HistogramsofBostondata crim Boston[, i] Density 0 20 40 60 80 0.00 0.06 zn Boston[, i] Density

Collinearity

Let’s consider the Credit data set.

Credit <- read.csv("data/credit.csv",header=TRUE, sep=",")pdf(file = "examples/collinear.pdf")par(mfrow=c(1, 2))plot(Credit$Limit, Credit$Age, xlab="Limit", ylab="Age")plot(Credit$Limit, Credit$Rating, xlab="Limit",

ylab="Rating")dev.off()

## pdf## 2

47 / 49

Page 48: Multiple Linear Regression - Statistical Sciencercs46/lectures_2017/03-lr/03-mlr.pdf · HistogramsofBostondata crim Boston[, i] Density 0 20 40 60 80 0.00 0.06 zn Boston[, i] Density

Collinearity

● ●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●● ●

●●

● ●

●●

●●

●●●

●●

2000 6000 10000 14000

2040

6080

100

Limit

Age

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

2000 6000 10000 14000

200

400

600

800

1000

Limit

Rat

ing

Figure 1: Scatterplots of the observations from the Credit data set. Left:Plot of age versus limit. Right: A plot of age versus rating, with a highcorrelated relationship.

48 / 49

Page 49: Multiple Linear Regression - Statistical Sciencercs46/lectures_2017/03-lr/03-mlr.pdf · HistogramsofBostondata crim Boston[, i] Density 0 20 40 60 80 0.00 0.06 zn Boston[, i] Density

The Marketing Plan

Exercise: Now that we have walked through this, see Section 3.4and answer these questions on your own.

49 / 49