<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/multiple-regression-part-3-diagnostics/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r-exercises.com/2017/03/11/multiple-regression-part-3-diagnostics/?utm_source=rss&utm_medium=rss&utm_campaign=multiple-regression-part-3-diagnostics"> R-exercises</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a target="_blank" rel="nofollow" href="http://r-exercises.com/wp-content/uploads/2017/03/mlr-exercises3-6.png?x61433"><img sizes="(max-width: 150px) 100vw, 150px" alt="" class="wp-image-4670" src="https://i2.wp.com/r-exercises.com/wp-content/uploads/2017/03/mlr-exercises3-6-150x150.png?resize=150%2C150" data-recalc-dims="1"></a></p>
<p>In the exercises below we cover some more material on multiple regression diagnostics in R. This includes added variable (partial-regression) plots, component+residual (partial-residual) plots, CERES plots, VIF values, tests for heteroscedasticity (nonconstant variance), tests for Normality, and a test for autocorrelation of residuals. These are perhaps not as common as what we have seen in Multiple Regression (Part 2), but their aid in investigating our model’s assumptions is valuable.</p>
<p>Answers to the exercises are available <a target="_blank" rel="nofollow" href="http://r-exercises.com/2017/03/11/multiple-regression-part-3-diagnostics-solutions/">here</a>.</p>
<p>If you obtained a different (correct) answer than those listed on the solutions page, please feel free to post your answer as a comment on that page.</p>
<p>Multiple Regression (Part 2) Diagnostics can be found <a target="_blank" rel="nofollow" href="http://r-exercises.com/2017/01/26/multiple-regression-part-2-diagnostics/">here</a>.</p>
<p>As usual, we will be using the dataset <code>state.x77</code>, which is part of the <code>state</code> datasets available in <code>R</code>. (Additional information about the dataset can be obtained by running <code>help(state.x77)</code>.)</p>
<p>First, please run the following code to obtain and format the data as usual:<br>
<code>data(state)</code><br>
<code>state77 &lt;- as.data.frame(state.x77)</code><br>
<code>names(state77)[4] &lt;- &quot;Life.Exp&quot;</code><br>
<code>names(state77)[6] &lt;- &quot;HS.Grad&quot;</code></p>
<p><strong>Exercise 1</strong><br>
For the model with <code>Life.Exp</code> as dependent variable, and <code>HS.Grad</code> and <code>Murder</code> as predictors, suppose we would like to study the marginal effect of each predictor variable, given that the other predictor is in the model.<br>
a. Use a function from the <code>car</code> package to obtain added-variable (partial regression) plots for this purpose.<br>
b. Re-create the added-variable plots from part a., labeling the two most influential points in the plots (according to Mahalanobis distance).</p>
<div>
<aside><img src="http://r-exercises.com/2017/03/11/multiple-regression-part-3-diagnostics/?utm_source=rss&utm_medium=rss&utm_campaign=multiple-regression-part-3-diagnostics"></aside>
<div><strong>Learn more</strong> about multiple linear regression in the online course <a target="_blank" rel="nofollow" href="http://r-exercises.com/product/linear-regression-in-r-for-data-scientists/">Linear regression in R for Data Scientists</a>. In this course you will learn how to:
<ul>
<li>Model basic and complex real world problem using linear regression</li>
<li>Understand when models are performing poorly and correct it</li>
<li>Design complex models for hierarchical data</li>
<li>And much more</li>
</ul>
</div>
</div>
<p><strong>Exercise 2</strong><br>
a. <code>Illiteracy</code> is highly correlated with both <code>HS.Grad</code> and <code>Murder</code>. To illustrate problems that occur when multicollinearity exists, suppose we would like to study the marginal effect of <code>Illiteracy</code> (only), given that <code>HS.Grad</code> and <code>Murder</code> are in the model. Use a function from the <code>car</code> package to get the relevant added-variable plot.<br>
b. From the correlation matrix in the previous Exercise Set, we know that <code>Population</code> and <code>Area</code> are the least strongly correlated variables with <code>Life.Exp</code>. Create added-variable plots for each of these two variables, given that all other six variables are in the model.</p>
<p><strong>Exercise 3</strong><br>
Consider the model with <code>HS.Grad</code>, <code>Murder</code>, <code>Income</code>, and <code>Area</code> as predictors. Create component+residual (partial-residual) plots for this model.</p>
<p><strong>Exercise 4</strong><br>
Create CERES plots for the model in Exercise 3.</p>
<p><strong>Exercise 5</strong><br>
As an illustration of high collinearities, compute VIF (Variance Inflation Factor) values for a model with <code>Life.Exp</code> as the response, that includes all the variables as predictors. Which variables seem to be causing the most problems?</p>
<p><strong>Exercise 6</strong><br>
Using a function from the package <code>lmtest</code>, conduct a Breusch-Pagan test for heteroscedasticity (non-constant variance) for the model in Exercise 1.</p>
<p><strong>Exercise 7</strong><br>
Re-do the test in the previous exercise by using a function from the <code>car</code> package.</p>
<p><strong>Exercise 8</strong><br>
The test in Exercise 6 (and 7) is for linear forms of heteroscedasticity. To test for nonlinear heteroscedasticity (e.g., “bowtie-shape” in a residual plot), conduct White’s test.</p>
<p><strong>Exercise 9</strong><br>
a. Conduct the Kolmogorov-Smirnov normality test for the residuals from the model in Exercise 1.<br>
b. Now conduct the Shapiro-Wilk normality test.<br>
Note: More Normality tests can be found in the <code>nortest</code> package.</p>
<p><strong>Exercise 10</strong><br>
For illustration purposes only, conduct the Durbin-Watson test for autocorrelation in residuals. (NOTE: This test is ONLY appropriate when the response variable is a time series, or somehow time-related (e.g., ordered by data collection time.))</p>
<div>
<h3>Related exercise sets:</h3>
<ol>
<li><a title="Multiple Regression (Part 2) – Diagnostics" target="_blank" rel="nofollow" href="http://r-exercises.com/2017/01/26/multiple-regression-part-2-diagnostics/">Multiple Regression (Part 2) – Diagnostics </a></li>
<li><a title="Model Evaluation 2" target="_blank" rel="nofollow" href="http://r-exercises.com/2016/12/22/model-evaluation-2/">Model Evaluation 2 </a></li>
<li><a title="Multiple Regression (Part 1)" target="_blank" rel="nofollow" href="http://r-exercises.com/2017/01/15/multiple-regression-part-1/">Multiple Regression (Part 1) </a></li>
<li><a target="_blank" rel="nofollow" href="http://r-exercises.com/start-here-to-learn-r/">Explore all our (>1000) R exercises</a></li>
<li><a target="_blank" rel="nofollow" href="http://r-exercises.com/r-courses/">Find an R course using our R Course Finder</a> directory</li>
</ol>
</div>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://r-exercises.com/2017/03/11/multiple-regression-part-3-diagnostics/?utm_source=rss&utm_medium=rss&utm_campaign=multiple-regression-part-3-diagnostics"> R-exercises</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/XbRG0lLCV_M"></a></div>