<?xml version="1.0" encoding="UTF-8" ?>
<!-- RSS generated by PHPBoost on Mon, 13 Apr 2026 00:29:37 +0200 -->

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

  <div id="rdoc">





<p><strong>Linear regression</strong> (or <strong>linear model</strong>) is used to predict a quantitative outcome variable (y) on the basis of one or multiple predictor variables (x) <span class="citation">(James et al. 2014,<span class="citation">P. Bruce and Bruce (2017)</span>)</span>.</p>
<p>The goal is to build a mathematical formula that defines y as a function of the x variable. Once, we built a statistically significant model, it’s possible to use it for predicting future outcome on the basis of new x values.</p>
<p>When you build a regression model, you need to assess the performance of the predictive model. In other words, you need to evaluate how well the model is in predicting the outcome of a new test data that have not been used to build the model.</p>
<p>Two important metrics are commonly used to assess the performance of the predictive regression model:</p>
<ul>
<li><strong>Root Mean Squared Error</strong>, which measures the model prediction error. It corresponds to the average difference between the observed known values of the outcome and the predicted value by the model. RMSE is computed as <code>RMSE = mean((observeds - predicteds)^2) %>% sqrt()</code>. The lower the RMSE, the better the model.</li>
<li><strong>R-square</strong>, representing the squared correlation between the observed known outcome values and the predicted values by the model. The higher the R2, the better the model.</li>
</ul>
<p>A simple workflow to build to build a predictive regression model is as follow:</p>
<ol style="list-style-type: decimal">
<li>Randomly split your data into training set (80%) and test set (20%)</li>
<li>Build the regression model using the training set</li>
<li>Make predictions using the test set and compute the model accuracy metrics</li>
</ol>
<p>In this chapter, you will learn:</p>
<ul>
<li>the basics and the formula of linear regression,</li>
<li>how to compute simple and multiple regression models in R,</li>
<li>how to make predictions of the outcome of new data,</li>
<li>how to assess the performance of the model</li>
</ul>
<p>Contents:</p>
<div id="TOC">
<ul>
<li><a href="#formula">Formula</a></li>
<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="#computing-linear-regression">Computing linear regression</a><ul>
<li><a href="#quick-start-r-code">Quick start R code</a></li>
<li><a href="#simple-linear-regression">Simple linear regression</a></li>
<li><a href="#multiple-linear-regression">Multiple linear regression</a></li>
</ul></li>
<li><a href="#interpretation">Interpretation</a><ul>
<li><a href="#model-summary">Model summary</a></li>
<li><a href="#coefficients-significance">Coefficients significance</a></li>
<li><a href="#model-accuracy">Model accuracy</a></li>
</ul></li>
<li><a href="#making-predictions">Making predictions</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="formula" class="section level2">
<h2>Formula</h2>
<p>The mathematical formula of the linear regression can be written as follow:</p>
<p><code>y = b0 + b1*x + e</code></p>
<p>We read this as “y is modeled as beta1 (<code>b1</code>) times <code>x</code>, plus a constant beta0 (<code>b0</code>), plus an error term <code>e</code>.”</p>
<p>When you have multiple predictor variables, the equation can be written as <code>y = b0 + b1*x1 + b2*x2 + ... + bn*xn</code>, where:</p>
<ul>
<li>b0 is the intercept,</li>
<li>b1, b2, …, bn are the regression weights or coefficients associated with the predictors x1, x2, …, xn.</li>
<li><code>e</code> is the <em>error term</em> (also known as the <em>residual errors</em>), the part of y that can be explained by the regression model</li>
</ul>
<div class="notice">
<p>
Note that, b0, b1, b2, … and bn are known as the regression beta coefficients or parameters.
</p>
</div>
<p>The figure below illustrates a simple linear regression model, where:</p>
<ul>
<li>the best-fit regression line is in blue</li>
<li>the intercept (b0) and the slope (b1) are shown in green</li>
<li>the error terms (e) are represented by vertical red lines</li>
</ul>
<p><img src="https://www.sthda.com/english/sthda-upload/images/machine-learning-essentials/linear-regression.png" alt="Linear regression" /></p>
<p>From the scatter plot above, it can be seen that not all the data points fall exactly on the fitted regression line. Some of the points are above the blue curve and some are below it; overall, the residual errors (e) have approximately mean zero.</p>
<p>The sum of the squares of the residual errors are called the <strong>Residual Sum of Squares</strong> or <strong>RSS</strong>.  </p>
<p>The average variation of points around the fitted regression line is called the <strong>Residual Standard Error</strong> (<strong>RSE</strong>). This is one the metrics used to evaluate the overall quality of the fitted regression model. The lower the RSE, the better it is.</p>
<p>Since the mean error term is zero, the outcome variable y can be approximately estimated as follow:</p>
<p><code>y ~ b0 + b1*x</code></p>
<p>Mathematically, the beta coefficients (b0 and b1) are determined so that the RSS is as minimal as possible. This method of determining the beta coefficients is technically called <strong>least squares</strong> regression or <strong>ordinary least squares</strong> (OLS) regression.</p>
<p>Once, the beta coefficients are calculated, a t-test is performed to check whether or not these coefficients are significantly different from zero. A non-zero beta coefficients means that there is a significant relationship between the predictors (x) and the outcome variable (y).</p>
</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)
theme_set(theme_bw())</code></pre>
</div>
<div id="preparing-the-data" class="section level2">
<h2>Preparing the data</h2>
<p>We’ll use the <code>marketing</code> data set, introduced in the Chapter @ref(regression-analysis), for predicting sales units on the basis of the amount of money spent in the three advertising medias (youtube, facebook and newspaper)</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("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>
<pre class="r"><code># Split the data into training and test set
set.seed(123)
training.samples <- marketing$sales %>%
  createDataPartition(p = 0.8, list = FALSE)
train.data  <- marketing[training.samples, ]
test.data <- marketing[-training.samples, ]</code></pre>
</div>
<div id="computing-linear-regression" class="section level2">
<h2>Computing linear regression</h2>
<p>The R function <code>lm()</code> is used to compute linear regression model.</p>
<div id="quick-start-r-code" class="section level3">
<h3>Quick start R code</h3>
<pre class="r"><code># Build the model
model <- lm(sales ~., data = train.data)
# Summarize the model
summary(model)
# Make predictions
predictions <- model %>% predict(test.data)
# Model performance
# (a) Prediction error, RMSE
RMSE(predictions, test.data$sales)
# (b) R-square
R2(predictions, test.data$sales)</code></pre>
</div>
<div id="simple-linear-regression" class="section level3">
<h3>Simple linear regression</h3>
<p>The <strong>simple linear regression</strong> is used to predict a continuous outcome variable (y) based on one single predictor variable (x).</p>
<p>In the following example, we’ll build a simple linear model to predict sales units based on the advertising budget spent on youtube. The regression equation can be written as <code>sales = b0 + b1*youtube</code>.</p>
<p>The R function <code>lm()</code> can be used to determine the beta coefficients of the linear model, as follow:</p>
<pre class="r"><code>model <- lm(sales ~ youtube, data = train.data)
summary(model)$coef</code></pre>
<pre><code>##             Estimate Std. Error t value Pr(>|t|)
## (Intercept)   8.3839    0.62442    13.4 5.22e-28
## youtube       0.0468    0.00301    15.6 7.84e-34</code></pre>
<p>The output above shows the estimate of the regression beta coefficients (column <code>Estimate</code>) and their significance levels (column <code>Pr(>|t|)</code>. The intercept (<code>b0</code>) is 8.38 and the coefficient of youtube variable is 0.046.</p>
<p>The estimated regression equation can be written as follow: <code>sales = 8.38 + 0.046*youtube</code>. Using this formula, for each new youtube advertising budget, you can predict the number of sale units.</p>
<p>For example:</p>
<ul>
<li>For a youtube advertising budget equal zero, we can expect a sale of 8.38 units.</li>
<li>For a youtube advertising budget equal 1000, we can expect a sale of 8.38 + 0.046*1000 = 55 units.</li>
</ul>
<p>Predictions can be easily made using the R function <code>predict()</code>. In the following example, we predict sales units for two youtube advertising budget: 0 and 1000.</p>
<pre class="r"><code>newdata <- data.frame(youtube = c(0,  1000))
model %>% predict(newdata)</code></pre>
<pre><code>##     1     2 
##  8.38 55.19</code></pre>
</div>
<div id="multiple-linear-regression" class="section level3">
<h3>Multiple linear regression</h3>
<p><strong>Multiple linear regression</strong> is an extension of simple linear regression for predicting an outcome variable (y) on the basis of multiple distinct predictor variables (x).</p>
<p>For example, with three predictor variables (x), the prediction of y is expressed by the following equation: <code>y = b0 + b1*x1 + b2*x2 + b3*x3</code></p>
<p>The regression beta coefficients measure the association between each predictor variable and the outcome. “b_j” can be interpreted as the average effect on y of a one unit increase in “x_j”, holding all other predictors fixed.</p>
<p>In this section, we’ll build a multiple regression model to predict sales based on the budget invested in three advertising medias: youtube, facebook and newspaper. The formula is as follow: <code>sales = b0 + b1*youtube + b2*facebook + b3*newspaper</code></p>
<p>You can compute the multiple regression model coefficients in R as follow:</p>
<pre class="r"><code>model <- lm(sales ~ youtube + facebook + newspaper, 
            data = train.data)
summary(model)$coef</code></pre>
<p>Note that, if you have many predictor variables in your data, you can simply include all the available variables in the model using <code>~.</code>:</p>
<pre class="r"><code>model <- lm(sales ~., data = train.data)
summary(model)$coef</code></pre>
<pre><code>##             Estimate Std. Error t value Pr(>|t|)
## (Intercept)  3.39188    0.44062   7.698 1.41e-12
## youtube      0.04557    0.00159  28.630 2.03e-64
## facebook     0.18694    0.00989  18.905 2.07e-42
## newspaper    0.00179    0.00677   0.264 7.92e-01</code></pre>
<p>From the output above, the coefficients table shows the beta coefficient estimates and their significance levels. Columns are:</p>
<ul>
<li><code>Estimate</code>: the intercept (b0) and the beta coefficient estimates associated to each predictor variable</li>
<li><code>Std.Error</code>: the standard error of the coefficient estimates. This represents the accuracy of the coefficients. The larger the standard error, the less confident we are about the estimate.</li>
<li><code>t value</code>: the t-statistic, which is the coefficient estimate (column 2) divided by the standard error of the estimate (column 3)</li>
<li><code>Pr(>|t|)</code>: The p-value corresponding to the t-statistic. The smaller the p-value, the more significant the estimate is.</li>
</ul>
<p>As previously described, you can easily make predictions using the R function <code>predict()</code>:</p>
<pre class="r"><code># New advertising budgets
newdata <- data.frame(
  youtube = 2000, facebook = 1000,
  newspaper = 1000
)
# Predict sales values
model %>% predict(newdata)</code></pre>
<pre><code>##   1 
## 283</code></pre>
</div>
</div>
<div id="interpretation" class="section level2">
<h2>Interpretation</h2>
<p>Before using a model for predictions, you need to assess the statistical significance of the model. This can be easily checked by displaying the statistical summary of the model.</p>
<div id="model-summary" class="section level3">
<h3>Model summary</h3>
<p>Display the statistical summary of the model as follow:</p>
<pre class="r"><code>summary(model)</code></pre>
<pre><code>## 
## Call:
## lm(formula = sales ~ ., data = train.data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -10.412  -1.110   0.348   1.422   3.499 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  3.39188    0.44062    7.70  1.4e-12 ***
## youtube      0.04557    0.00159   28.63  < 2e-16 ***
## facebook     0.18694    0.00989   18.90  < 2e-16 ***
## newspaper    0.00179    0.00677    0.26     0.79    
## ---
## Signif. codes:  0 &amp;#39;***&amp;#39; 0.001 &amp;#39;**&amp;#39; 0.01 &amp;#39;*&amp;#39; 0.05 &amp;#39;.&amp;#39; 0.1 &amp;#39; &amp;#39; 1
## 
## Residual standard error: 2.12 on 158 degrees of freedom
## Multiple R-squared:  0.89,   Adjusted R-squared:  0.888 
## F-statistic:  427 on 3 and 158 DF,  p-value: <2e-16</code></pre>
<p>The summary outputs shows 6 components, including:</p>
<ul>
<li><strong>Call</strong>. Shows the function call used to compute the regression model.</li>
<li><strong>Residuals</strong>. Provide a quick view of the distribution of the residuals, which by definition have a mean zero. Therefore, the median should not be far from zero, and the minimum and maximum should be roughly equal in absolute value.</li>
<li><strong>Coefficients</strong>. Shows the regression beta coefficients and their statistical significance. Predictor variables, that are significantly associated to the outcome variable, are marked by stars.</li>
<li><strong>Residual standard error</strong> (RSE), <strong>R-squared</strong> (R2) and the <strong>F-statistic</strong> are metrics that are used to check how well the model fits to our data.</li>
</ul>
<p>The first step in interpreting the multiple regression analysis is to examine the F-statistic and the associated p-value, at the bottom of model summary.</p>
<div class="success">
<p>
In our example, it can be seen that p-value of the F-statistic is < 2.2e-16, which is highly significant. This means that, at least, one of the predictor variables is significantly related to the outcome variable.
</p>
</div>
</div>
<div id="coefficients-significance" class="section level3">
<h3>Coefficients significance</h3>
<p>To see which predictor variables are significant, you can examine the coefficients table, which shows the estimate of regression beta coefficients and the associated t-statistic p-values.</p>
<pre class="r"><code>summary(model)$coef</code></pre>
<pre><code>##             Estimate Std. Error t value Pr(>|t|)
## (Intercept)  3.39188    0.44062   7.698 1.41e-12
## youtube      0.04557    0.00159  28.630 2.03e-64
## facebook     0.18694    0.00989  18.905 2.07e-42
## newspaper    0.00179    0.00677   0.264 7.92e-01</code></pre>
<p>For a given the predictor, the t-statistic evaluates whether or not there is significant association between the predictor and the outcome variable, that is whether the beta coefficient of the predictor is significantly different from zero.</p>
<div class="success">
<p>
It can be seen that, changing in youtube and facebook advertising budget are significantly associated to changes in sales while changes in newspaper budget is not significantly associated with sales.
</p>
</div>
<p>For a given predictor variable, the coefficient (b) can be interpreted as the average effect on y of a one unit increase in predictor, holding all other predictors fixed.</p>
<p>For example, for a fixed amount of youtube and newspaper advertising budget, spending an additional 1 000 dollars on facebook advertising leads to an increase in sales by approximately 0.1885*1000 = 189 sale units, on average.</p>
<p>The youtube coefficient suggests that for every 1 000 dollars increase in youtube advertising budget, holding all other predictors constant, we can expect an increase of 0.045*1000 = 45 sales units, on average.</p>
<p>We found that newspaper is not significant in the multiple regression model. This means that, for a fixed amount of youtube and newspaper advertising budget, changes in the newspaper advertising budget will not significantly affect sales units.</p>
<p>As the newspaper variable is not significant, it is possible to remove it from the model:</p>
<pre class="r"><code>model <- lm(sales ~ youtube + facebook, data = train.data)
summary(model)</code></pre>
<pre><code>## 
## Call:
## lm(formula = sales ~ youtube + facebook, data = train.data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -10.481  -1.104   0.349   1.423   3.486 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  3.43446    0.40877     8.4  2.3e-14 ***
## youtube      0.04558    0.00159    28.7  < 2e-16 ***
## facebook     0.18788    0.00920    20.4  < 2e-16 ***
## ---
## Signif. codes:  0 &amp;#39;***&amp;#39; 0.001 &amp;#39;**&amp;#39; 0.01 &amp;#39;*&amp;#39; 0.05 &amp;#39;.&amp;#39; 0.1 &amp;#39; &amp;#39; 1
## 
## Residual standard error: 2.11 on 159 degrees of freedom
## Multiple R-squared:  0.89,   Adjusted R-squared:  0.889 
## F-statistic:  644 on 2 and 159 DF,  p-value: <2e-16</code></pre>
<div class="success">
<p>
Finally, our model equation can be written as follow: <code>sales = 3.43+ 0.045<em>youtube + 0.187</em>facebook</code>.
</p>
</div>
</div>
<div id="model-accuracy" class="section level3">
<h3>Model accuracy</h3>
<p>Once you identified that, at least, one predictor variable is significantly associated to the outcome, you should continue the diagnostic by checking how well the model fits the data. This process is also referred to as the <em>goodness-of-fit</em></p>
<p>The overall quality of the linear regression fit can be assessed using the following three quantities, displayed in the model summary:</p>
<ol style="list-style-type: decimal">
<li>Residual Standard Error (RSE),</li>
<li>R-squared (R2) and adjusted R2,</li>
<li>F-statistic, which has been already described in the previous section</li>
</ol>
<pre><code>##    rse r.squared f.statistic  p.value
## 1 2.11      0.89         644 5.64e-77</code></pre>
<ol style="list-style-type: decimal">
<li><strong>Residual standard error</strong> (RSE). </li>
</ol>
<p>The RSE (or model <em>sigma</em>), corresponding to the prediction error, represents roughly the average difference between the observed outcome values and the predicted values by the model. The lower the RSE the best the model fits to our data.</p>
<p>Dividing the RSE by the average value of the outcome variable will give you the prediction error rate, which should be as small as possible.</p>
<div class="success">
<p>
In our example, using only youtube and facebook predictor variables, the RSE = 2.11, meaning that the observed sales values deviate from the predicted values by approximately 2.11 units in average.
</p>
<p>
This corresponds to an error rate of 2.11/mean(train.data$sales) = 2.11/16.77 = 13%, which is low.
</p>
</div>
<ol start="2" style="list-style-type: decimal">
<li><strong>R-squared and Adjusted R-squared</strong>:</li>
</ol>
<p>The R-squared (R2) ranges from 0 to 1 and represents the proportion of variation in the outcome variable that can be explained by the model predictor variables.</p>
<p>For a simple linear regression, R2 is the square of the Pearson correlation coefficient between the outcome and the predictor variables. In multiple linear regression, the R2 represents the correlation coefficient between the observed outcome values and the predicted values.</p>
<p>The R2 measures, how well the model fits the data. The higher the R2, the better the model. However, a problem with the R2, is that, it will always increase when more variables are added to the model, even if those variables are only weakly associated with the outcome <span class="citation">(James et al. 2014)</span>. A solution is to adjust the R2 by taking into account the number of predictor variables.</p>
<p>The adjustment in the “Adjusted R Square” value in the summary output is a correction for the number of x variables included in the predictive model.</p>
<p>So, you should mainly consider the adjusted R-squared, which is a penalized R2 for a higher number of predictors.</p>
<ul>
<li>An (adjusted) R2 that is close to 1 indicates that a large proportion of the variability in the outcome has been explained by the regression model.</li>
<li>A number near 0 indicates that the regression model did not explain much of the variability in the outcome.</li>
</ul>
<div class="success">
<p>
In our example, the adjusted R2 is 0.88, which is good.
</p>
</div>
<ol start="3" style="list-style-type: decimal">
<li><strong>F-Statistic</strong>: </li>
</ol>
<p>Recall that, the F-statistic gives the overall significance of the model. It assess whether at least one predictor variable has a non-zero coefficient.</p>
<p>In a simple linear regression, this test is not really interesting since it just duplicates the information given by the t-test, available in the coefficient table.</p>
<p>The F-statistic becomes more important once we start using multiple predictors as in multiple linear regression.</p>
<div class="success">
<p>
A large F-statistic will corresponds to a statistically significant p-value (p < 0.05). In our example, the F-statistic equal 644 producing a p-value of 1.46e-42, which is highly significant.
</p>
</div>
</div>
</div>
<div id="making-predictions" class="section level2">
<h2>Making predictions</h2>
<p>We’ll make predictions using the test data in order to evaluate the performance of our regression model.</p>
<p>The procedure is as follow:</p>
<ol style="list-style-type: decimal">
<li>Predict the sales values based on new advertising budgets in the test data</li>
<li>Assess the model performance by computing:
<ul>
<li>The prediction error RMSE (Root Mean Squared Error), representing the average difference between the observed known outcome values in the test data and the predicted outcome values by the model. The lower the RMSE, the better the model.</li>
<li>The R-square (R2), representing the correlation between the observed outcome values and the predicted outcome values. The higher the R2, the better the model.</li>
</ul></li>
</ol>
<pre class="r"><code># Make predictions
predictions <- model %>% predict(test.data)
# Model performance
# (a) Compute the prediction error, RMSE
RMSE(predictions, test.data$sales)</code></pre>
<pre><code>## [1] 1.58</code></pre>
<pre class="r"><code># (b) Compute R-square
R2(predictions, test.data$sales)</code></pre>
<pre><code>## [1] 0.938</code></pre>
<div class="success">
<p>
From the output above, the R2 is 0.93, meaning that the observed and the predicted outcome values are highly correlated, which is very good.
</p>
<p>
The prediction error RMSE is 1.58, representing an error rate of 1.58/mean(test.data$sales) = 1.58/17 = 9.2%, which is good.
</p>
</div>
</div>
<div id="discussion" class="section level2">
<h2>Discussion</h2>
<p>This chapter describes the basics of linear regression and provides practical examples in R for computing simple and multiple linear regression models. We also described how to assess the performance of the model for predictions.</p>
<p>Note that, linear regression assumes a linear relationship between the outcome and the predictor variables. This can be easily checked by creating a scatter plot of the outcome variable vs the predictor variable.</p>
<p>For example, the following R code displays sales units versus youtube advertising budget. We’ll also add a smoothed line:</p>
<pre class="r"><code>ggplot(marketing, aes(x = youtube, y = sales)) +
  geom_point() +
  stat_smooth()</code></pre>
<p><img src="https://www.sthda.com/english/sthda-upload/figures/machine-learning-essentials/006-linear-regression-scatter-plot-1.png" width="384" /></p>
<p>The graph above shows a linearly increasing relationship between the <code>sales</code> and the <code>youtube</code> variables, which is a good thing.</p>
<p>In addition to the linearity assumptions, the linear regression method makes many other assumptions about your data (see Chapter @ref(regression-assumptions-and-diagnostics)). You should make sure that these assumptions hold true for your data.</p>
<p>Potential problems, include: a) the presence of influential observations in the data (Chapter @ref(regression-assumptions-and-diagnostics)), non-linearity between the outcome and some predictor variables (@ref(polynomial-and-spline-regression)) and the presence of strong correlation between predictor variables (Chapter @ref(multicollinearity)).</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:47:00 +0100</pubDate>
			
		</item>
		
		<item>
			<title><![CDATA[Interaction Effect in Multiple Regression: Essentials ]]></title>
			<link>https://www.sthda.com/english/articles/40-regression-analysis/164-interaction-effect-in-multiple-regression-essentials/</link>
			<guid>https://www.sthda.com/english/articles/40-regression-analysis/164-interaction-effect-in-multiple-regression-essentials/</guid>
			<description><![CDATA[<!-- START HTML -->

  <div id="rdoc">





<p>This chapter describes how to compute multiple linear regression with <strong>interaction effects</strong>.</p>
<p>Previously, we have described how to build a multiple linear regression model (Chapter @ref(linear-regression)) for predicting a continuous outcome variable (y) based on multiple predictor variables (x).</p>
<p>For example, to predict sales, based on advertising budgets spent on youtube and facebook, the model equation is <code>sales = b0 + b1*youtube + b2*facebook</code>, where, b0 is the intercept; b1 and b2 are the regression coefficients associated respectively with the predictor variables youtube and facebook.</p>
<p>The above equation, also known as <em>additive model</em>, investigates only the main effects of predictors. It assumes that the relationship between a given predictor variable and the outcome is independent of the other predictor variables <span class="citation">(James et al. 2014,<span class="citation">P. Bruce and Bruce (2017)</span>)</span>.</p>
<p>Considering our example, the additive model assumes that, the effect on sales of youtube advertising is independent of the effect of facebook advertising.</p>
<p>This assumption might not be true. For example, spending money on facebook advertising may increase the effectiveness of youtube advertising on sales. In marketing, this is known as a synergy effect, and in statistics it is referred to as an interaction effect <span class="citation">(James et al. 2014)</span>.</p>
<p>In this chapter, you’ll learn:</p>
<ul>
<li>the equation of multiple linear regression with interaction</li>
<li>R codes for computing the regression coefficients associated with the main effects and the interaction effects</li>
<li>how to interpret the interaction effect</li>
</ul>
<p>Contents:</p>
<div id="TOC">
<ul>
<li><a href="#equation">Equation</a></li>
<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="#computation">Computation</a><ul>
<li><a href="#additive-model">Additive model</a></li>
<li><a href="#interaction-effects">Interaction effects</a></li>
</ul></li>
<li><a href="#interpretation">Interpretation</a></li>
<li><a href="#comparing-the-additive-and-the-interaction-models">Comparing the additive and the interaction models</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="equation" class="section level2">
<h2>Equation</h2>
<p>The multiple linear regression equation, with interaction effects between two predictors (x1 and x2), can be written as follow:</p>
<p><code>y = b0 + b1*x1 + b2*x2 + b3*(x1*x2)</code></p>
<p>Considering our example, it becomes:</p>
<p><code>sales = b0 + b1*youtube + b2*facebook + b3*(youtube*facebook)</code></p>
<p>This can be also written as:</p>
<p><code>sales = b0 + (b1 + b3*facebook)*youtube + b2*facebook</code></p>
<p>or as:</p>
<p><code>sales = b0 + b1*youtube + (b2 +b3*youtube)*facebook</code></p>
<div class="success">
<p>
<code>b3</code> can be interpreted as the increase in the effectiveness of youtube advertising for a one unit increase in facebook advertising (or vice-versa).
</p>
</div>
<p>In the following sections, you will learn how to compute the regression coefficients in R.</p>
</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>marketing</code> data set, introduced in the Chapter @ref(regression-analysis), for predicting sales units on the basis of the amount of money spent in the three advertising medias (youtube, facebook and newspaper)</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).</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>
<pre class="r"><code># Split the data into training and test set
set.seed(123)
training.samples <- marketing$sales %>%
  createDataPartition(p = 0.8, list = FALSE)
train.data  <- marketing[training.samples, ]
test.data <- marketing[-training.samples, ]</code></pre>
</div>
<div id="computation" class="section level2">
<h2>Computation</h2>
<div id="additive-model" class="section level3">
<h3>Additive model</h3>
<p>The standard linear regression model can be computed as follow:</p>
<pre class="r"><code># Build the model
model1 <- lm(sales ~ youtube + facebook, data = train.data)
# Summarize the model
summary(model1)</code></pre>
<pre><code>## 
## Call:
## lm(formula = sales ~ youtube + facebook, data = train.data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -10.481  -1.104   0.349   1.423   3.486 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  3.43446    0.40877     8.4  2.3e-14 ***
## youtube      0.04558    0.00159    28.7  < 2e-16 ***
## facebook     0.18788    0.00920    20.4  < 2e-16 ***
## ---
## Signif. codes:  0 &amp;#39;***&amp;#39; 0.001 &amp;#39;**&amp;#39; 0.01 &amp;#39;*&amp;#39; 0.05 &amp;#39;.&amp;#39; 0.1 &amp;#39; &amp;#39; 1
## 
## Residual standard error: 2.11 on 159 degrees of freedom
## Multiple R-squared:  0.89,   Adjusted R-squared:  0.889 
## F-statistic:  644 on 2 and 159 DF,  p-value: <2e-16</code></pre>
<pre class="r"><code># Make predictions
predictions <- model1 %>% predict(test.data)
# Model performance
# (a) Prediction error, RMSE
RMSE(predictions, test.data$sales)</code></pre>
<pre><code>## [1] 1.58</code></pre>
<pre class="r"><code># (b) R-square
R2(predictions, test.data$sales)</code></pre>
<pre><code>## [1] 0.938</code></pre>
</div>
<div id="interaction-effects" class="section level3">
<h3>Interaction effects</h3>
<p>In R, you include interactions between variables using the <code>*</code> operator:</p>
<pre class="r"><code># Build the model
# Use this: 
model2 <- lm(sales ~ youtube + facebook + youtube:facebook,
             data = marketing)
# Or simply, use this: 
model2 <- lm(sales ~ youtube*facebook, data = train.data)

# Summarize the model
summary(model2)</code></pre>
<pre><code>## 
## Call:
## lm(formula = sales ~ youtube * facebook, data = train.data)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -7.438 -0.482  0.231  0.748  1.860 
## 
## Coefficients:
##                  Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      7.90e+00   3.28e-01   24.06   <2e-16 ***
## youtube          1.95e-02   1.64e-03   11.90   <2e-16 ***
## facebook         2.96e-02   9.83e-03    3.01    0.003 ** 
## youtube:facebook 9.12e-04   4.84e-05   18.86   <2e-16 ***
## ---
## Signif. codes:  0 &amp;#39;***&amp;#39; 0.001 &amp;#39;**&amp;#39; 0.01 &amp;#39;*&amp;#39; 0.05 &amp;#39;.&amp;#39; 0.1 &amp;#39; &amp;#39; 1
## 
## Residual standard error: 1.18 on 158 degrees of freedom
## Multiple R-squared:  0.966,  Adjusted R-squared:  0.966 
## F-statistic: 1.51e+03 on 3 and 158 DF,  p-value: <2e-16</code></pre>
<pre class="r"><code># Make predictions
predictions <- model2 %>% predict(test.data)
# Model performance
# (a) Prediction error, RMSE
RMSE(predictions, test.data$sales)</code></pre>
<pre><code>## [1] 0.963</code></pre>
<pre class="r"><code># (b) R-square
R2(predictions, test.data$sales)</code></pre>
<pre><code>## [1] 0.982</code></pre>
</div>
</div>
<div id="interpretation" class="section level2">
<h2>Interpretation</h2>
<p>It can be seen that all the coefficients, including the interaction term coefficient, are statistically significant, suggesting that there is an interaction relationship between the two predictor variables (youtube and facebook advertising).</p>
<p>Our model equation looks like this:</p>
<p><code>sales = 7.89 + 0.019*youtube + 0.029*facebook + 0.0009*youtube*facebook</code></p>
<p>We can interpret this as an increase in youtube advertising of 1000 dollars is associated with increased sales of <code>(b1 + b3*facebook)*1000 = 19 + 0.9*facebook units</code>. And an increase in facebook advertising of 1000 dollars will be associated with an increase in sales of <code>(b2 + b3*youtube)*1000 = 28 + 0.9*youtube units</code>.</p>
<div class="warning">
<p>
Note that, sometimes, it is the case that the interaction term is significant but not the main effects. The hierarchical principle states that, if we include an interaction in a model, we should also include the main effects, even if the p-values associated with their coefficients are not significant <span class="citation"><span class="citation">(James et al. 2014)</span></span>.
</p>
</div>
</div>
<div id="comparing-the-additive-and-the-interaction-models" class="section level2">
<h2>Comparing the additive and the interaction models</h2>
<p>The prediction error RMSE of the interaction model is 0.963, which is lower than the prediction error of the additive model (1.58).</p>
<p>Additionally, the R-square (R2) value of the interaction model is 98% compared to only 93% for the additive model.</p>
<p>These results suggest that the model with the interaction term is better than the model that contains only main effects. So, for this specific data, we should go for the model with the interaction model.</p>
</div>
<div id="discussion" class="section level2">
<h2>Discussion</h2>
<p>This chapter describes how to compute multiple linear regression with interaction effects. Interaction terms should be included in the model if they are significantly.</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:44:00 +0100</pubDate>
			
		</item>
		
		<item>
			<title><![CDATA[Regression with Categorical Variables: Dummy Coding Essentials in R]]></title>
			<link>https://www.sthda.com/english/articles/40-regression-analysis/163-regression-with-categorical-variables-dummy-coding-essentials-in-r/</link>
			<guid>https://www.sthda.com/english/articles/40-regression-analysis/163-regression-with-categorical-variables-dummy-coding-essentials-in-r/</guid>
			<description><![CDATA[<!-- START HTML -->

  <div id="rdoc">





<p>This chapter describes how to compute <strong>regression with categorical variables</strong>.</p>
<p><strong>Categorical variables</strong> (also known as <em>factor</em> or <em>qualitative variables</em>) are variables that classify observations into groups. They have a limited number of different values, called levels. For example the gender of individuals are a categorical variable that can take two levels: Male or Female.</p>
<p>Regression analysis requires numerical variables. So, when a researcher wishes to include a categorical variable in a regression model, supplementary steps are required to make the results interpretable.</p>
<p>In these steps, the categorical variables are recoded into a set of separate binary variables. This recoding is called “dummy coding” and leads to the creation of a table called <em>contrast matrix</em>. This is done automatically by statistical software, such as R.</p>
<p>Here, you’ll learn how to build and interpret a linear regression model with categorical predictor variables. We’ll also provide practical examples in 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="#example-of-data-set">Example of data set</a></li>
<li><a href="#categorical-variables-with-two-levels">Categorical variables with two levels</a></li>
<li><a href="#categorical-variables-with-more-than-two-levels">Categorical variables with more than two levels</a></li>
<li><a href="#discussion">Discussion</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>
</ul>
<pre class="r"><code>library(tidyverse)</code></pre>
</div>
<div id="example-of-data-set" class="section level2">
<h2>Example of data set</h2>
<p>We’ll use the <code>Salaries</code> data set [<code>car</code> package], which contains 2008-09 nine-month academic salary for Assistant Professors, Associate Professors and Professors in a college in the U.S.</p>
<p>The data were collected as part of the on-going effort of the college’s administration to monitor salary differences between male and female faculty members.</p>
<pre class="r"><code># Load the data
data("Salaries", package = "car")
# Inspect the data
sample_n(Salaries, 3)</code></pre>
<pre><code>##     rank discipline yrs.since.phd yrs.service    sex salary
## 115 Prof          A            12           0 Female 105000
## 313 Prof          A            29          19   Male  94350
## 162 Prof          B            26          19   Male 176500</code></pre>
</div>
<div id="categorical-variables-with-two-levels" class="section level2">
<h2>Categorical variables with two levels</h2>
<p>Recall that, the regression equation, for predicting an outcome variable (y) on the basis of a predictor variable (x), can be simply written as <code>y = b0 + b1*x</code>. <code>b0</code> and `b1 are the regression beta coefficients, representing the intercept and the slope, respectively.</p>
<p>Suppose that, we wish to investigate differences in salaries between males and females.</p>
<p>Based on the gender variable, we can create a new dummy variable that takes the value:</p>
<ul>
<li><code>1</code> if a person is male</li>
<li><code>0</code> if a person is female</li>
</ul>
<p>and use this variable as a predictor in the regression equation, leading to the following the model:</p>
<ul>
<li><code>b0 + b1</code> if person is male</li>
<li><code>bo</code> if person is female</li>
</ul>
<p>The coefficients can be interpreted as follow:</p>
<ol style="list-style-type: decimal">
<li><code>b0</code> is the average salary among females,</li>
<li><code>b0 + b1</code> is the average salary among males,</li>
<li>and <code>b1</code> is the average difference in salary between males and females.</li>
</ol>
<p>For simple demonstration purpose, the following example models the salary difference between males and females by computing a simple linear regression model on the <code>Salaries</code> data set [<code>car</code> package]. R creates dummy variables automatically:</p>
<pre class="r"><code># Compute the model
model <- lm(salary ~ sex, data = Salaries)
summary(model)$coef</code></pre>
<pre><code>##             Estimate Std. Error t value Pr(>|t|)
## (Intercept)   101002       4809   21.00 2.68e-66
## sexMale        14088       5065    2.78 5.67e-03</code></pre>
<p>From the output above, the average salary for female is estimated to be 101002, whereas males are estimated a total of 101002 + 14088 = 115090. The p-value for the dummy variable <code>sexMale</code> is very significant, suggesting that there is a statistical evidence of a difference in average salary between the genders.</p>
<p>The <code>contrasts()</code> function returns the coding that R have used to create the dummy variables:</p>
<pre class="r"><code>contrasts(Salaries$sex)</code></pre>
<pre><code>##        Male
## Female    0
## Male      1</code></pre>
<p>R has created a sexMale dummy variable that takes on a value of 1 if the sex is Male, and 0 otherwise. The decision to code males as 1 and females as 0 (baseline) is arbitrary, and has no effect on the regression computation, but does alter the interpretation of the coefficients.</p>
<p>You can use the function <code>relevel()</code> to set the baseline category to males as follow:</p>
<pre class="r"><code>Salaries <- Salaries %>%
  mutate(sex = relevel(sex, ref = "Male"))</code></pre>
<p>The output of the regression fit becomes:</p>
<pre class="r"><code>model <- lm(salary ~ sex, data = Salaries)
summary(model)$coef</code></pre>
<pre><code>##             Estimate Std. Error t value  Pr(>|t|)
## (Intercept)   115090       1587   72.50 2.46e-230
## sexFemale     -14088       5065   -2.78  5.67e-03</code></pre>
<p>The fact that the coefficient for <code>sexFemale</code> in the regression output is negative indicates that being a Female is associated with decrease in salary (relative to Males).</p>
<p>Now the estimates for <code>bo</code> and <code>b1</code> are 115090 and -14088, respectively, leading once again to a prediction of average salary of 115090 for males and a prediction of 115090 - 14088 = 101002 for females.</p>
<p>Alternatively, instead of a 0/1 coding scheme, we could create a dummy variable -1 (male) / 1 (female) . This results in the model:</p>
<ul>
<li><code>b0 - b1</code> if person is male</li>
<li><code>b0 + b1</code> if person is female</li>
</ul>
<p>So, if the categorical variable is coded as -1 and 1, then if the regression coefficient is positive, it is subtracted from the group coded as -1 and added to the group coded as 1. If the regression coefficient is negative, then addition and subtraction is reversed.</p>
</div>
<div id="categorical-variables-with-more-than-two-levels" class="section level2">
<h2>Categorical variables with more than two levels</h2>
<p>Generally, a categorical variable with n levels will be transformed into n-1 variables each with two levels. These n-1 new variables contain the same information than the single variable. This recoding creates a table called <strong>contrast matrix</strong>.</p>
<p>For example <code>rank</code> in the <code>Salaries</code> data has three levels: “AsstProf”, “AssocProf” and “Prof”. This variable could be dummy coded into two variables, one called AssocProf and one Prof:</p>
<ul>
<li>If rank = AssocProf, then the column AssocProf would be coded with a 1 and Prof with a 0.</li>
<li>If rank = Prof, then the column AssocProf would be coded with a 0 and Prof would be coded with a 1.</li>
<li>If rank = AsstProf, then both columns “AssocProf” and “Prof” would be coded with a 0.</li>
</ul>
<p>This dummy coding is automatically performed by R. For demonstration purpose, you can use the function <code>model.matrix()</code> to create a contrast matrix for a factor variable:</p>
<pre class="r"><code>res <- model.matrix(~rank, data = Salaries)
head(res[, -1])</code></pre>
<pre><code>##   rankAssocProf rankProf
## 1             0        1
## 2             0        1
## 3             0        0
## 4             0        1
## 5             0        1
## 6             1        0</code></pre>
<p>When building linear model, there are different ways to encode categorical variables, known as contrast coding systems. The default option in R is to use the first level of the factor as a reference and interpret the remaining levels relative to this level.</p>
<p>Note that, ANOVA (analyse of variance) is just a special case of linear model where the predictors are categorical variables. And, because R understands the fact that ANOVA and regression are both examples of linear models, it lets you extract the classic ANOVA table from your regression model using the R base <code>anova()</code> function or the <code>Anova()</code> function [in <code>car</code> package]. We generally recommend the <code>Anova()</code> function because it automatically takes care of unbalanced designs.</p>
<p>The results of predicting salary from using a multiple regression procedure are presented below.</p>
<pre class="r"><code>library(car)
model2 <- lm(salary ~ yrs.service + rank + discipline + sex,
             data = Salaries)
Anova(model2)</code></pre>
<pre><code>## Anova Table (Type II tests)
## 
## Response: salary
##               Sum Sq  Df F value  Pr(>F)    
## yrs.service 3.24e+08   1    0.63    0.43    
## rank        1.03e+11   2  100.26 < 2e-16 ***
## discipline  1.74e+10   1   33.86 1.2e-08 ***
## sex         7.77e+08   1    1.51    0.22    
## Residuals   2.01e+11 391                    
## ---
## Signif. codes:  0 &amp;#39;***&amp;#39; 0.001 &amp;#39;**&amp;#39; 0.01 &amp;#39;*&amp;#39; 0.05 &amp;#39;.&amp;#39; 0.1 &amp;#39; &amp;#39; 1</code></pre>
<p>Taking other variables (yrs.service, rank and discipline) into account, it can be seen that the categorical variable sex is no longer significantly associated with the variation in salary between individuals. Significant variables are rank and discipline.</p>
<p>If you want to interpret the contrasts of the categorical variable, type this:</p>
<pre class="r"><code>summary(model2)</code></pre>
<pre><code>## 
## Call:
## lm(formula = salary ~ yrs.service + rank + discipline + sex, 
##     data = Salaries)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -64202 -14255  -1533  10571  99163 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)    73122.9     3245.3   22.53  < 2e-16 ***
## yrs.service      -88.8      111.6   -0.80  0.42696    
## rankAssocProf  14560.4     4098.3    3.55  0.00043 ***
## rankProf       49159.6     3834.5   12.82  < 2e-16 ***
## disciplineB    13473.4     2315.5    5.82  1.2e-08 ***
## sexFemale      -4771.2     3878.0   -1.23  0.21931    
## ---
## Signif. codes:  0 &amp;#39;***&amp;#39; 0.001 &amp;#39;**&amp;#39; 0.01 &amp;#39;*&amp;#39; 0.05 &amp;#39;.&amp;#39; 0.1 &amp;#39; &amp;#39; 1
## 
## Residual standard error: 22700 on 391 degrees of freedom
## Multiple R-squared:  0.448,  Adjusted R-squared:  0.441 
## F-statistic: 63.4 on 5 and 391 DF,  p-value: <2e-16</code></pre>
<p>For example, it can be seen that being from discipline B (applied departments) is significantly associated with an average increase of 13473.38 in salary compared to discipline A (theoretical departments).</p>
</div>
<div id="discussion" class="section level2">
<h2>Discussion</h2>
<p>In this chapter we described how categorical variables are included in linear regression model. As regression requires numerical inputs, categorical variables need to be recoded into a set of binary variables.</p>
<p>We provide practical examples for the situations where you have categorical variables containing two or more levels.</p>
<p>Note that, for categorical variables with a large number of levels it might be useful to group together some of the levels.</p>
<p>Some categorical variables have levels that are ordered. They can be converted to numerical values and used as is. For example, if the professor grades (“AsstProf”, “AssocProf” and “Prof”) have a special meaning, you can convert them into numerical values, ordered from low to high, corresponding to higher-grade professors.</p>
</div>


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


<!-- END HTML -->]]></description>
			<pubDate>Sun, 11 Mar 2018 12:38:00 +0100</pubDate>
			
		</item>
		
		<item>
			<title><![CDATA[Nonlinear Regression Essentials in R: Polynomial and Spline Regression Models]]></title>
			<link>https://www.sthda.com/english/articles/40-regression-analysis/162-nonlinear-regression-essentials-in-r-polynomial-and-spline-regression-models/</link>
			<guid>https://www.sthda.com/english/articles/40-regression-analysis/162-nonlinear-regression-essentials-in-r-polynomial-and-spline-regression-models/</guid>
			<description><![CDATA[<!-- START HTML -->


  <div id="rdoc">





<p>In some cases, the true relationship between the outcome and a predictor variable might not be linear.</p>
<p>There are different solutions extending the linear regression model (Chapter @ref(linear-regression)) for capturing these nonlinear effects, including:</p>
<ul>
<li><p><strong>Polynomial regression</strong>. This is the simple approach to model non-linear relationships. It add polynomial terms or quadratic terms (square, cubes, etc) to a regression.</p></li>
<li><p><strong>Spline regression</strong>. Fits a smooth curve with a series of polynomial segments. The values delimiting the spline segments are called <strong>Knots</strong>.</p></li>
<li><p><strong>Generalized additive models</strong> (GAM). Fits spline models with automated selection of knots.</p></li>
</ul>
<p>In this chapter, you’ll learn how to compute non-linear regression models and how to compare the different models in order to choose the one that fits the best your data.</p>
<p>The RMSE and the R2 metrics, will be used to compare the different models (see Chapter @ref(linear regression)).</p>
<p>Recall that, the RMSE represents the model prediction error, that is the average difference the observed outcome values and the predicted outcome values. The R2 represents the squared correlation between the observed and predicted outcome values. The best model is the model with the lowest RMSE and the highest R2.</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="#linear-regression-linear-reg">Linear regression {linear-reg}</a></li>
<li><a href="#polynomial-regression">Polynomial regression</a></li>
<li><a href="#log-transformation">Log transformation</a></li>
<li><a href="#spline-regression">Spline regression</a></li>
<li><a href="#generalized-additive-models">Generalized additive models</a></li>
<li><a href="#comparing-the-models">Comparing the models</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)
theme_set(theme_classic())</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 the predictor variable <code>lstat</code> (percentage of lower status of the population).</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>
<p>First, visualize the scatter plot of the <code>medv</code> vs <code>lstat</code> variables as follow:</p>
<pre class="r"><code>ggplot(train.data, aes(lstat, medv) ) +
  geom_point() +
  stat_smooth()</code></pre>
<p><img src="https://www.sthda.com/english/sthda-upload/figures/machine-learning-essentials/009-polynomial-and-spline-regression-scatter-plot-1.png" width="384" /></p>
<div class="success">
<p>
The above scatter plot suggests a non-linear relationship between the two variables
</p>
</div>
<p>In the following sections, we start by computing linear and non-linear regression models. Next, we’ll compare the different models in order to choose the best one for our data.</p>
</div>
<div id="linear-regression-linear-reg" class="section level2">
<h2>Linear regression {linear-reg}</h2>
<p>The standard linear regression model equation can be written as <code>medv = b0 + b1*lstat</code>.</p>
<p>Compute linear regression model:</p>
<pre class="r"><code># Build the model
model <- lm(medv ~ lstat, data = train.data)
# Make predictions
predictions <- model %>% 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 6.07 0.535</code></pre>
<p>Visualize the data:</p>
<pre class="r"><code>ggplot(train.data, aes(lstat, medv) ) +
  geom_point() +
  stat_smooth(method = lm, formula = y ~ x)</code></pre>
<p><img src="https://www.sthda.com/english/sthda-upload/figures/machine-learning-essentials/009-polynomial-and-spline-regression-linear-regression-1.png" width="384" /></p>
</div>
<div id="polynomial-regression" class="section level2">
<h2>Polynomial regression</h2>
<p>The polynomial regression adds polynomial or quadratic terms to the regression equation as follow:</p>
<p><span class="math display">\[medv = b0 + b1*lstat + b2*lstat^2\]</span></p>
<p>In R, to create a predictor x^2 you should use the function <code>I()</code>, as follow: <code>I(x^2)</code>. This raise x to the power 2.</p>
<p>The polynomial regression can be computed in R as follow:</p>
<pre class="r"><code>lm(medv ~ lstat + I(lstat^2), data = train.data)</code></pre>
<p>An alternative simple solution is to use this:</p>
<pre class="r"><code>lm(medv ~ poly(lstat, 2, raw = TRUE), data = train.data)</code></pre>
<pre><code>## 
## Call:
## lm(formula = medv ~ poly(lstat, 2, raw = TRUE), data = train.data)
## 
## Coefficients:
##                 (Intercept)  poly(lstat, 2, raw = TRUE)1  
##                      43.351                       -2.340  
## poly(lstat, 2, raw = TRUE)2  
##                       0.043</code></pre>
<p>The output contains two coefficients associated with lstat : one for the linear term (lstat^1) and one for the quadratic term (lstat^2).</p>
<p>The following example computes a sixfth-order polynomial fit:</p>
<pre class="r"><code>lm(medv ~ poly(lstat, 6, raw = TRUE), data = train.data) %>%
  summary()</code></pre>
<pre><code>## 
## Call:
## lm(formula = medv ~ poly(lstat, 6, raw = TRUE), data = train.data)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -14.23  -3.24  -0.74   2.02  26.50 
## 
## Coefficients:
##                              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)                  7.14e+01   6.00e+00   11.90  < 2e-16 ***
## poly(lstat, 6, raw = TRUE)1 -1.45e+01   3.22e+00   -4.48  9.6e-06 ***
## poly(lstat, 6, raw = TRUE)2  1.87e+00   6.26e-01    2.98    0.003 ** 
## poly(lstat, 6, raw = TRUE)3 -1.32e-01   5.73e-02   -2.30    0.022 *  
## poly(lstat, 6, raw = TRUE)4  4.98e-03   2.66e-03    1.87    0.062 .  
## poly(lstat, 6, raw = TRUE)5 -9.56e-05   6.03e-05   -1.58    0.114    
## poly(lstat, 6, raw = TRUE)6  7.29e-07   5.30e-07    1.38    0.170    
## ---
## Signif. codes:  0 &amp;#39;***&amp;#39; 0.001 &amp;#39;**&amp;#39; 0.01 &amp;#39;*&amp;#39; 0.05 &amp;#39;.&amp;#39; 0.1 &amp;#39; &amp;#39; 1
## 
## Residual standard error: 5.28 on 400 degrees of freedom
## Multiple R-squared:  0.684,  Adjusted R-squared:  0.679 
## F-statistic:  144 on 6 and 400 DF,  p-value: <2e-16</code></pre>
<p>From the output above, it can be seen that polynomial terms beyond the fith order are not significant. So, just create a fith polynomial regression model as follow:</p>
<pre class="r"><code># Build the model
model <- lm(medv ~ poly(lstat, 5, raw = TRUE), data = train.data)
# Make predictions
predictions <- model %>% 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.96 0.689</code></pre>
<p>Visualize the fith polynomial regression line as follow:</p>
<pre class="r"><code>ggplot(train.data, aes(lstat, medv) ) +
  geom_point() +
  stat_smooth(method = lm, formula = y ~ poly(x, 5, raw = TRUE))</code></pre>
<p><img src="https://www.sthda.com/english/sthda-upload/figures/machine-learning-essentials/009-polynomial-and-spline-regression-polynomial-regression-1.png" width="384" /></p>
</div>
<div id="log-transformation" class="section level2">
<h2>Log transformation</h2>
<p>When you have a non-linear relationship, you can also try a logarithm transformation of the predictor variables:</p>
<pre class="r"><code># Build the model
model <- lm(medv ~ log(lstat), data = train.data)
# Make predictions
predictions <- model %>% 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.24 0.657</code></pre>
<p>Visualize the data:</p>
<pre class="r"><code>ggplot(train.data, aes(lstat, medv) ) +
  geom_point() +
  stat_smooth(method = lm, formula = y ~ log(x))</code></pre>
<p><img src="https://www.sthda.com/english/sthda-upload/figures/machine-learning-essentials/009-polynomial-and-spline-regression-log-transformation-1.png" width="384" /></p>
</div>
<div id="spline-regression" class="section level2">
<h2>Spline regression</h2>
<p>Polynomial regression only captures a certain amount of curvature in a nonlinear relationship. An alternative, and often superior, approach to modeling nonlinear relationships is to use splines <span class="citation">(P. Bruce and Bruce 2017)</span>.</p>
<p>Splines provide a way to smoothly interpolate between fixed points, called knots. Polynomial regression is computed between knots. In other words, splines are series of polynomial segments strung together, joining at knots <span class="citation">(P. Bruce and Bruce 2017)</span>.</p>
<p>The R package <code>splines</code> includes the function <code>bs</code> for creating a b-spline term in a regression model.</p>
<p>You need to specify two parameters: the degree of the polynomial and the location of the knots. In our example, we’ll place the knots at the lower quartile, the median quartile, and the upper quartile:</p>
<pre class="r"><code>knots <- quantile(train.data$lstat, p = c(0.25, 0.5, 0.75))</code></pre>
<p>We’ll create a model using a cubic spline (degree = 3):</p>
<pre class="r"><code>library(splines)
# Build the model
knots <- quantile(train.data$lstat, p = c(0.25, 0.5, 0.75))
model <- lm (medv ~ bs(lstat, knots = knots), data = train.data)
# Make predictions
predictions <- model %>% 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.97 0.688</code></pre>
<p>Note that, the coefficients for a spline term are not interpretable.</p>
<p>Visualize the cubic spline as follow:</p>
<pre class="r"><code>ggplot(train.data, aes(lstat, medv) ) +
  geom_point() +
  stat_smooth(method = lm, formula = y ~ splines::bs(x, df = 3))</code></pre>
<p><img src="https://www.sthda.com/english/sthda-upload/figures/machine-learning-essentials/009-polynomial-and-spline-regression-cubic-spline-1.png" width="384" /></p>
</div>
<div id="generalized-additive-models" class="section level2">
<h2>Generalized additive models</h2>
<p>Once you have detected a non-linear relationship in your data, the polynomial terms may not be flexible enough to capture the relationship, and spline terms require specifying the knots.</p>
<p>Generalized additive models, or GAM, are a technique to automatically fit a spline regression. This can be done using the <code>mgcv</code> R package:</p>
<pre class="r"><code>library(mgcv)
# Build the model
model <- gam(medv ~ s(lstat), data = train.data)
# Make predictions
predictions <- model %>% 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.02 0.684</code></pre>
<p>The term <code>s(lstat)</code> tells the <code>gam()</code> function to find the “best” knots for a spline term.</p>
<p>Visualize the data:</p>
<pre class="r"><code>ggplot(train.data, aes(lstat, medv) ) +
  geom_point() +
  stat_smooth(method = gam, formula = y ~ s(x))</code></pre>
<p><img src="https://www.sthda.com/english/sthda-upload/figures/machine-learning-essentials/009-polynomial-and-spline-regression-generalized-additive-model-1.png" width="384" /></p>
</div>
<div id="comparing-the-models" class="section level2">
<h2>Comparing the models</h2>
<p>From analyzing the RMSE and the R2 metrics of the different models, it can be seen that the polynomial regression, the spline regression and the generalized additive models outperform the linear regression model and the log transformation approaches.</p>
</div>
<div id="discussion" class="section level2">
<h2>Discussion</h2>
<p>This chapter describes how to compute non-linear regression models using R.</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>
</div>


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

 
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
  (function () {
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src  = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
    document.getElementsByTagName("head")[0].appendChild(script);
  })();
</script>


<!-- END HTML -->]]></description>
			<pubDate>Sun, 11 Mar 2018 12:33:00 +0100</pubDate>
			
		</item>
		
		<item>
			<title><![CDATA[Simple Linear Regression in R]]></title>
			<link>https://www.sthda.com/english/articles/40-regression-analysis/167-simple-linear-regression-in-r/</link>
			<guid>https://www.sthda.com/english/articles/40-regression-analysis/167-simple-linear-regression-in-r/</guid>
			<description><![CDATA[<!-- START HTML -->

  <div id="rdoc">





<p>The <strong>simple linear regression</strong> is used to predict a quantitative outcome <code>y</code> on the basis of one single predictor variable <code>x</code>. The goal is to build a mathematical model (or formula) that defines y as a function of the x variable.</p>
<p>Once, we built a statistically significant model, it’s possible to use it for predicting future outcome on the basis of new x values.</p>
<p>Consider that, we want to evaluate the impact of advertising budgets of three medias (youtube, facebook and newspaper) on future sales. This example of problem can be modeled with linear regression.</p>
<p>Contents:</p>
<div id="TOC">
<ul>
<li><a href="#formula-and-basics">Formula and basics</a></li>
<li><a href="#loading-required-r-packages">Loading required R packages</a></li>
<li><a href="#examples-of-data-and-problem">Examples of data and problem</a></li>
<li><a href="#visualization">Visualization</a></li>
<li><a href="#computation">Computation</a></li>
<li><a href="#interpretation">Interpretation</a></li>
<li><a href="#regression-line">Regression line</a></li>
<li><a href="#model-assessment">Model assessment</a><ul>
<li><a href="#model-summary">Model summary</a></li>
<li><a href="#coefficients-significance">Coefficients significance</a></li>
<li><a href="#model-accuracy">Model accuracy</a></li>
<li><a href="#summary">Summary</a></li>
</ul></li>
<li><a href="#read-more">Read more</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="formula-and-basics" class="section level2">
<h2>Formula and basics</h2>
<p>The mathematical formula of the linear regression can be written as <code>y = b0 + b1*x + e</code>, where:</p>
<ul>
<li><code>b0</code> and <code>b1</code> are known as the regression <em>beta coefficients</em> or <em>parameters</em>:
<ul>
<li><code>b0</code> is the <em>intercept</em> of the regression line; that is the predicted value when <code>x = 0</code>.</li>
<li><code>b1</code> is the <em>slope</em> of the regression line.</li>
</ul></li>
<li><code>e</code> is the <em>error term</em> (also known as the <em>residual errors</em>), the part of y that can be explained by the regression model</li>
</ul>
<p>The figure below illustrates the linear regression model, where:</p>
<ul>
<li>the best-fit regression line is in blue</li>
<li>the intercept (b0) and the slope (b1) are shown in green</li>
<li>the error terms (e) are represented by vertical red lines</li>
</ul>
<p><img src="https://www.sthda.com/english/sthda-upload/images/machine-learning-essentials/linear-regression.png" alt="Linear regression" /></p>
<p>From the scatter plot above, it can be seen that not all the data points fall exactly on the fitted regression line. Some of the points are above the blue curve and some are below it; overall, the residual errors (e) have approximately mean zero.</p>
<p>The sum of the squares of the residual errors are called the <strong>Residual Sum of Squares</strong> or <strong>RSS</strong>.</p>
<p>The average variation of points around the fitted regression line is called the <strong>Residual Standard Error</strong> (<strong>RSE</strong>). This is one the metrics used to evaluate the overall quality of the fitted regression model. The lower the RSE, the better it is.</p>
<p>Since the mean error term is zero, the outcome variable y can be approximately estimated as follow:</p>
<p><code>y ~ b0 + b1*x</code></p>
<p>Mathematically, the beta coefficients (b0 and b1) are determined so that the RSS is as minimal as possible. This method of determining the beta coefficients is technically called <strong>least squares</strong> regression or <strong>ordinary least squares</strong> (OLS) regression.</p>
<p>Once, the beta coefficients are calculated, a t-test is performed to check whether or not these coefficients are significantly different from zero. A non-zero beta coefficients means that there is a significant relationship between the predictors (x) and the outcome variable (y).</p>
</div>
<div id="loading-required-r-packages" class="section level2">
<h2>Loading required R packages</h2>
<p>Load required packages:</p>
<ul>
<li><code>tidyverse</code> for data manipulation and visualization</li>
<li><code>ggpubr</code>: creates easily a publication ready-plot</li>
</ul>
<pre class="r"><code>library(tidyverse)
library(ggpubr)
theme_set(theme_pubr())</code></pre>
</div>
<div id="examples-of-data-and-problem" class="section level2">
<h2>Examples of data and problem</h2>
<p>We’ll use the <code>marketing</code> data set [datarium package]. It contains the impact of three advertising medias (youtube, facebook and newspaper) on sales. Data are the advertising budget in thousands of dollars along with the sales. The advertising experiment has been repeated 200 times with different budgets and the observed sales have been recorded.</p>
<p>First install the <code>datarium</code> package using <code>devtools::install_github("kassmbara/datarium")</code>, then load and inspect the <code>marketing</code> data as follow:</p>
<p>Inspect the data:</p>
<pre class="r"><code># Load the package
data("marketing", package = "datarium")
head(marketing, 4)</code></pre>
<pre><code>##   youtube facebook newspaper sales
## 1   276.1     45.4      83.0  26.5
## 2    53.4     47.2      54.1  12.5
## 3    20.6     55.1      83.2  11.2
## 4   181.8     49.6      70.2  22.2</code></pre>
<p>We want to predict future sales on the basis of advertising budget spent on youtube.</p>
</div>
<div id="visualization" class="section level2">
<h2>Visualization</h2>
<ul>
<li>Create a scatter plot displaying the sales units versus youtube advertising budget.</li>
<li>Add a smoothed line</li>
</ul>
<pre class="r"><code>ggplot(marketing, aes(x = youtube, y = sales)) +
  geom_point() +
  stat_smooth()</code></pre>
<p><img src="https://www.sthda.com/english/sthda-upload/figures/machine-learning-essentials/066-simple-linear-regression-scatter-plot-1.png" width="384" /></p>
<p>The graph above suggests a linearly increasing relationship between the <code>sales</code> and the <code>youtube</code> variables. This is a good thing, because, one important assumption of the linear regression is that the relationship between the outcome and predictor variables is linear and additive.</p>
<p>It’s also possible to compute the correlation coefficient between the two variables using the R function <code>cor()</code>:</p>
<pre class="r"><code>cor(marketing$sales, marketing$youtube)</code></pre>
<pre><code>## [1] 0.782</code></pre>
<p>The correlation coefficient measures the level of the association between two variables x and y. Its value ranges between -1 (perfect negative correlation: when x increases, y decreases) and +1 (perfect positive correlation: when x increases, y increases).</p>
<p>A value closer to 0 suggests a weak relationship between the variables. A low correlation (-0.2 < x < 0.2) probably suggests that much of variation of the outcome variable (y) is not explained by the predictor (x). In such case, we should probably look for better predictor variables.</p>
<p>In our example, the correlation coefficient is large enough, so we can continue by building a linear model of y as a function of x.</p>
</div>
<div id="computation" class="section level2">
<h2>Computation</h2>
<p>The simple linear regression tries to find the best line to predict sales on the basis of youtube advertising budget.</p>
<p>The linear model equation can be written as follow: <code>sales = b0 + b1 * youtube</code></p>
<p>The R function <code>lm()</code> can be used to determine the beta coefficients of the linear model:</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>The results show the intercept and the beta coefficient for the youtube variable.</p>
</div>
<div id="interpretation" class="section level2">
<h2>Interpretation</h2>
<p>From the output above:</p>
<ul>
<li><p>the estimated regression line equation can be written as follow: <code>sales = 8.44 + 0.048*youtube</code></p></li>
<li><p>the intercept (<code>b0</code>) is 8.44. It can be interpreted as the predicted sales unit for a zero youtube advertising budget. Recall that, we are operating in units of thousand dollars. This means that, for a youtube advertising budget equal zero, we can expect a sale of 8.44 *1000 = 8440 dollars.</p></li>
<li><p>the regression beta coefficient for the variable youtube (<code>b1</code>), also known as the slope, is 0.048. This means that, for a youtube advertising budget equal to 1000 dollars, we can expect an increase of 48 units (0.048*1000) in sales. That is, <code>sales = 8.44 + 0.048*1000 = 56.44 units</code>. As we are operating in units of thousand dollars, this represents a sale of 56440 dollars.</p></li>
</ul>
</div>
<div id="regression-line" class="section level2">
<h2>Regression line</h2>
<p>To add the regression line onto the scatter plot, you can use the function <code>stat_smooth()</code> [ggplot2]. By default, the fitted line is presented with confidence interval around it. The confidence bands reflect the uncertainty about the line. If you don’t want to display it, specify the option <code>se = FALSE</code> in the function <code>stat_smooth()</code>.</p>
<pre class="r"><code>ggplot(marketing, aes(youtube, sales)) +
  geom_point() +
  stat_smooth(method = lm)</code></pre>
<p><img src="https://www.sthda.com/english/sthda-upload/figures/machine-learning-essentials/066-simple-linear-regression-scatter-plot-with-regression-line-1.png" width="384" /></p>
</div>
<div id="model-assessment" class="section level2">
<h2>Model assessment</h2>
<p>In the previous section, we built a linear model of sales as a function of youtube advertising budget: <code>sales = 8.44 + 0.048*youtube</code>.</p>
<p>Before using this formula to predict future sales, you should make sure that this model is statistically significant, that is:</p>
<ul>
<li>there is a statistically significant relationship between the predictor and the outcome variables</li>
<li>the model that we built fits very well the data in our hand.</li>
</ul>
<p>In this section, we’ll describe how to check the quality of a linear regression model.</p>
<div id="model-summary" class="section level3">
<h3>Model summary</h3>
<p>We start by displaying the statistical summary of the model using the R function <code>summary()</code>:</p>
<pre class="r"><code>summary(model)</code></pre>
<pre><code>## 
## Call:
## lm(formula = sales ~ youtube, data = marketing)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -10.06  -2.35  -0.23   2.48   8.65 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  8.43911    0.54941    15.4   <2e-16 ***
## youtube      0.04754    0.00269    17.7   <2e-16 ***
## ---
## Signif. codes:  0 &amp;#39;***&amp;#39; 0.001 &amp;#39;**&amp;#39; 0.01 &amp;#39;*&amp;#39; 0.05 &amp;#39;.&amp;#39; 0.1 &amp;#39; &amp;#39; 1
## 
## Residual standard error: 3.91 on 198 degrees of freedom
## Multiple R-squared:  0.612,  Adjusted R-squared:  0.61 
## F-statistic:  312 on 1 and 198 DF,  p-value: <2e-16</code></pre>
<p>The summary outputs shows 6 components, including:</p>
<ul>
<li><strong>Call</strong>. Shows the function call used to compute the regression model.</li>
<li><strong>Residuals</strong>. Provide a quick view of the distribution of the residuals, which by definition have a mean zero. Therefore, the median should not be far from zero, and the minimum and maximum should be roughly equal in absolute value.</li>
<li><strong>Coefficients</strong>. Shows the regression beta coefficients and their statistical significance. Predictor variables, that are significantly associated to the outcome variable, are marked by stars.</li>
<li><strong>Residual standard error</strong> (RSE), <strong>R-squared</strong> (R2) and the <strong>F-statistic</strong> are metrics that are used to check how well the model fits to our data.</li>
</ul>
</div>
<div id="coefficients-significance" class="section level3">
<h3>Coefficients significance</h3>
<p>The coefficients table, in the model statistical summary, shows:</p>
<ul>
<li>the estimates of the <strong>beta coefficients</strong></li>
<li>the <strong>standard errors</strong> (SE), which defines the accuracy of beta coefficients. For a given beta coefficient, the SE reflects how the coefficient varies under repeated sampling. It can be used to compute the confidence intervals and the t-statistic.</li>
<li>the <strong>t-statistic</strong> and the associated <strong>p-value</strong>, which defines the statistical significance of the beta coefficients.</li>
</ul>
<pre><code>##             Estimate Std. Error t value Pr(>|t|)
## (Intercept)   8.4391    0.54941    15.4 1.41e-35
## youtube       0.0475    0.00269    17.7 1.47e-42</code></pre>
<p><strong>t-statistic and p-values</strong>:</p>
<p>For a given predictor, the t-statistic (and its associated p-value) tests whether or not there is a statistically significant relationship between a given predictor and the outcome variable, that is whether or not the beta coefficient of the predictor is significantly different from zero.</p>
<p>The statistical hypotheses are as follow:</p>
<ul>
<li>Null hypothesis (H0): the coefficients are equal to zero (i.e., no relationship between x and y)</li>
<li>Alternative Hypothesis (Ha): the coefficients are not equal to zero (i.e., there is some relationship between x and y)</li>
</ul>
<p>Mathematically, for a given beta coefficient (b), the t-test is computed as <code>t = (b - 0)/SE(b)</code>, where SE(b) is the standard error of the coefficient b. The t-statistic measures the number of standard deviations that b is away from 0. Thus a large t-statistic will produce a small p-value.</p>
<p>The higher the t-statistic (and the lower the p-value), the more significant the predictor. The symbols to the right visually specifies the level of significance. The line below the table shows the definition of these symbols; one star means 0.01 < p < 0.05. The more the stars beside the variable’s p-value, the more significant the variable.</p>
<p>A statistically significant coefficient indicates that there is an association between the predictor (x) and the outcome (y) variable.</p>
<div class="success">
<p>
In our example, both the p-values for the intercept and the predictor variable are highly significant, so we can reject the null hypothesis and accept the alternative hypothesis, which means that there is a significant association between the predictor and the outcome variables.
</p>
</div>
<p>The t-statistic is a very useful guide for whether or not to include a predictor in a model. High t-statistics (which go with low p-values near 0) indicate that a predictor should be retained in a model, while very low t-statistics indicate a predictor could be dropped <span class="citation">(P. Bruce and Bruce 2017)</span>.</p>
<p><strong>Standard errors and confidence intervals</strong>:</p>
<p>The standard error measures the variability/accuracy of the beta coefficients. It can be used to compute the confidence intervals of the coefficients.</p>
<p>For example, the 95% confidence interval for the coefficient b1 is defined as <code>b1 +/- 2*SE(b1)</code>, where:</p>
<ul>
<li>the lower limits of b1 = <code>b1 - 2*SE(b1) = 0.047 - 2*0.00269 = 0.042</code></li>
<li>the upper limits of b1 = <code>b1 + 2*SE(b1) = 0.047 + 2*0.00269 = 0.052</code></li>
</ul>
<p>That is, there is approximately a 95% chance that the interval [0.042, 0.052] will contain the true value of b1. Similarly the 95% confidence interval for b0 can be computed as <code>b0 +/- 2*SE(b0)</code>.</p>
<p>To get these information, simply type:</p>
<pre class="r"><code>confint(model)</code></pre>
<pre><code>##              2.5 % 97.5 %
## (Intercept) 7.3557 9.5226
## youtube     0.0422 0.0528</code></pre>
</div>
<div id="model-accuracy" class="section level3">
<h3>Model accuracy</h3>
<p>Once you identified that, at least, one predictor variable is significantly associated the outcome, you should continue the diagnostic by checking how well the model fits the data. This process is also referred to as the <em>goodness-of-fit</em></p>
<p>The overall quality of the linear regression fit can be assessed using the following three quantities, displayed in the model summary:</p>
<ol style="list-style-type: decimal">
<li>The Residual Standard Error (RSE).</li>
<li>The R-squared (R2)</li>
<li>F-statistic</li>
</ol>
<pre><code>##    rse r.squared f.statistic  p.value
## 1 3.91     0.612         312 1.47e-42</code></pre>
<ol style="list-style-type: decimal">
<li><strong>Residual standard error</strong> (RSE).</li>
</ol>
<p>The RSE (also known as the model sigma) is the residual variation, representing the average variation of the observations points around the fitted regression line. This is the standard deviation of residual errors.</p>
<p>RSE provides an absolute measure of patterns in the data that can’t be explained by the model. When comparing two models, the model with the small RSE is a good indication that this model fits the best the data.</p>
<p>Dividing the RSE by the average value of the outcome variable will give you the prediction error rate, which should be as small as possible.</p>
<p>In our example, RSE = 3.91, meaning that the observed sales values deviate from the true regression line by approximately 3.9 units in average.</p>
<p>Whether or not an RSE of 3.9 units is an acceptable prediction error is subjective and depends on the problem context. However, we can calculate the percentage error. In our data set, the mean value of sales is 16.827, and so the percentage error is 3.9/16.827 = 23%.</p>
<pre class="r"><code>sigma(model)*100/mean(marketing$sales)</code></pre>
<pre><code>## [1] 23.2</code></pre>
<ol start="2" style="list-style-type: decimal">
<li><strong>R-squared and Adjusted R-squared</strong>:</li>
</ol>
<p>The R-squared (R2) ranges from 0 to 1 and represents the proportion of information (i.e. variation) in the data that can be explained by the model. The adjusted R-squared adjusts for the degrees of freedom.</p>
<p>The R2 measures, how well the model fits the data. For a simple linear regression, R2 is the square of the Pearson correlation coefficient.</p>
<p>A high value of R2 is a good indication. However, as the value of R2 tends to increase when more predictors are added in the model, such as in multiple linear regression model, you should mainly consider the adjusted R-squared, which is a penalized R2 for a higher number of predictors.</p>
<ul>
<li>An (adjusted) R2 that is close to 1 indicates that a large proportion of the variability in the outcome has been explained by the regression model.</li>
<li>A number near 0 indicates that the regression model did not explain much of the variability in the outcome.</li>
</ul>
<ol start="3" style="list-style-type: decimal">
<li><strong>F-Statistic</strong>:</li>
</ol>
<p>The F-statistic gives the overall significance of the model. It assess whether at least one predictor variable has a non-zero coefficient.</p>
<p>In a simple linear regression, this test is not really interesting since it just duplicates the information in given by the t-test, available in the coefficient table. In fact, the F test is identical to the square of the t test: 312.1 = (17.67)^2. This is true in any model with 1 degree of freedom.</p>
<p>The F-statistic becomes more important once we start using multiple predictors as in multiple linear regression.</p>
<p>A large F-statistic will corresponds to a statistically significant p-value (p < 0.05). In our example, the F-statistic equal 312.14 producing a p-value of 1.46e-42, which is highly significant.</p>
</div>
<div id="summary" class="section level3">
<h3>Summary</h3>
<p>After computing a regression model, a first step is to check whether, at least, one predictor is significantly associated with outcome variables.</p>
<p>If one or more predictors are significant, the second step is to assess how well the model fits the data by inspecting the Residuals Standard Error (RSE), the R2 value and the F-statistics. These metrics give the overall quality of the model.</p>
<div class="success">
<ul>
<li>
RSE: Closer to zero the better
</li>
<li>
R-Squared: Higher the better
</li>
<li>
F-statistic: Higher the better
</li>
</ul>
</div>
</div>
</div>
<div id="read-more" class="section level2">
<h2>Read more</h2>
<ul>
<li>Introduction to statistical learning <span class="citation">(James et al. 2014)</span></li>
<li>Practical Statistics for Data Scientists <span class="citation">(P. Bruce and Bruce 2017)</span></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>Sat, 10 Mar 2018 15:30:00 +0100</pubDate>
			
		</item>
		
		<item>
			<title><![CDATA[Multiple Linear Regression in R]]></title>
			<link>https://www.sthda.com/english/articles/40-regression-analysis/168-multiple-linear-regression-in-r/</link>
			<guid>https://www.sthda.com/english/articles/40-regression-analysis/168-multiple-linear-regression-in-r/</guid>
			<description><![CDATA[<!-- START HTML -->

  <div id="rdoc">
<p><strong>Multiple linear regression</strong> is an extension of <a href="https://www.sthda.com/english/articles/40-regression-analysis/167-simple-linear-regression-in-r/">simple linear regression</a> used to predict an outcome variable (y) on the basis of multiple distinct predictor variables (x).</p>
<p>With three predictor variables (x), the prediction of y is expressed by the following equation:</p>
<p><code>y = b0 + b1*x1 + b2*x2 + b3*x3</code></p>
<p>The “b” values are called the regression weights (or <em>beta coefficients</em>). They measure the association between the predictor variable and the outcome. “b_j” can be interpreted as the average effect on y of a one unit increase in “x_j”, holding all other predictors fixed.</p>
<p>In this chapter, you will learn how to:</p>
<ul>
<li>Build and interpret a multiple linear regression model in R</li>
<li>Check the overall quality of the model</li>
</ul>
<p>Make sure, you have read our previous article: [simple linear regression model]((<a href="https://www.sthda.com/english/articles/40-regression-analysis/167-simple-linear-regression-in-r/" class="uri">https://www.sthda.com/english/articles/40-regression-analysis/167-simple-linear-regression-in-r/</a>).</p>
<p>Contents:</p>
<div id="TOC">
<ul>
<li><a href="#loading-required-r-packages">Loading required R packages</a></li>
<li><a href="#examples-of-data">Examples of data</a></li>
<li><a href="#building-model">Building model</a></li>
<li><a href="#interpretation">Interpretation</a></li>
<li><a href="#model-accuracy-assessment">Model accuracy assessment</a></li>
<li><a href="#read-also">Read also</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>
<p>The following R packages are required for this chapter:</p>
<ul>
<li><code>tidyverse</code> for data manipulation and visualization</li>
</ul>
<pre class="r"><code>library(tidyverse)</code></pre>
</div>
<div id="examples-of-data" class="section level2">
<h2>Examples of data</h2>
<p>We’ll use the <code>marketing</code> data set [datarium package], which contains the impact of the amount of money spent on three advertising medias (youtube, facebook and newspaper) on sales.</p>
<p>First install the <code>datarium</code> package using <code>devtools::install_github("kassmbara/datarium")</code>, then load and inspect the <code>marketing</code> data as follow:</p>
<pre class="r"><code>data("marketing", package = "datarium")
head(marketing, 4)</code></pre>
<pre><code>##   youtube facebook newspaper sales
## 1   276.1     45.4      83.0  26.5
## 2    53.4     47.2      54.1  12.5
## 3    20.6     55.1      83.2  11.2
## 4   181.8     49.6      70.2  22.2</code></pre>
</div>
<div id="building-model" class="section level2">
<h2>Building model</h2>
<p>We want to build a model for estimating sales based on the advertising budget invested in youtube, facebook and newspaper, as follow:</p>
<p><code>sales = b0 + b1*youtube + b2*facebook + b3*newspaper</code></p>
<p>You can compute the model coefficients in R as follow:</p>
<pre class="r"><code>model <- lm(sales ~ youtube + facebook + newspaper, data = marketing)
summary(model)</code></pre>
<pre><code>## 
## Call:
## lm(formula = sales ~ youtube + facebook + newspaper, data = marketing)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -10.59  -1.07   0.29   1.43   3.40 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  3.52667    0.37429    9.42   <2e-16 ***
## youtube      0.04576    0.00139   32.81   <2e-16 ***
## facebook     0.18853    0.00861   21.89   <2e-16 ***
## newspaper   -0.00104    0.00587   -0.18     0.86    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.02 on 196 degrees of freedom
## Multiple R-squared:  0.897,  Adjusted R-squared:  0.896 
## F-statistic:  570 on 3 and 196 DF,  p-value: <2e-16</code></pre>
</div>
<div id="interpretation" class="section level2">
<h2>Interpretation</h2>
<p>The first step in interpreting the multiple regression analysis is to examine the F-statistic and the associated p-value, at the bottom of model summary.</p>
<p>In our example, it can be seen that p-value of the F-statistic is < 2.2e-16, which is highly significant. This means that, at least, one of the predictor variables is significantly related to the outcome variable.</p>
<p>To see which predictor variables are significant, you can examine the coefficients table, which shows the estimate of regression beta coefficients and the associated t-statitic p-values:</p>
<pre class="r"><code>summary(model)$coefficient</code></pre>
<pre><code>##             Estimate Std. Error t value Pr(>|t|)
## (Intercept)  3.52667    0.37429   9.422 1.27e-17
## youtube      0.04576    0.00139  32.809 1.51e-81
## facebook     0.18853    0.00861  21.893 1.51e-54
## newspaper   -0.00104    0.00587  -0.177 8.60e-01</code></pre>
<p>For a given the predictor, the t-statistic evaluates whether or not there is significant association between the predictor and the outcome variable, that is whether the beta coefficient of the predictor is significantly different from zero.</p>
<p>It can be seen that, changing in youtube and facebook advertising budget are significantly associated to changes in sales while changes in newspaper budget is not significantly associated with sales.</p>
<p>For a given predictor variable, the coefficient (b) can be interpreted as the average effect on y of a one unit increase in predictor, holding all other predictors fixed.</p>
<p>For example, for a fixed amount of youtube and newspaper advertising budget, spending an additional 1 000 dollars on facebook advertising leads to an increase in sales by approximately 0.1885*1000 = 189 sale units, on average.</p>
<p>The youtube coefficient suggests that for every 1 000 dollars increase in youtube advertising budget, holding all other predictors constant, we can expect an increase of 0.045*1000 = 45 sales units, on average.</p>
<p>We found that newspaper is not significant in the multiple regression model. This means that, for a fixed amount of youtube and newspaper advertising budget, changes in the newspaper advertising budget will not significantly affect sales units.</p>
<p>As the newspaper variable is not significant, it is possible to remove it from the model:</p>
<pre class="r"><code>model  <- lm(sales ~ youtube + facebook, data = marketing)
summary(model)</code></pre>
<pre><code>## 
## Call:
## lm(formula = sales ~ youtube + facebook, data = marketing)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -10.557  -1.050   0.291   1.405   3.399 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  3.50532    0.35339    9.92   <2e-16 ***
## youtube      0.04575    0.00139   32.91   <2e-16 ***
## facebook     0.18799    0.00804   23.38   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.02 on 197 degrees of freedom
## Multiple R-squared:  0.897,  Adjusted R-squared:  0.896 
## F-statistic:  860 on 2 and 197 DF,  p-value: <2e-16</code></pre>
<p>Finally, our model equation can be written as follow: <code>sales = 3.5 + 0.045*youtube + 0.187*facebook</code>.</p>
<p>The confidence interval of the model coefficient can be extracted as follow:</p>
<pre class="r"><code>confint(model)</code></pre>
<pre><code>##             2.5 % 97.5 %
## (Intercept) 2.808 4.2022
## youtube     0.043 0.0485
## facebook    0.172 0.2038</code></pre>
</div>
<div id="model-accuracy-assessment" class="section level2">
<h2>Model accuracy assessment</h2>
<p>As we have seen in simple linear regression, the overall quality of the model can be assessed by examining the R-squared (R2) and Residual Standard Error (RSE).</p>
<p><strong>R-squared</strong>:</p>
<p>In multiple linear regression, the R2 represents the correlation coefficient between the observed values of the outcome variable (y) and the fitted (i.e., predicted) values of y. For this reason, the value of R will always be positive and will range from zero to one.</p>
<p>R2 represents the proportion of variance, in the outcome variable y, that may be predicted by knowing the value of the x variables. An R2 value close to 1 indicates that the model explains a large portion of the variance in the outcome variable.</p>
<p>A problem with the R2, is that, it will always increase when more variables are added to the model, even if those variables are only weakly associated with the response <span class="citation">(James et al. 2014)</span>. A solution is to adjust the R2 by taking into account the number of predictor variables.</p>
<p>The adjustment in the “Adjusted R Square” value in the summary output is a correction for the number of x variables included in the prediction model.</p>
<div class="success">
<p>
In our example, with youtube and facebook predictor variables, the adjusted R2 = 0.89, meaning that “89% of the variance in the measure of sales can be predicted by youtube and facebook advertising budgets.
</p>
<p>
Thi model is better than the simple linear model with only youtube (Chapter simple-linear-regression), which had an adjusted R2 of 0.61.
</p>
</div>
<p><strong>Residual Standard Error</strong> (RSE), or sigma:</p>
<p>The RSE estimate gives a measure of error of prediction. The lower the RSE, the more accurate the model (on the data in hand).</p>
<p>The error rate can be estimated by dividing the RSE by the mean outcome variable:</p>
<pre class="r"><code>sigma(model)/mean(marketing$sales)</code></pre>
<pre><code>## [1] 0.12</code></pre>
<div class="success">
<p>
In our multiple regression example, the RSE is 2.023 corresponding to 12% error rate.
</p>
<p>
Again, this is better than the simple model, with only youtube variable, where the RSE was 3.9 (~23% error rate) (Chapter simple-linear-regression).
</p>
</div>
</div>
<div id="read-also" class="section level2">
<h2>Read also</h2>
<ul>
<li><a href="https://www.sthda.com/english/articles/40-regression-analysis/164-interaction-effect-in-multiple-regression-essentials/">Interaction Effect and Main Effect in Multiple Regression</a></li>
<li><a href="https://www.sthda.com/english/articles/39-regression-model-diagnostics/160-multicollinearity-essentials-and-vif-in-r/">Multicollinearity Essentials and VIF in R</a></li>
<li><a href="https://www.sthda.com/english/articles/39-regression-model-diagnostics/159-confounding-variable-essentials/">Confounding Variable Essentials</a></li>
</ul>
</div>
<div id="discussion" class="section level2">
<h2>Discussion</h2>
<p>This chapter describes multiple linear regression model.</p>
<p>Note that, if you have many predictors variable in your data, you don’t necessarily need to type their name when computing the model.</p>
<p>To compute multiple regression using all of the predictors in the data set, simply type this:</p>
<pre class="r"><code>model <- lm(sales ~., data = marketing)</code></pre>
<p>If you want to perform the regression using all of the variables except one, say newspaper, type this:</p>
<pre class="r"><code>model <- lm(sales ~. -newspaper, data = marketing)</code></pre>
<p>Alternatively, you can use the update function:</p>
<pre class="r"><code>model1 <- update(model,  ~. -newspaper)</code></pre>
</div>
<div id="references" class="section level2 unnumbered">
<h2>References</h2>
<div id="refs" class="references">
<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>Sat, 10 Mar 2018 15:29:00 +0100</pubDate>
			
		</item>
		
		<item>
			<title><![CDATA[Predict in R: Model Predictions and Confidence Intervals]]></title>
			<link>https://www.sthda.com/english/articles/40-regression-analysis/166-predict-in-r-model-predictions-and-confidence-intervals/</link>
			<guid>https://www.sthda.com/english/articles/40-regression-analysis/166-predict-in-r-model-predictions-and-confidence-intervals/</guid>
			<description><![CDATA[<!-- START HTML -->

  <div id="rdoc">
<p>The main goal of <a href="https://www.sthda.com/english/articles/40-regression-analysis/165-linear-regression-essentials-in-r/"><strong>linear regression</strong></a> is to <strong>predict</strong> an outcome value on the basis of one or multiple predictor variables.</p>
<p>In this chapter, we’ll describe how to predict outcome for new observations data using R.. You will also learn how to display the confidence intervals and the prediction intervals.</p>
<p>Contents:</p>
<div id="TOC">
<ul>
<li><a href="#build-a-linear-regression">Build a linear regression</a></li>
<li><a href="#prediction-for-new-data-set">Prediction for new data set</a></li>
<li><a href="#confidence-interval">Confidence interval</a></li>
<li><a href="#prediction-interval">Prediction interval</a></li>
<li><a href="#prediction-interval-or-confidence-interval">Prediction interval or confidence interval?</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="build-a-linear-regression" class="section level2">
<h2>Build a linear regression</h2>
<p>We start by building a simple linear regression model that predicts the stopping distances of cars on the basis of the speed.</p>
<pre class="r"><code># Load the data
data("cars", package = "datasets")
# Build the model
model <- lm(dist ~ speed, data = cars)
model</code></pre>
<pre><code>## 
## Call:
## lm(formula = dist ~ speed, data = cars)
## 
## Coefficients:
## (Intercept)        speed  
##      -17.58         3.93</code></pre>
<p>The linear model equation can be written as follow: <code>dist = -17.579 + 3.932*speed</code>.</p>
<p>Note that, the units of the variable <code>speed</code> and <code>dist</code> are respectively, <code>mph</code> and <code>ft</code>.</p>
</div>
<div id="prediction-for-new-data-set" class="section level2">
<h2>Prediction for new data set</h2>
<p>Using the above model, we can predict the stopping distance for a new speed value.</p>
<p>Start by creating a new data frame containing, for example, three new speed values:</p>
<pre class="r"><code>new.speeds <- data.frame(
  speed = c(12, 19, 24)
)</code></pre>
<p>You can predict the corresponding stopping distances using the R function <code>predict()</code> as follow:</p>
<pre class="r"><code>predict(model, newdata = new.speeds)</code></pre>
<pre><code>##    1    2    3 
## 29.6 57.1 76.8</code></pre>
</div>
<div id="confidence-interval" class="section level2">
<h2>Confidence interval</h2>
<p>The confidence interval reflects the uncertainty around the mean predictions. To display the 95% confidence intervals around the mean the predictions, specify the option <code>interval = "confidence"</code>:</p>
<pre class="r"><code>predict(model, newdata = new.speeds, interval = "confidence")</code></pre>
<pre><code>##    fit  lwr  upr
## 1 29.6 24.4 34.8
## 2 57.1 51.8 62.4
## 3 76.8 68.4 85.2</code></pre>
<p>The output contains the following columns:</p>
<ul>
<li><code>fit</code>: the predicted sale values for the three new advertising budget</li>
<li><code>lwr</code> and <code>upr</code>: the lower and the upper confidence limits for the expected values, respectively. By default the function produces the 95% confidence limits.</li>
</ul>
<p>For example, the 95% confidence interval associated with a speed of 19 is (51.83, 62.44). This means that, according to our model, a car with a speed of 19 mph has, on average, a stopping distance ranging between 51.83 and 62.44 ft.</p>
</div>
<div id="prediction-interval" class="section level2">
<h2>Prediction interval</h2>
<p>The prediction interval gives uncertainty around a single value. In the same way, as the confidence intervals, the prediction intervals can be computed as follow:</p>
<pre class="r"><code>predict(model, newdata = new.speeds, interval = "prediction")</code></pre>
<pre><code>##    fit   lwr   upr
## 1 29.6 -1.75  61.0
## 2 57.1 25.76  88.5
## 3 76.8 44.75 108.8</code></pre>
<p>The 95% prediction intervals associated with a speed of 19 is (25.76, 88.51). This means that, according to our model, 95% of the cars with a speed of 19 mph have a stopping distance between 25.76 and 88.51.</p>
<div class="warning">
<p>
Note that, prediction interval relies strongly on the assumption that the residual errors are normally distributed with a constant variance. So, you should only use such intervals if you believe that the assumption is approximately met for the data at hand.
</p>
</div>
</div>
<div id="prediction-interval-or-confidence-interval" class="section level2">
<h2>Prediction interval or confidence interval?</h2>
<p>A prediction interval reflects the uncertainty around a single value, while a confidence interval reflects the uncertainty around the mean prediction values. Thus, a prediction interval will be generally much wider than a confidence interval for the same value.</p>
<p>Which one should we use? The answer to this question depends on the context and the purpose of the analysis. Generally, we are interested in specific individual predictions, so a prediction interval would be more appropriate. Using a confidence interval when you should be using a prediction interval will greatly underestimate the uncertainty in a given predicted value <span class="citation">(P. Bruce and Bruce 2017)</span>.</p>
<p>The R code below creates a scatter plot with:</p>
<ul>
<li>The regression line in blue</li>
<li>The confidence band in gray</li>
<li>The prediction band in red</li>
</ul>
<pre class="r"><code># 0. Build linear model 
data("cars", package = "datasets")
model <- lm(dist ~ speed, data = cars)
# 1. Add predictions 
pred.int <- predict(model, interval = "prediction")
mydata <- cbind(cars, pred.int)
# 2. Regression line + confidence intervals
library("ggplot2")
p <- ggplot(mydata, aes(speed, dist)) +
  geom_point() +
  stat_smooth(method = lm)
# 3. Add prediction intervals
p + geom_line(aes(y = lwr), color = "red", linetype = "dashed")+
    geom_line(aes(y = upr), color = "red", linetype = "dashed")</code></pre>
<p><img src="https://www.sthda.com/english/sthda-upload/figures/machine-learning-essentials/065-predict-in-r-scatter-plot-with-regression-line-confidence-and-prediction-intervals-1.png" width="384" /></p>
</div>
<div id="discussion" class="section level2">
<h2>Discussion</h2>
<p>In this chapter, we have described how to use the R function <code>predict</code>() for predicting outcome for new data.</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>
</div>
</div><!--end rdoc-->

<!-- END HTML -->]]></description>
			<pubDate>Sat, 10 Mar 2018 15:10:00 +0100</pubDate>
			
		</item>
		
	</channel>
</rss>
