<?xml version="1.0" encoding="UTF-8" ?>
<!-- RSS generated by PHPBoost on Sat, 02 May 2026 07:03:30 +0200 -->

<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[Last articles - STHDA : Regression Model Diagnostics]]></title>
		<atom:link href="https://www.sthda.com/english/syndication/rss/articles/39" rel="self" type="application/rss+xml"/>
		<link>https://www.sthda.com</link>
		<description><![CDATA[Last articles - STHDA : Regression Model Diagnostics]]></description>
		<copyright>(C) 2005-2026 PHPBoost</copyright>
		<language>en</language>
		<generator>PHPBoost</generator>
		
		
		<item>
			<title><![CDATA[Linear Regression Assumptions and Diagnostics in R: Essentials]]></title>
			<link>https://www.sthda.com/english/articles/39-regression-model-diagnostics/161-linear-regression-assumptions-and-diagnostics-in-r-essentials/</link>
			<guid>https://www.sthda.com/english/articles/39-regression-model-diagnostics/161-linear-regression-assumptions-and-diagnostics-in-r-essentials/</guid>
			<description><![CDATA[<!-- START HTML -->

  <div id="rdoc">





<p>Linear regression (Chapter @ref(linear-regression)) makes several assumptions about the data at hand. This chapter describes <strong>regression assumptions</strong> and provides built-in plots for <strong>regression diagnostics</strong> in R programming language.</p>
<p>After performing a regression analysis, you should always check if the model works well for the data at hand.</p>
<p>A first step of this regression diagnostic is to inspect the significance of the regression beta coefficients, as well as, the R2 that tells us how well the linear regression model fits to the data. This has been described in the Chapters @ref(linear-regression) and @ref(cross-validation).</p>
<p>In this current chapter, you will learn additional steps to evaluate how well the model fits the data.</p>
<p>For example, the linear regression model makes the assumption that the relationship between the predictors (x) and the outcome variable is linear. This might not be true. The relationship could be polynomial or logarithmic.</p>
<p>Additionally, the data might contain some influential observations, such as outliers (or extreme values), that can affect the result of the regression.</p>
<p>Therefore, you should closely diagnostic the regression model that you built in order to detect potential problems and to check whether the assumptions made by the linear regression model are met or not.</p>
<p>To do so, we generally examine the distribution of <strong>residuals errors</strong>, that can tell you more about your data.</p>
<p>In this chapter,</p>
<ul>
<li>we start by explaining <strong>residuals errors</strong> and <strong>fitted values</strong>.</li>
<li>next, we present linear <strong>regresion assumptions</strong>, as well as, potential problems you can face when performing regression analysis.</li>
<li>finally, we describe some built-in <strong>diagnostic plots</strong> in R for testing the assumptions underlying linear regression model.</li>
</ul>
<p>Contents:</p>
<div id="TOC">
<ul>
<li><a href="#loading-required-r-packages">Loading Required R packages</a></li>
<li><a href="#example-of-data">Example of data</a></li>
<li><a href="#building-a-regression-model">Building a regression model</a></li>
<li><a href="#fitted-values-and-residuals">Fitted values and residuals</a></li>
<li><a href="#regression-assumptions">Regression assumptions</a></li>
<li><a href="#regression-diagnostics-reg-diag">Regression diagnostics {reg-diag}</a><ul>
<li><a href="#diagnostic-plots">Diagnostic plots</a></li>
</ul></li>
<li><a href="#linearity-of-the-data">Linearity of the data</a></li>
<li><a href="#homogeneity-of-variance">Homogeneity of variance</a></li>
<li><a href="#normality-of-residuals">Normality of residuals</a></li>
<li><a href="#outliers-and-high-levarage-points">Outliers and high levarage points</a></li>
<li><a href="#influential-values">Influential values</a></li>
<li><a href="#discussion">Discussion</a></li>
<li><a href="#references">References</a></li>
</ul>
</div>
<br/>
<div class = "small-block content-privileged-friends navr-book">
  <p>The Book:</p>
        <a href = "https://www.sthda.com/english/web/5-bookadvisor/54-machine-learning-essentials/">
          <img src = "https://www.sthda.com/english/upload/machine-learning-essentials-frontcover-200px.png" /><br/>
     Machine Learning Essentials: Practical Guide in R
      </a>
</div>
<div class="spacer"></div>


<div id="loading-required-r-packages" class="section level2">
<h2>Loading Required R packages</h2>
<ul>
<li><code>tidyverse</code> for easy data manipulation and visualization</li>
<li><code>broom</code>: creates a tidy data frame from statistical test results</li>
</ul>
<pre class="r"><code>library(tidyverse)
library(broom)
theme_set(theme_classic())</code></pre>
</div>
<div id="example-of-data" class="section level2">
<h2>Example of data</h2>
<p>We’ll use the data set <code>marketing</code> [datarium package], introduced in Chapter @ref(regression-analysis).</p>
<pre class="r"><code># Load the data
data("marketing", package = "datarium")
# Inspect the data
sample_n(marketing, 3)</code></pre>
<pre><code>##     youtube facebook newspaper sales
## 58    163.4     23.0      19.9  15.8
## 157   112.7     52.2      60.6  18.4
## 81     91.7     32.0      26.8  14.2</code></pre>
</div>
<div id="building-a-regression-model" class="section level2">
<h2>Building a regression model</h2>
<p>We build a model to predict sales on the basis of advertising budget spent in youtube medias.</p>
<pre class="r"><code>model <- lm(sales ~ youtube, data = marketing)
model</code></pre>
<pre><code>## 
## Call:
## lm(formula = sales ~ youtube, data = marketing)
## 
## Coefficients:
## (Intercept)      youtube  
##      8.4391       0.0475</code></pre>
<p>Our regression equation is: <code>y = 8.43 + 0.07*x</code>, that is <code>sales = 8.43 + 0.047*youtube</code>.</p>
<p>Before, describing regression assumptions and regression diagnostics, we start by explaining two key concepts in regression analysis: Fitted values and residuals errors. These are important for understanding the diagnostic plots presented hereafter.</p>
</div>
<div id="fitted-values-and-residuals" class="section level2">
<h2>Fitted values and residuals</h2>
<p>The <strong>fitted</strong> (or <strong>predicted</strong>) values are the y-values that you would expect for the given x-values according to the built regression model (or visually, the best-fitting straight regression line).</p>
<p>In our example, for a given youtube advertising budget, the fitted (predicted) sales value would be, <code>sales =  8.44 + 0.0048*youtube</code>.</p>
<p>From the scatter plot below, it can be seen that not all the data points fall exactly on the estimated regression line. This means that, for a given youtube advertising budget, the observed (or measured) sale values can be different from the predicted sale values. The difference is called the <strong>residual errors</strong>, represented by a vertical red lines.</p>
<p><img src="https://www.sthda.com/english/sthda-upload/images/machine-learning-essentials/linear-regression.png" alt="Linear regression" /></p>
<p>In R, you can easily augment your data to add fitted values and residuals by using the function <code>augment()</code> [broom package]. Let’s call the output <code>model.diag.metrics</code> because it contains several metrics useful for regression diagnostics. We’ll describe theme later.</p>
<pre class="r"><code>model.diag.metrics <- augment(model)
head(model.diag.metrics)</code></pre>
<pre><code>##   sales youtube .fitted .se.fit .resid    .hat .sigma  .cooksd .std.resid
## 1 26.52   276.1   21.56   0.385  4.955 0.00970   3.90 7.94e-03     1.2733
## 2 12.48    53.4   10.98   0.431  1.502 0.01217   3.92 9.20e-04     0.3866
## 3 11.16    20.6    9.42   0.502  1.740 0.01649   3.92 1.69e-03     0.4486
## 4 22.20   181.8   17.08   0.277  5.119 0.00501   3.90 4.34e-03     1.3123
## 5 15.48   217.0   18.75   0.297 -3.273 0.00578   3.91 2.05e-03    -0.8393
## 6  8.64    10.4    8.94   0.525 -0.295 0.01805   3.92 5.34e-05    -0.0762</code></pre>
<p>Among the table columns, there are:</p>
<ul>
<li><code>youtube</code>: the invested youtube advertising budget</li>
<li><code>sales</code>: the observed sale values</li>
<li><code>.fitted</code>: the fitted sale values</li>
<li><code>.resid</code>: the residual errors</li>
<li>…</li>
</ul>
<p>The following R code plots the residuals error (in red color) between observed values and the fitted regression line. Each vertical red segments represents the residual error between an observed sale value and the corresponding predicted (i.e. fitted) value.</p>
<pre class="r"><code>ggplot(model.diag.metrics, aes(youtube, sales)) +
  geom_point() +
  stat_smooth(method = lm, se = FALSE) +
  geom_segment(aes(xend = youtube, yend = .fitted), color = "red", size = 0.3)</code></pre>
<p><img src="https://www.sthda.com/english/sthda-upload/figures/machine-learning-essentials/011-regression-assumptions-and-diagnostics-residual-error-1.png" width="384" /></p>
<div class="block">
<p>
In order to check regression assumptions, we’ll examine the distribution of residuals.
</p>
</div>
</div>
<div id="regression-assumptions" class="section level2">
<h2>Regression assumptions</h2>
<p>Linear regression makes several assumptions about the data, such as :</p>
<ol style="list-style-type: decimal">
<li><strong>Linearity of the data</strong>. The relationship between the predictor (x) and the outcome (y) is assumed to be linear.</li>
<li><strong>Normality of residuals</strong>. The residual errors are assumed to be normally distributed.</li>
<li><strong>Homogeneity of residuals variance</strong>. The residuals are assumed to have a constant variance (<strong>homoscedasticity</strong>)</li>
<li><strong>Independence of residuals error terms</strong>.</li>
</ol>
<p>You should check whether or not these assumptions hold true. Potential problems include:</p>
<ol style="list-style-type: decimal">
<li><strong>Non-linearity</strong> of the outcome - predictor relationships</li>
<li><strong>Heteroscedasticity</strong>: Non-constant variance of error terms.</li>
<li><strong>Presence of influential values</strong> in the data that can be:
<ul>
<li>Outliers: extreme values in the outcome (y) variable</li>
<li>High-leverage points: extreme values in the predictors (x) variable</li>
</ul></li>
</ol>
<p>All these assumptions and potential problems can be checked by producing some diagnostic plots visualizing the residual errors.</p>
</div>
<div id="regression-diagnostics-reg-diag" class="section level2">
<h2>Regression diagnostics {reg-diag}</h2>
<div id="diagnostic-plots" class="section level3">
<h3>Diagnostic plots</h3>
<p>Regression diagnostics plots can be created using the R base function <code>plot()</code> or the <code>autoplot()</code> function [ggfortify package], which creates a ggplot2-based graphics.</p>
<ul>
<li>Create the diagnostic plots with the R base function:</li>
</ul>
<pre class="r"><code>par(mfrow = c(2, 2))
plot(model)</code></pre>
<p><img src="https://www.sthda.com/english/sthda-upload/figures/machine-learning-essentials/011-regression-assumptions-and-diagnostics-diagnostic-plots-1.png" width="672" /></p>
<ul>
<li>Create the diagnostic plots using ggfortify:</li>
</ul>
<pre class="r"><code>library(ggfortify)
autoplot(model)</code></pre>
<p>The diagnostic plots show residuals in four different ways:</p>
<ol style="list-style-type: decimal">
<li><p><strong>Residuals vs Fitted</strong>. Used to check the linear relationship assumptions. A horizontal line, without distinct patterns is an indication for a linear relationship, what is good.</p></li>
<li><p><strong>Normal Q-Q</strong>. Used to examine whether the residuals are normally distributed. It’s good if residuals points follow the straight dashed line.</p></li>
<li><p><strong>Scale-Location</strong> (or Spread-Location). Used to check the homogeneity of variance of the residuals (homoscedasticity). Horizontal line with equally spread points is a good indication of homoscedasticity. This is not the case in our example, where we have a heteroscedasticity problem.</p></li>
<li><p><strong>Residuals vs Leverage</strong>. Used to identify influential cases, that is extreme values that might influence the regression results when included or excluded from the analysis. This plot will be described further in the next sections.</p></li>
</ol>
<p>The four plots show the top 3 most extreme data points labeled with with the row numbers of the data in the data set. They might be potentially problematic. You might want to take a close look at them individually to check if there is anything special for the subject or if it could be simply data entry errors. We’ll discuss about this in the following sections.</p>
<p>The metrics used to create the above plots are available in the <code>model.diag.metrics</code> data, described in the previous section.</p>
<pre class="r"><code># Add observations indices and
# drop some columns (.se.fit, .sigma) for simplification
model.diag.metrics <- model.diag.metrics %>%
  mutate(index = 1:nrow(model.diag.metrics)) %>%
  select(index, everything(), -.se.fit, -.sigma)
# Inspect the data
head(model.diag.metrics, 4)</code></pre>
<pre><code>##   index sales youtube .fitted .resid    .hat .cooksd .std.resid
## 1     1  26.5   276.1   21.56   4.96 0.00970 0.00794      1.273
## 2     2  12.5    53.4   10.98   1.50 0.01217 0.00092      0.387
## 3     3  11.2    20.6    9.42   1.74 0.01649 0.00169      0.449
## 4     4  22.2   181.8   17.08   5.12 0.00501 0.00434      1.312</code></pre>
<p>We’ll use mainly the following columns:</p>
<ul>
<li><code>.fitted</code>: fitted values</li>
<li><code>.resid</code>: residual errors</li>
<li><code>.hat</code>: hat values, used to detect high-leverage points (or extreme values in the predictors x variables)</li>
<li><code>.std.resid</code>: standardized residuals, which is the residuals divided by their standard errors. Used to detect outliers (or extreme values in the outcome y variable)</li>
<li><code>.cooksd</code>: Cook’s distance, used to detect influential values, which can be an outlier or a high leverage point</li>
</ul>
<p>In the following section, we’ll describe, in details, how to use these graphs and metrics to check the regression assumptions and to diagnostic potential problems in the model.</p>
</div>
</div>
<div id="linearity-of-the-data" class="section level2">
<h2>Linearity of the data</h2>
<p>The linearity assumption can be checked by inspecting the <strong>Residuals vs Fitted</strong> plot (1st plot):</p>
<pre class="r"><code>plot(model, 1)</code></pre>
<p><img src="https://www.sthda.com/english/sthda-upload/figures/machine-learning-essentials/011-regression-assumptions-and-diagnostics-residual-vs-fitted-plot-1.png" width="432" /></p>
<p>Ideally, the residual plot will show no fitted pattern. That is, the red line should be approximately horizontal at zero. The presence of a pattern may indicate a problem with some aspect of the linear model.</p>
<div class="success">
<p>
In our example, there is no pattern in the residual plot. This suggests that we can assume linear relationship between the predictors and the outcome variables.
</p>
</div>
<div class="warning">
<p>
Note that, if the residual plot indicates a non-linear relationship in the data, then a simple approach is to use non-linear transformations of the predictors, such as log(x), sqrt(x) and x^2, in the regression model.
</p>
</div>
</div>
<div id="homogeneity-of-variance" class="section level2">
<h2>Homogeneity of variance</h2>
<p>This assumption can be checked by examining the <em>scale-location plot</em>, also known as the <em>spread-location plot</em>.</p>
<pre class="r"><code>plot(model, 3)</code></pre>
<p><img src="https://www.sthda.com/english/sthda-upload/figures/machine-learning-essentials/011-regression-assumptions-and-diagnostics-scale-location-plot-1.png" width="384" /></p>
<p>This plot shows if residuals are spread equally along the ranges of predictors. It’s good if you see a horizontal line with equally spread points. In our example, this is not the case.</p>
<p>It can be seen that the variability (variances) of the residual points increases with the value of the fitted outcome variable, suggesting non-constant variances in the residuals errors (or <em>heteroscedasticity</em>).</p>
<p>A possible solution to reduce the heteroscedasticity problem is to use a log or square root transformation of the outcome variable (y).</p>
<pre class="r"><code>model2 <- lm(log(sales) ~ youtube, data = marketing)
plot(model2, 3)</code></pre>
<p><img src="https://www.sthda.com/english/sthda-upload/figures/machine-learning-essentials/011-regression-assumptions-and-diagnostics-heteroscedasticity-1.png" width="384" /></p>
</div>
<div id="normality-of-residuals" class="section level2">
<h2>Normality of residuals</h2>
<p>The QQ plot of residuals can be used to visually check the normality assumption. The normal probability plot of residuals should approximately follow a straight line.</p>
<p>In our example, all the points fall approximately along this reference line, so we can assume normality.</p>
<pre class="r"><code>plot(model, 2)</code></pre>
<p><img src="https://www.sthda.com/english/sthda-upload/figures/machine-learning-essentials/011-regression-assumptions-and-diagnostics-normality-of-residuals-1.png" width="384" /></p>
</div>
<div id="outliers-and-high-levarage-points" class="section level2">
<h2>Outliers and high levarage points</h2>
<p><strong>Outliers</strong>:</p>
<p>An outlier is a point that has an extreme outcome variable value. The presence of outliers may affect the interpretation of the model, because it increases the RSE.</p>
<p>Outliers can be identified by examining the <em>standardized residual</em> (or <em>studentized residual</em>), which is the residual divided by its estimated standard error. Standardized residuals can be interpreted as the number of standard errors away from the regression line.</p>
<p>Observations whose standardized residuals are greater than 3 in absolute value are possible outliers <span class="citation">(James et al. 2014)</span>.</p>
<p><strong>High leverage points</strong>:</p>
<p>A data point has high leverage, if it has extreme predictor x values. This can be detected by examining the leverage statistic or the <em>hat-value</em>. A value of this statistic above <code>2(p + 1)/n</code> indicates an observation with high leverage <span class="citation">(P. Bruce and Bruce 2017)</span>; where, <code>p</code> is the number of predictors and <code>n</code> is the number of observations.</p>
<p>Outliers and high leverage points can be identified by inspecting the <em>Residuals vs Leverage</em> plot:</p>
<pre class="r"><code>plot(model, 5)</code></pre>
<p><img src="https://www.sthda.com/english/sthda-upload/figures/machine-learning-essentials/011-regression-assumptions-and-diagnostics-residuals-vs-leverage-points-1.png" width="384" /></p>
<div class="success">
<p>
The plot above highlights the top 3 most extreme points (#26, #36 and #179), with a standardized residuals below -2. However, there is no outliers that exceed 3 standard deviations, what is good.
</p>
<p>
Additionally, there is no high leverage point in the data. That is, all data points, have a leverage statistic below 2(p + 1)/n = 4/200 = 0.02.
</p>
</div>
</div>
<div id="influential-values" class="section level2">
<h2>Influential values</h2>
<p>An influential value is a value, which inclusion or exclusion can alter the results of the regression analysis. Such a value is associated with a large residual.</p>
<p>Not all outliers (or extreme data points) are influential in linear regression analysis.</p>
<p>Statisticians have developed a metric called <em>Cook’s distance</em> to determine the influence of a value. This metric defines influence as a combination of leverage and residual size.</p>
<p>A rule of thumb is that an observation has high influence if Cook’s distance exceeds <code>4/(n - p - 1)</code><span class="citation">(P. Bruce and Bruce 2017)</span>, where <code>n</code> is the number of observations and <code>p</code> the number of predictor variables.</p>
<p>The <em>Residuals vs Leverage</em> plot can help us to find influential observations if any. On this plot, outlying values are generally located at the upper right corner or at the lower right corner. Those spots are the places where data points can be influential against a regression line.</p>
<p>The following plots illustrate the Cook’s distance and the leverage of our model:</p>
<pre class="r"><code># Cook&amp;#39;s distance
plot(model, 4)
# Residuals vs Leverage
plot(model, 5)</code></pre>
<p><img src="https://www.sthda.com/english/sthda-upload/figures/machine-learning-essentials/011-regression-assumptions-and-diagnostics-cooks-distance-1.png" width="720" /></p>
<p>By default, the top 3 most extreme values are labelled on the Cook’s distance plot. If you want to label the top 5 extreme values, specify the option <code>id.n</code> as follow:</p>
<pre class="r"><code>plot(model, 4, id.n = 5)</code></pre>
<p>If you want to look at these top 3 observations with the highest Cook’s distance in case you want to assess them further, type this R code:</p>
<pre class="r"><code>model.diag.metrics %>%
  top_n(3, wt = .cooksd)</code></pre>
<pre><code>##   index sales youtube .fitted .resid   .hat .cooksd .std.resid
## 1    26  14.4     315    23.4  -9.04 0.0142  0.0389      -2.33
## 2    36  15.4     349    25.0  -9.66 0.0191  0.0605      -2.49
## 3   179  14.2     332    24.2 -10.06 0.0165  0.0563      -2.59</code></pre>
<div class="warning">
<p>
When data points have high Cook’s distance scores and are to the upper or lower right of the leverage plot, they have leverage meaning they are influential to the regression results. The regression results will be altered if we exclude those cases.
</p>
</div>
<div class="success">
<p>
In our example, the data don’t present any influential points. Cook’s distance lines (a red dashed line) are not shown on the Residuals vs Leverage plot because all points are well inside of the Cook’s distance lines.
</p>
</div>
<p>Let’s show now another example, where the data contain two extremes values with potential influence on the regression results:</p>
<pre class="r"><code>df2 <- data.frame(
  x = c(marketing$youtube, 500, 600),
  y = c(marketing$sales, 80, 100)
)
model2 <- lm(y ~ x, df2)</code></pre>
<p>Create the <em>Residuals vs Leverage</em> plot of the two models:</p>
<pre class="r"><code># Cook&amp;#39;s distance
plot(model2, 4)
# Residuals vs Leverage
plot(model2, 5)</code></pre>
<p><img src="https://www.sthda.com/english/sthda-upload/figures/machine-learning-essentials/011-regression-assumptions-and-diagnostics-influential-observations-1.png" width="720" /></p>
<p>On the Residuals vs Leverage plot, look for a data point outside of a dashed line, Cook’s distance. When the points are outside of the Cook’s distance, this means that they have high Cook’s distance scores. In this case, the values are influential to the regression results. The regression results will be altered if we exclude those cases.</p>
<p>In the above example 2, two data points are far beyond the Cook’s distance lines. The other residuals appear clustered on the left. The plot identified the influential observation as #201 and #202. If you exclude these points from the analysis, the slope coefficient changes from 0.06 to 0.04 and R2 from 0.5 to 0.6. Pretty big impact!</p>
</div>
<div id="discussion" class="section level2">
<h2>Discussion</h2>
<p>This chapter describes linear regression assumptions and shows how to diagnostic potential problems in the model.</p>
<p>The diagnostic is essentially performed by visualizing the residuals. Having patterns in residuals is not a stop signal. Your current regression model might not be the best way to understand your data.</p>
<p>Potential problems might be:</p>
<ul>
<li><p>A non-linear relationships between the outcome and the predictor variables. When facing to this problem, one solution is to include a quadratic term, such as polynomial terms or log transformation. See Chapter @ref(polynomial-and-spline-regression).</p></li>
<li><p>Existence of important variables that you left out from your model. Other variables you didn’t include (e.g., age or gender) may play an important role in your model and data. See Chapter @ref(confounding-variables).</p></li>
<li><p>Presence of outliers. If you believe that an outlier has occurred due to an error in data collection and entry, then one solution is to simply remove the concerned observation.</p></li>
</ul>
</div>
<div id="references" class="section level2 unnumbered">
<h2>References</h2>
<div id="refs" class="references">
<div id="ref-bruce2017">
<p>Bruce, Peter, and Andrew Bruce. 2017. <em>Practical Statistics for Data Scientists</em>. O’Reilly Media.</p>
</div>
<div id="ref-james2014">
<p>James, Gareth, Daniela Witten, Trevor Hastie, and Robert Tibshirani. 2014. <em>An Introduction to Statistical Learning: With Applications in R</em>. Springer Publishing Company, Incorporated.</p>
</div>
</div>
</div>


</div><!--end rdoc-->


<!-- END HTML -->]]></description>
			<pubDate>Sun, 11 Mar 2018 12:20:00 +0100</pubDate>
			
		</item>
		
		<item>
			<title><![CDATA[Multicollinearity Essentials and VIF in R]]></title>
			<link>https://www.sthda.com/english/articles/39-regression-model-diagnostics/160-multicollinearity-essentials-and-vif-in-r/</link>
			<guid>https://www.sthda.com/english/articles/39-regression-model-diagnostics/160-multicollinearity-essentials-and-vif-in-r/</guid>
			<description><![CDATA[<!-- START HTML -->

  <div id="rdoc">





<p>In multiple regression (Chapter @ref(linear-regression)), two or more predictor variables might be correlated with each other. This situation is referred as <em>collinearity</em>.</p>
<p>There is an extreme situation, called <strong>multicollinearity</strong>, where collinearity exists between three or more variables even if no pair of variables has a particularly high correlation. This means that there is redundancy between predictor variables.</p>
<p>In the presence of multicollinearity, the solution of the regression model becomes unstable.</p>
<p>For a given predictor (p), multicollinearity can assessed by computing a score called the <strong>variance inflation factor</strong> (or <strong>VIF</strong>), which measures how much the variance of a regression coefficient is inflated due to multicollinearity in the model.</p>
<p>The smallest possible value of VIF is one (absence of multicollinearity). As a rule of thumb, a VIF value that exceeds 5 or 10 indicates a problematic amount of collinearity <span class="citation">(James et al. 2014)</span>.</p>
<p>When faced to multicollinearity, the concerned variables should be removed, since the presence of multicollinearity implies that the information that this variable provides about the response is redundant in the presence of the other variables <span class="citation">(James et al. 2014,<span class="citation">P. Bruce and Bruce (2017)</span>)</span>.</p>
<p>This chapter describes how to detect multicollinearity in a regression model using R.</p>
<p>Contents:</p>
<div id="TOC">
<ul>
<li><a href="#loading-required-r-packages">Loading Required R packages</a></li>
<li><a href="#preparing-the-data">Preparing the data</a></li>
<li><a href="#building-a-regression-model">Building a regression model</a></li>
<li><a href="#detecting-multicollinearity">Detecting multicollinearity</a></li>
<li><a href="#dealing-with-multicollinearity">Dealing with multicollinearity</a></li>
<li><a href="#discussion">Discussion</a></li>
<li><a href="#references">References</a></li>
</ul>
</div>
<br/>
<div class = "small-block content-privileged-friends navr-book">
  <p>The Book:</p>
        <a href = "https://www.sthda.com/english/web/5-bookadvisor/54-machine-learning-essentials/">
          <img src = "https://www.sthda.com/english/upload/machine-learning-essentials-frontcover-200px.png" /><br/>
     Machine Learning Essentials: Practical Guide in R
      </a>
</div>
<div class="spacer"></div>


<div id="loading-required-r-packages" class="section level2">
<h2>Loading Required R packages</h2>
<ul>
<li><code>tidyverse</code> for easy data manipulation and visualization</li>
<li><code>caret</code> for easy machine learning workflow</li>
</ul>
<pre class="r"><code>library(tidyverse)
library(caret)</code></pre>
</div>
<div id="preparing-the-data" class="section level2">
<h2>Preparing the data</h2>
<p>We’ll use the <code>Boston</code> data set [in <code>MASS</code> package], introduced in Chapter @ref(regression-analysis), for predicting the median house value (<code>mdev</code>), in Boston Suburbs, based on multiple predictor variables.</p>
<p>We’ll randomly split the data into training set (80% for building a predictive model) and test set (20% for evaluating the model). Make sure to set seed for reproducibility.</p>
<pre class="r"><code># Load the data
data("Boston", package = "MASS")
# Split the data into training and test set
set.seed(123)
training.samples <- Boston$medv %>%
  createDataPartition(p = 0.8, list = FALSE)
train.data  <- Boston[training.samples, ]
test.data <- Boston[-training.samples, ]</code></pre>
</div>
<div id="building-a-regression-model" class="section level2">
<h2>Building a regression model</h2>
<p>The following regression model include all predictor variables:</p>
<pre class="r"><code># Build the model
model1 <- lm(medv ~., data = train.data)
# Make predictions
predictions <- model1 %>% predict(test.data)
# Model performance
data.frame(
  RMSE = RMSE(predictions, test.data$medv),
  R2 = R2(predictions, test.data$medv)
)</code></pre>
<pre><code>##   RMSE   R2
## 1 4.99 0.67</code></pre>
</div>
<div id="detecting-multicollinearity" class="section level2">
<h2>Detecting multicollinearity</h2>
<p>The R function <code>vif()</code> [car package] can be used to detect multicollinearity in a regression model:</p>
<pre class="r"><code>car::vif(model1)</code></pre>
<pre><code>##    crim      zn   indus    chas     nox      rm     age     dis     rad 
##    1.87    2.36    3.90    1.06    4.47    2.01    3.02    3.96    7.80 
##     tax ptratio   black   lstat 
##    9.16    1.91    1.31    2.97</code></pre>
<p>In our example, the VIF score for the predictor variable <code>tax</code> is very high (VIF = 9.16). This might be problematic.</p>
</div>
<div id="dealing-with-multicollinearity" class="section level2">
<h2>Dealing with multicollinearity</h2>
<p>In this section, we’ll update our model by removing the the predictor variables with high VIF value:</p>
<pre class="r"><code># Build a model excluding the tax variable
model2 <- lm(medv ~. -tax, data = train.data)
# Make predictions
predictions <- model2 %>% predict(test.data)
# Model performance
data.frame(
  RMSE = RMSE(predictions, test.data$medv),
  R2 = R2(predictions, test.data$medv)
)</code></pre>
<pre><code>##   RMSE    R2
## 1 5.01 0.671</code></pre>
<p>It can be seen that removing the <code>tax</code> variable does not affect very much the model performance metrics.</p>
</div>
<div id="discussion" class="section level2">
<h2>Discussion</h2>
<p>This chapter describes how to detect and deal with multicollinearity in regression models. Multicollinearity problems consist of including, in the model, different variables that have a similar predictive relationship with the outcome. This can be assessed for each predictor by computing the VIF value.</p>
<p>Any variable with a high VIF value (above 5 or 10) should be removed from the model. This leads to a simpler model without compromising the model accuracy, which is good.</p>
<p>Note that, in a large data set presenting multiple correlated predictor variables, you can perform principal component regression and partial least square regression strategies. See Chapter @ref(pcr-and-pls-regression).</p>
</div>
<div id="references" class="section level2 unnumbered">
<h2>References</h2>
<div id="refs" class="references">
<div id="ref-bruce2017">
<p>Bruce, Peter, and Andrew Bruce. 2017. <em>Practical Statistics for Data Scientists</em>. O’Reilly Media.</p>
</div>
<div id="ref-james2014">
<p>James, Gareth, Daniela Witten, Trevor Hastie, and Robert Tibshirani. 2014. <em>An Introduction to Statistical Learning: With Applications in R</em>. Springer Publishing Company, Incorporated.</p>
</div>
</div>
</div>


</div><!--end rdoc-->


<!-- END HTML -->]]></description>
			<pubDate>Sun, 11 Mar 2018 12:13:00 +0100</pubDate>
			
		</item>
		
		<item>
			<title><![CDATA[Confounding Variable Essentials]]></title>
			<link>https://www.sthda.com/english/articles/39-regression-model-diagnostics/159-confounding-variable-essentials/</link>
			<guid>https://www.sthda.com/english/articles/39-regression-model-diagnostics/159-confounding-variable-essentials/</guid>
			<description><![CDATA[<!-- START HTML -->

  <div id="rdoc">




<p>A <strong>Confounding variable</strong> is an important variable that should be included in the predictive model but you omit it.Naive interpretation of such models can lead to invalid conclusions.</p>
<p>For example, consider that we want to model life expentency in different countries based on the GDP per capita, using the <code>gapminder</code> data set:</p>
<pre class="r"><code>library(gapminder)
lm(lifeExp ~ gdpPercap, data = gapminder)</code></pre>
<p>In this example, it is clear that the continent is an important variable: countries in Europe are estimated to have a higher life expectancy compared to countries in Africa. Therefore, continent is a confounding variable that should be included in the model:</p>
<pre class="r"><code>lm(lifeExp ~ gdpPercap + continent, data = gapminder)</code></pre>


<br/>
<div class = "small-block content-privileged-friends navr-book">
  <p>The Book:</p>
        <a href = "https://www.sthda.com/english/web/5-bookadvisor/54-machine-learning-essentials/">
          <img src = "https://www.sthda.com/english/upload/machine-learning-essentials-frontcover-200px.png" /><br/>
     Machine Learning Essentials: Practical Guide in R
      </a>
</div>
<div class="spacer"></div>


</div><!--end rdoc-->


<!-- END HTML -->]]></description>
			<pubDate>Sun, 11 Mar 2018 12:07:00 +0100</pubDate>
			
		</item>
		
	</channel>
</rss>
