-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathAssumptions-of-Linear-Regression.html
467 lines (430 loc) · 31 KB
/
Assumptions-of-Linear-Regression.html
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
<!DOCTYPE html>
<html>
<head>
<title>10 Assumptions of Linear Regression - Full List with Examples and Code</title>
<meta charset="utf-8">
<meta name="Description" content="R Language Tutorials for Advanced Statistics">
<meta name="Keywords" content="R, Tutorial, Machine learning, Statistics, Data Mining, Analytics, Data science, Linear Regression, Logistic Regression, Time series, Forecasting">
<meta name="Distribution" content="Global">
<meta name="Author" content="Selva Prabhakaran">
<meta name="Robots" content="index, follow">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="shortcut icon" href="/screenshots/iconb-64.png" type="image/x-icon" />
<link href="www/bootstrap.min.css" rel="stylesheet">
<link href="www/highlight.css" rel="stylesheet">
<link href='http://fonts.googleapis.com/css?family=Inconsolata:400,700'
rel='stylesheet' type='text/css'>
<!-- Color Script -->
<style type="text/css">
a {
color: #3675C5;
color: rgb(25, 145, 248);
color: #4582ec;
color: #3F73D8;
}
li {
line-height: 1.65;
}
/* reduce spacing around math formula*/
.MathJax_Display {
margin: 0em 0em;
}
</style>
<!-- Add Google search -->
<script language="Javascript" type="text/javascript">
function my_search_google()
{
var query = document.getElementById("my-google-search").value;
window.open("http://google.com/search?q=" + query
+ "%20site:" + "http://r-statistics.co");
}
</script>
</head>
<body>
<div class="container">
<div class="masthead">
<!--
<ul class="nav nav-pills pull-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
Table of contents<b class="caret"></b>
</a>
<ul class="dropdown-menu pull-right" role="menu">
<li class="dropdown-header"></li>
<li class="dropdown-header">Tutorial</li>
<li><a href="R-Tutorial.html">R Tutorial</a></li>
<li class="dropdown-header">ggplot2</li>
<li><a href="ggplot2-Tutorial-With-R.html">ggplot2 Short Tutorial</a></li>
<li><a href="Complete-Ggplot2-Tutorial-Part1-With-R-Code.html">ggplot2 Tutorial 1 - Intro</a></li>
<li><a href="Complete-Ggplot2-Tutorial-Part2-Customizing-Theme-With-R-Code.html">ggplot2 Tutorial 2 - Theme</a></li>
<li><a href="Top50-Ggplot2-Visualizations-MasterList-R-Code.html">ggplot2 Tutorial 3 - Masterlist</a></li>
<li><a href="ggplot2-cheatsheet.html">ggplot2 Quickref</a></li>
<li class="dropdown-header">Foundations</li>
<li><a href="Linear-Regression.html">Linear Regression</a></li>
<li><a href="Statistical-Tests-in-R.html">Statistical Tests</a></li>
<li><a href="Missing-Value-Treatment-With-R.html">Missing Value Treatment</a></li>
<li><a href="Outlier-Treatment-With-R.html">Outlier Analysis</a></li>
<li><a href="Variable-Selection-and-Importance-With-R.html">Feature Selection</a></li>
<li><a href="Model-Selection-in-R.html">Model Selection</a></li>
<li><a href="Logistic-Regression-With-R.html">Logistic Regression</a></li>
<li><a href="Environments.html">Advanced Linear Regression</a></li>
<li class="dropdown-header">Advanced Regression Models</li>
<li><a href="adv-regression-models.html">Advanced Regression Models</a></li>
<li class="dropdown-header">Time Series</li>
<li><a href="Time-Series-Analysis-With-R.html">Time Series Analysis</a></li>
<li><a href="Time-Series-Forecasting-With-R.html">Time Series Forecasting </a></li>
<li><a href="Time-Series-Forecasting-With-R-part2.html">More Time Series Forecasting</a></li>
<li class="dropdown-header">High Performance Computing</li>
<li><a href="Parallel-Computing-With-R.html">Parallel computing</a></li>
<li><a href="Strategies-To-Improve-And-Speedup-R-Code.html">Strategies to Speedup R code</a></li>
<li class="dropdown-header">Useful Techniques</li>
<li><a href="Association-Mining-With-R.html">Association Mining</a></li>
<li><a href="Multi-Dimensional-Scaling-With-R.html">Multi Dimensional Scaling</a></li>
<li><a href="Profiling.html">Optimization</a></li>
<li><a href="Information-Value-With-R.html">InformationValue package</a></li>
</ul>
</li>
</ul>
-->
<ul class="nav nav-pills pull-right">
<div class="input-group">
<form onsubmit="my_search_google()">
<input type="text" class="form-control" id="my-google-search" placeholder="Search..">
<form>
</div><!-- /input-group -->
</ul><!-- /.col-lg-6 -->
<h3 class="muted"><a href="/">r-statistics.co</a><small> by Selva Prabhakaran</small></h3>
<hr>
</div>
<div class="row">
<div class="col-xs-12 col-sm-3" id="nav">
<div class="well">
<li>
<ul class="list-unstyled">
<li class="dropdown-header"></li>
<li class="dropdown-header">Tutorial</li>
<li><a href="R-Tutorial.html">R Tutorial</a></li>
<li class="dropdown-header">ggplot2</li>
<li><a href="ggplot2-Tutorial-With-R.html">ggplot2 Short Tutorial</a></li>
<li><a href="Complete-Ggplot2-Tutorial-Part1-With-R-Code.html">ggplot2 Tutorial 1 - Intro</a></li>
<li><a href="Complete-Ggplot2-Tutorial-Part2-Customizing-Theme-With-R-Code.html">ggplot2 Tutorial 2 - Theme</a></li>
<li><a href="Top50-Ggplot2-Visualizations-MasterList-R-Code.html">ggplot2 Tutorial 3 - Masterlist</a></li>
<li><a href="ggplot2-cheatsheet.html">ggplot2 Quickref</a></li>
<li class="dropdown-header">Foundations</li>
<li><a href="Linear-Regression.html">Linear Regression</a></li>
<li><a href="Statistical-Tests-in-R.html">Statistical Tests</a></li>
<li><a href="Missing-Value-Treatment-With-R.html">Missing Value Treatment</a></li>
<li><a href="Outlier-Treatment-With-R.html">Outlier Analysis</a></li>
<li><a href="Variable-Selection-and-Importance-With-R.html">Feature Selection</a></li>
<li><a href="Model-Selection-in-R.html">Model Selection</a></li>
<li><a href="Logistic-Regression-With-R.html">Logistic Regression</a></li>
<li><a href="Environments.html">Advanced Linear Regression</a></li>
<li class="dropdown-header">Advanced Regression Models</li>
<li><a href="adv-regression-models.html">Advanced Regression Models</a></li>
<li class="dropdown-header">Time Series</li>
<li><a href="Time-Series-Analysis-With-R.html">Time Series Analysis</a></li>
<li><a href="Time-Series-Forecasting-With-R.html">Time Series Forecasting </a></li>
<li><a href="Time-Series-Forecasting-With-R-part2.html">More Time Series Forecasting</a></li>
<li class="dropdown-header">High Performance Computing</li>
<li><a href="Parallel-Computing-With-R.html">Parallel computing</a></li>
<li><a href="Strategies-To-Improve-And-Speedup-R-Code.html">Strategies to Speedup R code</a></li>
<li class="dropdown-header">Useful Techniques</li>
<li><a href="Association-Mining-With-R.html">Association Mining</a></li>
<li><a href="Multi-Dimensional-Scaling-With-R.html">Multi Dimensional Scaling</a></li>
<li><a href="Profiling.html">Optimization</a></li>
<li><a href="Information-Value-With-R.html">InformationValue package</a></li>
</ul>
</li>
</div>
<div class="well">
<p>Stay up-to-date. <a href="https://docs.google.com/forms/d/1xkMYkLNFU9U39Dd8S_2JC0p8B5t6_Yq6zUQjanQQJpY/viewform">Subscribe!</a></p>
<p><a href="https://docs.google.com/forms/d/13GrkCFcNa-TOIllQghsz2SIEbc-YqY9eJX02B19l5Ow/viewform">Chat!</a></p>
</div>
<h4>Contents</h4>
<ul class="list-unstyled" id="toc"></ul>
<!--
<hr>
<p><a href="/contribute.html">How to contribute</a></p>
<p><a class="btn btn-primary" href="">Edit this page</a></p>
-->
</div>
<div id="content" class="col-xs-12 col-sm-8 pull-right">
<h1>Assumptions of Linear Regression</h1>
<blockquote>
<p>Building a linear regression model is only half of the work. In order to actually be usable in practice, the model should conform to the assumptions of linear regression.</p>
</blockquote>
<h2>Assumption 1</h2>
<blockquote>
<h4><em>The regression model is linear in parameters</em></h4>
</blockquote>
<p>An example of model equation that is <em>linear in parameters</em> <br /><span class="math display"><em>Y</em> = <em>a</em> + (<em>β</em>1*<em>X</em>1) + (<em>β</em>2*<em>X</em>2<sup>2</sup>)</span><br /></p>
<p>Though, the <code>X2</code> is raised to power 2, the equation is still linear in beta parameters. So the assumption is satisfied in this case.</p>
<h2>Assumption 2</h2>
<blockquote>
<h4><em>The mean of residuals is zero</em></h4>
</blockquote>
<h4>How to check?</h4>
<p>Check the mean of the residuals. If it zero (or very close), then this assumption is held true for that model. This is default unless you explicitly make amends, such as setting the intercept term to zero.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">mod <-<span class="st"> </span><span class="kw">lm</span>(dist ~<span class="st"> </span>speed, <span class="dt">data=</span>cars)
<span class="kw">mean</span>(mod$residuals)
<span class="co">#=> 2.442491e-17</span></code></pre></div>
<p>Since the mean of residuals is approximately zero, this assumption holds true for this model.</p>
<h2>Assumption 3</h2>
<blockquote>
<h4><em>Homoscedasticity of residuals or equal variance</em></h4>
</blockquote>
<h4>How to check?</h4>
<p>Once the regression model is built, set <code>par(mfrow=c(2, 2))</code>, then, plot the model using <code>plot(lm.mod)</code>. This produces four plots. The <em>top-left</em> and <em>bottom-left</em> plots shows how the <em>residuals</em> vary as the <em>fitted</em> values increase.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">par</span>(<span class="dt">mfrow=</span><span class="kw">c</span>(<span class="dv">2</span>,<span class="dv">2</span>)) <span class="co"># set 2 rows and 2 column plot layout</span>
mod_1 <-<span class="st"> </span><span class="kw">lm</span>(mpg ~<span class="st"> </span>disp, <span class="dt">data=</span>mtcars) <span class="co"># linear model</span>
<span class="kw">plot</span>(mod_1)</code></pre></div>
<p><img src='screenshots/regression-plot-mtcars.png' width='528' height='528' /></p>
<p>From the first plot (top-left), as the fitted values along x increase, the residuals decrease and then increase. This pattern is indicated by the red line, which should be approximately flat if the disturbances are homoscedastic. The plot on the bottom left also checks this, and is more convenient as the disturbance term in Y axis is standardized.</p>
<p>In this case, there is a definite pattern noticed. So, there is heteroscedasticity. Lets check this on a different model.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">mod <-<span class="st"> </span><span class="kw">lm</span>(dist ~<span class="st"> </span>speed, <span class="dt">data=</span>cars[<span class="dv">1</span>:<span class="dv">20</span>, ]) <span class="co"># linear model</span>
<span class="kw">plot</span>(mod)</code></pre></div>
<p><img src='screenshots/regression-plot-cars-20rows.png' width='528' height='528' /></p>
<p>Now, the points appear random and the line looks pretty flat, with no increasing or decreasing trend. So, the condition of homoscedasticity can be accepted.</p>
<h2>Assumption 4</h2>
<blockquote>
<h4><em>No autocorrelation of residuals</em></h4>
<p>This is applicable especially for time series data. Autocorrelation is the correlation of a time Series with lags of itself. When the residuals are autocorrelated, it means that the current value is dependent of the previous (historic) values and that there is a definite unexplained pattern in the Y variable that shows up in the disturbances.</p>
</blockquote>
<h4>How to check for autocorrelation?</h4>
<p>Below, are 3 ways you could check for autocorrelation of residuals.</p>
<h5>Using acf plot</h5>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># Method 1: Visualise with acf plot</span>
<span class="kw">library</span>(ggplot2)
<span class="kw">data</span>(economics)
lmMod <-<span class="st"> </span><span class="kw">lm</span>(pce ~<span class="st"> </span>pop, <span class="dt">data=</span>economics)
<span class="kw">acf</span>(lmMod$residuals) <span class="co"># highly autocorrelated from the picture.</span></code></pre></div>
<p><img src='screenshots/acf-plot.png' width='528' height='305' /></p>
<p>The X axis corresponds to the lags of the residual, increasing in steps of 1. The very first line (to the left) shows the correlation of residual with itself (Lag0), therefore, it will always be equal to 1.</p>
<p>If the residuals were not autocorrelated, the correlation (Y-axis) from the immediate next line onwards will drop to a near zero value below the dashed blue line (significance level). Clearly, this is not the case here. So we can conclude that the residuals are autocorrelated.</p>
<h5>Using runs test</h5>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># Method 2: Runs test to test for randomness</span>
lawstat::<span class="kw">runs.test</span>(lmMod$residuals)
<span class="co">#=> Runs Test - Two sided</span>
<span class="co">#=> data: lmMod$residuals</span>
<span class="co">#=> Standardized Runs Statistic = -23.812, p-value < 2.2e-16</span></code></pre></div>
<p>With a p-value < 2.2e-16, we reject the null hypothesis that it is random. This means there is a definite pattern in the residuals.</p>
<h5>Using Durbin-Watson test.</h5>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># Method 3: Durbin-Watson test</span>
lmtest::<span class="kw">dwtest</span>(lmMod)
<span class="co">#=> Durbin-Watson test</span>
<span class="co">#=> data: lmMod</span>
<span class="co">#=> DW = 0.0021559, p-value < 2.2e-16</span>
<span class="co">#=> alternative hypothesis: true autocorrelation is greater than 0</span></code></pre></div>
<p>So, <code>dwtest</code> also confirms our finding.</p>
<h4>How to rectify?</h4>
<p>Add lag1 of residual as an X variable to the original model. This can be conveniently done using the <code>slide</code> function in <code>DataCombine</code> package.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(DataCombine)
econ_data <-<span class="st"> </span><span class="kw">data.frame</span>(economics, <span class="dt">resid_mod1=</span>lmMod$residuals)
econ_data_1 <-<span class="st"> </span><span class="kw">slide</span>(econ_data, <span class="dt">Var=</span><span class="st">"resid_mod1"</span>, <span class="dt">NewVar =</span> <span class="st">"lag1"</span>, <span class="dt">slideBy =</span> -<span class="dv">1</span>)
econ_data_2 <-<span class="st"> </span><span class="kw">na.omit</span>(econ_data_1)
lmMod2 <-<span class="st"> </span><span class="kw">lm</span>(pce ~<span class="st"> </span>pop +<span class="st"> </span>lag1, <span class="dt">data=</span>econ_data_2)</code></pre></div>
<p>Lets check if the problem of autocorrelation of residuals is taken care of using this method.</p>
<p><strong>Check 1</strong></p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">acf</span>(lmMod2$residuals)</code></pre></div>
<p><img src='screenshots/acf-plot2.png' width='528' height='305' /></p>
<p>Unlike the <code>acf</code> plot of <code>lmMod</code>, the correlation values drop below the dashed blue line from lag1 itself. So autocorrelation can’t be confirmed.</p>
<p><strong>Check 2</strong></p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">runs.test</span>(lmMod2$residuals) <span class="co"># runs test</span>
<span class="co">#=> Runs Test - Two sided</span>
<span class="co">#=> </span>
<span class="co">#=> data: lmMod2$residuals</span>
<span class="co">#=> Standardized Runs Statistic = 0.96176, p-value = 0.3362</span></code></pre></div>
<p>p-value = 0.3362. Can’t reject null hypothesis that it is random. With a p-value = 0.3362, we cannot reject the null hypothesis. Therefore we can safely assume that residuals are not autocorrelated.</p>
<p><strong>Check 3</strong></p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">lmtest::<span class="kw">dwtest</span>(lmMod2)
<span class="co">#=> Durbin-Watson test</span>
<span class="co">#=> </span>
<span class="co">#=> data: lmMod2</span>
<span class="co">#=> DW = 2.0431, p-value = 0.6672</span>
<span class="co">#=> alternative hypothesis: true autocorrelation is greater than 0</span></code></pre></div>
<p>With a high p value of 0.667, we cannot reject the null hypothesis that true autocorrelation is zero. So the assumption that residuals should not be autocorrelated is satisfied by this model.</p>
<p>If, even after adding lag1 as an X variable, does not satisfy the assumption of autocorrelation of residuals, you might want to try adding lag2, or be creative in making meaningful derived explanatory variables or interaction terms. This is more like art than an algorithm.</p>
<h2>Assumption 5</h2>
<blockquote>
<h4><em>The X variables and residuals are uncorrelated</em></h4>
</blockquote>
<h4>How to check?</h4>
<p>Do a correlation test on the X variable and the residuals.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">mod.lm <-<span class="st"> </span><span class="kw">lm</span>(dist ~<span class="st"> </span>speed, <span class="dt">data=</span>cars)
<span class="kw">cor.test</span>(cars$speed, mod.lm$residuals) <span class="co"># do correlation test </span>
<span class="co">#=> Pearson's product-moment correlation</span>
<span class="co">#=> </span>
<span class="co">#=> data: cars$speed and mod.lm$residuals</span>
<span class="co">#=> t = -8.1225e-17, df = 48, p-value = 1</span>
<span class="co">#=> alternative hypothesis: true correlation is not equal to 0</span>
<span class="co">#=> 95 percent confidence interval:</span>
<span class="co">#=> -0.2783477 0.2783477</span>
<span class="co">#=> sample estimates:</span>
<span class="co">#=> cor </span>
<span class="co">#=> -1.172376e-17</span></code></pre></div>
<p>p-value is high, so null hypothesis that true correlation is 0 can’t be rejected. So, the assumption holds true for this model.</p>
<h2>Assumption 6</h2>
<h4><em>The number of observations must be greater than number of Xs</em></h4>
<p>This can be directly observed by looking at the data.</p>
<h2>Assumption 7</h2>
<blockquote>
<h4><em>The variability in X values is positive</em></h4>
</blockquote>
<p>This means the X values in a given sample must not all be the same (or even nearly the same).</p>
<h4>How to check?</h4>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">var</span>(cars$speed)
<span class="co">#=> [1] 27.95918</span></code></pre></div>
<p>The variance in the X variable above is much larger than 0. So, this assumption is satisfied.</p>
<h2>Assumption 8</h2>
<blockquote>
<h4><em>The regression model is correctly specified</em></h4>
</blockquote>
<p>This means that if the Y and X variable has an inverse relationship, the model equation should be specified appropriately:</p>
<p><br /><span class="math display">$$Y = \beta1 + \beta2 * \left( 1 \over X \right)$$</span><br /></p>
<h2>Assumption 9</h2>
<blockquote>
<h4><em>No perfect multicollinearity</em></h4>
</blockquote>
<p>There is no perfect linear relationship between explanatory variables.</p>
<h4>How to check?</h4>
<p>Using Variance Inflation factor (VIF). But, What is VIF?</p>
<p>VIF is a metric computed for every <span class="math inline"><em>X</em></span> variable that goes into a linear model. If the VIF of a variable is high, it means the information in that variable is already explained by other <span class="math inline"><em>X</em></span> variables present in the given model, which means, more redundant is that variable. So, lower the VIF (<2) the better. VIF for a <span class="math inline"><em>X</em></span> var is calculated as:</p>
<p><br /><span class="math display">$$VIF = {1 \over \left( 1-R_{sq} \right)}$$</span><br /></p>
<p>where, <span class="math inline"><em>R</em><sub><em>s</em><em>q</em></sub></span> is the <span class="math inline"><em>R</em><sub><em>s</em><em>q</em></sub></span> term for the model with given <span class="math inline"><em>X</em></span> as response against all other <span class="math inline"><em>X</em><em>s</em></span> that went into the model as predictors.</p>
<p>Practically, if two of the <span class="math inline"><em>X</em>′<em>s</em></span> have high correlation, they will likely have high VIFs. Generally, VIF for an <span class="math inline"><em>X</em></span> variable should be less than 4 in order to be accepted as not causing multi-collinearity. The cutoff is kept as low as 2, if you want to be strict about your <span class="math inline"><em>X</em></span> variables.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(car)
mod2 <-<span class="st"> </span><span class="kw">lm</span>(mpg ~<span class="st"> </span>., <span class="dt">data=</span>mtcars)
<span class="kw">vif</span>(mod2)
<span class="co"># cyl disp hp drat wt qsec vs am gear carb </span>
<span class="co"># 15.373833 21.620241 9.832037 3.374620 15.164887 7.527958 4.965873 4.648487 5.357452 7.908747 </span></code></pre></div>
<h4>How to rectify?</h4>
<p>Two ways:</p>
<ol style="list-style-type: decimal">
<li>Either iteratively remove the <span class="math inline"><em>X</em></span> var with the highest VIF or,</li>
<li>See correlation between all variables and keep only one of all highly correlated pairs.</li>
</ol>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(corrplot)
<span class="kw">corrplot</span>(<span class="kw">cor</span>(mtcars[, -<span class="dv">1</span>]))
<span class="co">#=> Interpreted from below plot.</span>
<span class="co">#=> Correlated pairs: </span>
<span class="co">#=> - disp, cyl, hp, wt</span>
<span class="co">#=> - gear, am</span>
<span class="co">#=> - hp, carb</span></code></pre></div>
<p><img src='screenshots/corrplot.png' width='528' height='528' /></p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">mod <-<span class="st"> </span><span class="kw">lm</span>(mpg ~<span class="st"> </span>cyl +<span class="st"> </span>gear, <span class="dt">data=</span>mtcars)
<span class="kw">vif</span>(mod)
<span class="co">#=> cyl gear </span>
<span class="co">#=> 1.320551 1.320551</span></code></pre></div>
<p>The convention is, the VIF should not go more than 4 for any of the X variables. That means we are not letting the <span class="math inline"><em>R</em><sub><em>S</em><em>q</em></sub></span> of any of the <span class="math inline"><em>X</em><em>s</em></span> (the model that was built with that X as a response variable and the remaining <span class="math inline"><em>X</em><em>s</em></span> are predictors) to go more than 75%. => 1/(1-0.75) => 1/0.25 => 4.</p>
<h2>Assumption 10</h2>
<blockquote>
<h4><em>Normality of residuals</em></h4>
</blockquote>
<p>The residuals should be normally distributed. If the maximum likelihood method (not OLS) is used to compute the estimates, this also implies the <span class="math inline"><em>Y</em></span> and the <span class="math inline"><em>X</em><em>s</em></span> are also normally distributed.</p>
<p>This can be visually checked using the <code>qqnorm()</code> plot (top right plot).</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">par</span>(<span class="dt">mfrow=</span><span class="kw">c</span>(<span class="dv">2</span>,<span class="dv">2</span>))
mod <-<span class="st"> </span><span class="kw">lm</span>(dist ~<span class="st"> </span>speed, <span class="dt">data=</span>cars)
<span class="kw">plot</span>(mod)</code></pre></div>
<p><img src='screenshots/regression-plot.png' width='528' height='528' /></p>
<p>The <code>qqnorm()</code> plot in top-right evaluates this assumption. If points lie exactly on the line, it is perfectly normal distribution. However, some deviation is to be expected, particularly near the ends (note the upper right), but the deviations should be small, even lesser that they are here.</p>
<h2>Check Assumptions Automatically</h2>
<p>The <code>gvlma()</code> function from <code>gvlma</code> offers a way to check the important assumptions on a given linear model.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">par</span>(<span class="dt">mfrow=</span><span class="kw">c</span>(<span class="dv">2</span>,<span class="dv">2</span>)) <span class="co"># draw 4 plots in same window</span>
mod <-<span class="st"> </span><span class="kw">lm</span>(dist ~<span class="st"> </span>speed, <span class="dt">data=</span>cars)
gvlma::<span class="kw">gvlma</span>(mod)
<span class="co">#=> Value p-value Decision</span>
<span class="co">#=> Global Stat 15.801 0.003298 Assumptions NOT satisfied!</span>
<span class="co">#=> Skewness 6.528 0.010621 Assumptions NOT satisfied!</span>
<span class="co">#=> Kurtosis 1.661 0.197449 Assumptions acceptable.</span>
<span class="co">#=> Link Function 2.329 0.126998 Assumptions acceptable.</span>
<span class="co">#=> Heteroscedasticity 5.283 0.021530 Assumptions NOT satisfied!</span>
<span class="kw">plot</span>(mod)</code></pre></div>
<p>Three of the assumptions are not satisfied. This is probably because we have only 50 data points in the data and having even 2 or 3 outliers can impact the quality of the model. So the immediate approach to address this is to remove those outliers and re-build the model. Take a look at the diagnostic plot below to arrive at your own conclusion.</p>
<p><img src='screenshots/regression-plot-big.png' width='528' height='393' /></p>
<p>From the above plot the data points: 23, 35 and 49 are marked as outliers. Lets remove them from the data and re-build the model.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">mod <-<span class="st"> </span><span class="kw">lm</span>(dist ~<span class="st"> </span>speed, <span class="dt">data=</span>cars[-<span class="kw">c</span>(<span class="dv">23</span>, <span class="dv">35</span>, <span class="dv">49</span>), ])
gvlma::<span class="kw">gvlma</span>(mod)
<span class="co">#=> Value p-value Decision</span>
<span class="co">#=> Global Stat 7.5910 0.10776 Assumptions acceptable.</span>
<span class="co">#=> Skewness 0.8129 0.36725 Assumptions acceptable.</span>
<span class="co">#=> Kurtosis 0.2210 0.63831 Assumptions acceptable.</span>
<span class="co">#=> Link Function 3.2239 0.07257 Assumptions acceptable.</span>
<span class="co">#=> Heteroscedasticity 3.3332 0.06789 Assumptions acceptable.</span>
<span class="co">#=> plot(mod)</span></code></pre></div>
<p><img src='screenshots/regression-plot-big-no-outliers.png' width='528' height='393' /></p>
<p>Though the changes look minor, it is more closer to conforming with the assumptions. There is one more thing left to be explained. That is, the plot in the bottom right. It is the plot of standardized residuals against the leverage. Leverage is a measure of how much each data point influences the regression. The plot also contours values of <a href="https://en.wikipedia.org/wiki/Cook%27s_distance">Cook’s distance</a>, which reflects how much the fitted values would change if a point was deleted.</p>
<p>A point far from the centroid with a large residual can severely distort the regression. For a good regression model, the red smoothed line should stay close to the mid-line and no point should have a large cook’s distance (i.e. should not have too much influence on the model.)</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">influence.measures</span>(mod)
<span class="co">#=> dfb.1_ dfb.sped dffit cov.r cook.d hat inf</span>
<span class="co">#=> 0.087848 -0.08003 0.08834 1.184 3.99e-03 0.1187 *</span>
<span class="co">#=> 0.351238 -0.32000 0.35320 1.138 6.25e-02 0.1187 *</span>
<span class="co">#=> -0.145914 0.12652 -0.15010 1.114 1.14e-02 0.0735 </span>
<span class="co">#=> 0.285653 -0.24768 0.29384 1.075 4.31e-02 0.0735 </span>
<span class="co">#=> 0.047920 -0.04053 0.05012 1.113 1.28e-03 0.0615 </span>
<span class="co">#=> -0.136783 0.11208 -0.14670 1.083 1.09e-02 0.0511</span>
<span class="co">#=> .</span>
<span class="co">#=> .</span>
<span class="co">#=> . </span>
<span class="co">#=> 0.200260 -0.27525 -0.33127 1.051 5.43e-02 0.0687 </span>
<span class="co">#=> 0.024652 -0.03277 -0.03811 1.138 7.42e-04 0.0816 *</span>
<span class="co">#=> -0.358515 0.47655 0.55420 0.979 1.46e-01 0.0816 </span>
<span class="co">#=> -0.377456 0.50173 0.58348 0.964 1.60e-01 0.0816 </span>
<span class="co">#=> -0.195430 0.25314 0.28687 1.118 4.14e-02 0.0961</span></code></pre></div>
</div>
</div>
<div class="footer">
<hr>
<p>© 2016-17 Selva Prabhakaran. Powered by <a href="http://jekyllrb.com/">jekyll</a>,
<a href="http://yihui.name/knitr/">knitr</a>, and
<a href="http://johnmacfarlane.net/pandoc/">pandoc</a>.
This work is licensed under the <a href="http://creativecommons.org/licenses/by-nc/3.0/">Creative Commons License.</a>
</p>
</div>
</div> <!-- /container -->
<script src="//code.jquery.com/jquery.js"></script>
<script src="www/bootstrap.min.js"></script>
<script src="www/toc.js"></script>
<!-- MathJax Script -->
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]}
});
</script>
<script type="text/javascript"
src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
<!-- Google Analytics Code -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-69351797-1', 'auto');
ga('send', 'pageview');
</script>
<style type="text/css">
/* reduce spacing around math formula*/
.MathJax_Display {
margin: 0em 0em;
}
body {
font-family: 'Helvetica Neue', Roboto, Arial, sans-serif;
font-size: 16px;
line-height: 27px;
font-weight: 400;
}
blockquote p {
line-height: 1.75;
color: #717171;
}
.well li{
line-height: 28px;
}
li.dropdown-header {
display: block;
padding: 0px;
font-size: 14px;
}
</style>
</body>
</html>