<?xml version="1.0" encoding="UTF-8" ?>
<!-- RSS generated by PHPBoost on Wed, 22 Apr 2026 10:00:38 +0200 -->

<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[Last articles - STHDA : Regression Model Validation]]></title>
		<atom:link href="https://www.sthda.com/english/syndication/rss/articles/38" rel="self" type="application/rss+xml"/>
		<link>https://www.sthda.com</link>
		<description><![CDATA[Last articles - STHDA : Regression Model Validation]]></description>
		<copyright>(C) 2005-2026 PHPBoost</copyright>
		<language>en</language>
		<generator>PHPBoost</generator>
		
		
		<item>
			<title><![CDATA[Regression Model Accuracy Metrics: R-square, AIC, BIC, Cp and more]]></title>
			<link>https://www.sthda.com/english/articles/38-regression-model-validation/158-regression-model-accuracy-metrics-r-square-aic-bic-cp-and-more/</link>
			<guid>https://www.sthda.com/english/articles/38-regression-model-validation/158-regression-model-accuracy-metrics-r-square-aic-bic-cp-and-more/</guid>
			<description><![CDATA[<!-- START HTML -->

  <div id="rdoc">





<p>In this chapter we’ll describe different statistical regression <strong>metrics</strong> for measuring the performance of a regression model (Chapter @ref(linear-regression)).</p>
<p>Next, we’ll provide practical examples in R for comparing the performance of two models in order to select the best one for our data.</p>
<p>Contents:</p>
<div id="TOC">
<ul>
<li><a href="#model-performance-metrics">Model performance metrics</a></li>
<li><a href="#loading-required-r-packages">Loading required R packages</a></li>
<li><a href="#example-of-data">Example of data</a></li>
<li><a href="#building-regression-models">Building regression models</a></li>
<li><a href="#assessing-model-quality">Assessing model quality</a></li>
<li><a href="#comparing-regression-models-performance">Comparing regression models performance</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="model-performance-metrics" class="section level2">
<h2>Model performance metrics</h2>
<p>In regression model, the most commonly known evaluation metrics include:</p>
<ol style="list-style-type: decimal">
<li><p><strong>R-squared</strong> (R2), which is the proportion of variation in the outcome that is explained by the predictor variables. In multiple regression models, R2 corresponds to the squared correlation between the observed outcome values and the predicted values by the model. The Higher the R-squared, the better the model.</p></li>
<li><p><strong>Root Mean Squared Error</strong> (RMSE), which measures the average error performed by the model in predicting the outcome for an observation. Mathematically, the RMSE is the square root of the <em>mean squared error (MSE)</em>, which is the average squared difference between the observed actual outome values and the values predicted by the model. So, <code>MSE = mean((observeds - predicteds)^2)</code> and <code>RMSE = sqrt(MSE</code>). The lower the RMSE, the better the model.</p></li>
<li><p><strong>Residual Standard Error</strong> (RSE), also known as the <em>model sigma</em>, is a variant of the RMSE adjusted for the number of predictors in the model. The lower the RSE, the better the model. In practice, the difference between RMSE and RSE is very small, particularly for large multivariate data.</p></li>
<li><p><strong>Mean Absolute Error</strong> (MAE), like the RMSE, the MAE measures the prediction error. Mathematically, it is the average absolute difference between observed and predicted outcomes, <code>MAE = mean(abs(observeds - predicteds))</code>. MAE is less sensitive to outliers compared to RMSE.</p></li>
</ol>
<p>The problem with the above metrics, is that they are sensible to the inclusion of additional variables in the model, even if those variables dont have significant contribution in explaining the outcome. Put in other words, including additional variables in the model will always increase the R2 and reduce the RMSE. So, we need a more robust metric to guide the model choice.</p>
<p>Concerning R2, there is an adjusted version, called <strong>Adjusted R-squared</strong>, which adjusts the R2 for having too many variables in the model.</p>
<p>Additionally, there are four other important metrics - <strong>AIC</strong>, <strong>AICc</strong>, <strong>BIC</strong> and <strong>Mallows Cp</strong> - that are commonly used for model evaluation and selection. These are an unbiased estimate of the model prediction error MSE. The lower these metrics, he better the model.</p>
<ol style="list-style-type: decimal">
<li><strong>AIC</strong> stands for (<em>Akaike’s Information Criteria</em>), a metric developped by the Japanese Statistician, Hirotugu Akaike, 1970. The basic idea of AIC is to penalize the inclusion of additional variables to a model. It adds a penalty that increases the error when including additional terms. The lower the AIC, the better the model.</li>
<li><strong>AICc</strong> is a version of AIC corrected for small sample sizes.</li>
<li><strong>BIC</strong> (or <em>Bayesian information criteria</em>) is a variant of AIC with a stronger penalty for including additional variables to the model.</li>
<li><strong>Mallows Cp</strong>: A variant of AIC developed by Colin Mallows.</li>
</ol>
<div class="block">
<p>
Generally, the most commonly used metrics, for measuring regression model quality and for comparing models, are: Adjusted R2, AIC, BIC and Cp.
</p>
</div>
<p>In the following sections, we’ll show you how to compute these above mentionned metrics.</p>
</div>
<div id="loading-required-r-packages" class="section level2">
<h2>Loading required R packages</h2>
<ul>
<li><code>tidyverse</code> for data manipulation and visualization</li>
<li><code>modelr</code> provides helper functions for computing regression model performance metrics</li>
<li><code>broom</code> creates easily a tidy data frame containing the model statistical metrics</li>
</ul>
<pre class="r"><code>library(tidyverse)
library(modelr)
library(broom)</code></pre>
</div>
<div id="example-of-data" class="section level2">
<h2>Example of data</h2>
<p>We’ll use the built-in R <code>swiss</code> data, introduced in the Chapter @ref(regression-analysis), for predicting fertility score on the basis of socio-economic indicators.</p>
<pre class="r"><code># Load the data
data("swiss")
# Inspect the data
sample_n(swiss, 3)</code></pre>
</div>
<div id="building-regression-models" class="section level2">
<h2>Building regression models</h2>
<p>We start by creating two models:</p>
<ol style="list-style-type: decimal">
<li>Model 1, including all predictors</li>
<li>Model 2, including all predictors except the variable Examination</li>
</ol>
<pre class="r"><code>model1 <- lm(Fertility ~., data = swiss)

model2 <- lm(Fertility ~. -Examination, data = swiss)</code></pre>
</div>
<div id="assessing-model-quality" class="section level2">
<h2>Assessing model quality</h2>
<p>There are many R functions and packages for assessing model quality, including:</p>
<ul>
<li><code>summary()</code> [stats package], returns the R-squared, adjusted R-squared and the RSE</li>
<li><code>AIC()</code> and <code>BIC()</code> [stats package], computes the AIC and the BIC, respectively</li>
</ul>
<pre class="r"><code>summary(model1)
AIC(model1)
BIC(model1)</code></pre>
<ul>
<li><code>rsquare()</code>, <code>rmse()</code> and <code>mae()</code> [modelr package], computes, respectively, the R2, RMSE and the MAE.</li>
</ul>
<pre class="r"><code>library(modelr)
data.frame(
  R2 = rsquare(model1, data = swiss),
  RMSE = rmse(model1, data = swiss),
  MAE = mae(model1, data = swiss)
)</code></pre>
<ul>
<li><code>R2()</code>, <code>RMSE()</code> and <code>MAE()</code> [caret package], computes, respectively, the R2, RMSE and the MAE.</li>
</ul>
<pre class="r"><code>library(caret)
predictions <- model1 %>% predict(swiss)
data.frame(
  R2 = R2(predictions, swiss$Fertility),
  RMSE = RMSE(predictions, swiss$Fertility),
  MAE = MAE(predictions, swiss$Fertility)
)</code></pre>
<ul>
<li><code>glance()</code> [broom package], computes the R2, adjusted R2, sigma (RSE), AIC, BIC.</li>
</ul>
<pre class="r"><code>library(broom)
glance(model1)</code></pre>
<ul>
<li>Manual computation of R2, RMSE and MAE:</li>
</ul>
<pre class="r"><code># Make predictions and compute the
# R2, RMSE and MAE
swiss %>%
  add_predictions(model1) %>%
  summarise(
    R2 = cor(Fertility, pred)^2,
    MSE = mean((Fertility - pred)^2),
    RMSE = sqrt(MSE),
    MAE = mean(abs(Fertility - pred))
  )</code></pre>
</div>
<div id="comparing-regression-models-performance" class="section level2">
<h2>Comparing regression models performance</h2>
<p>Here, we’ll use the function <code>glance()</code> to simply compare the overall quality of our two models:</p>
<pre class="r"><code># Metrics for model 1
glance(model1) %>%
  dplyr::select(adj.r.squared, sigma, AIC, BIC, p.value)</code></pre>
<pre><code>##   adj.r.squared sigma AIC BIC  p.value
## 1         0.671  7.17 326 339 5.59e-10</code></pre>
<pre class="r"><code># Metrics for model 2
glance(model2) %>%
  dplyr::select(adj.r.squared, sigma, AIC, BIC, p.value)</code></pre>
<pre><code>##   adj.r.squared sigma AIC BIC  p.value
## 1         0.671  7.17 325 336 1.72e-10</code></pre>
<p>From the output above, it can be seen that:</p>
<ol style="list-style-type: decimal">
<li><p>The two models have exactly the samed <strong>adjusted R2</strong> (0.67), meaning that they are equivalent in explaining the outcome, here fertility score. Additionally, they have the same amount of <strong>residual standard error</strong> (RSE or sigma = 7.17). However, the model 2 is more simple than model 1 because it incorporates less variables. All things equal, the simple model is always better in statistics.</p></li>
<li><p>The AIC and the BIC of the model 2 are lower than those of the model1. In model comparison strategies, the model with the lowest AIC and BIC score is preferred.</p></li>
<li><p>Finally, the F-statistic p.value of the model 2 is lower than the one of the model 1. This means that the model 2 is statistically more significant compared to model 1, which is consistent to the above conclusion.</p></li>
</ol>
<p>Note that, the RMSE and the RSE are measured in the same scale as the outcome variable. 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>
<pre class="r"><code>sigma(model1)/mean(swiss$Fertility)</code></pre>
<pre><code>## [1] 0.102</code></pre>
<p>In our example the average prediction error rate is 10%.</p>
</div>
<div id="discussion" class="section level2">
<h2>Discussion</h2>
<p>This chapter describes several metrics for assessing the overall performance of a regression model.</p>
<p>The most important metrics are the Adjusted R-square, RMSE, AIC and the BIC. These metrics are also used as the basis of model comparison and optimal model selection.</p>
<p>Note that, these regression metrics are all internal measures, that is they have been computed on the same data that was used to build the regression model. They tell you how well the model fits to the data in hand, called training data set.</p>
<p>In general, we do not really care how well the method works on the training data. Rather, we are interested in the accuracy of the predictions that we obtain when we apply our method to previously unseen test data.</p>
<p>However, the test data is not always available making the test error very difficult to estimate. In this situation, methods such as <strong>cross-validation</strong> (Chapter @ref(cross-validation)) and <strong>bootstrap</strong> (Chapter @ref(bootstrap-resampling)) are applied for estimating the test error (or the prediction error rate) using training data.</p>
</div>


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


<!-- END HTML -->]]></description>
			<pubDate>Sun, 11 Mar 2018 10:28:00 +0100</pubDate>
			
		</item>
		
		<item>
			<title><![CDATA[Cross-Validation Essentials in R]]></title>
			<link>https://www.sthda.com/english/articles/38-regression-model-validation/157-cross-validation-essentials-in-r/</link>
			<guid>https://www.sthda.com/english/articles/38-regression-model-validation/157-cross-validation-essentials-in-r/</guid>
			<description><![CDATA[<!-- START HTML -->


  <div id="rdoc">





<p><strong>Cross-validation</strong> refers to a set of methods for measuring the performance of a given predictive model on new test data sets.</p>
<p>The basic idea, behind cross-validation techniques, consists of dividing the data into two sets:</p>
<ol style="list-style-type: decimal">
<li>The training set, used to train (i.e. build) the model;</li>
<li>and the testing set (or validation set), used to test (i.e. validate) the model by estimating the prediction error.</li>
</ol>
<p>Cross-validation is also known as a <em>resampling method</em> because it involves fitting the same statistical method multiple times using different subsets of the data.</p>
<p>In this chapter, you’ll learn:</p>
<ol style="list-style-type: decimal">
<li><p>the most commonly used statistical metrics (Chapter @ref(regression-model-accuracy-metrics)) for measuring the performance of a regression model in predicting the outcome of new test data.</p></li>
<li>The different cross-validation methods for assessing model performance. We cover the following approaches:
<ul>
<li>Validation set approach (or data split)</li>
<li>Leave One Out Cross Validation</li>
<li>k-fold Cross Validation</li>
<li>Repeated k-fold Cross Validation</li>
</ul></li>
</ol>
<p>Each of these methods has their advantages and drawbacks. Use the method that best suits your problem. Generally, the (repeated) k-fold cross validation is recommended.</p>
<ol start="3" style="list-style-type: decimal">
<li>Practical examples of R codes for computing cross-validation methods.</li>
</ol>
<p>Contents:</p>
<div id="TOC">
<ul>
<li><a href="#loading-required-r-packages">Loading required R packages</a></li>
<li><a href="#example-of-data">Example of data</a></li>
<li><a href="#model-performance-metrics">Model performance metrics</a></li>
<li><a href="#cross-validation-methods">Cross-validation methods</a><ul>
<li><a href="#the-validation-set-approach">The Validation set Approach</a></li>
<li><a href="#leave-one-out-cross-validation---loocv">Leave one out cross validation - LOOCV</a></li>
<li><a href="#k-fold-cross-validation">K-fold cross-validation</a></li>
<li><a href="#repeated-k-fold-cross-validation">Repeated K-fold cross-validation</a></li>
</ul></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 easily computing cross-validation methods</li>
</ul>
<pre class="r"><code>library(tidyverse)
library(caret)</code></pre>
</div>
<div id="example-of-data" class="section level2">
<h2>Example of data</h2>
<p>We’ll use the built-in R <code>swiss</code> data, introduced in the Chapter @ref(regression-analysis), for predicting fertility score on the basis of socio-economic indicators.</p>
<pre class="r"><code># Load the data
data("swiss")
# Inspect the data
sample_n(swiss, 3)</code></pre>
</div>
<div id="model-performance-metrics" class="section level2">
<h2>Model performance metrics</h2>
<p>After building a model, we are interested in determining the accuracy of this model on predicting the outcome for new unseen observations not used to build the model. Put in other words, we want to estimate the prediction error.</p>
<p>To do so, the basic strategy is to:</p>
<ol style="list-style-type: decimal">
<li>Build the model on a training data set</li>
<li>Apply the model on a new test data set to make predictions</li>
<li>Compute the prediction errors</li>
</ol>
<p>In Chapter @ref(regression-model-accuracy-metrics), we described several statistical metrics for quantifying the overall quality of regression models. These include:</p>
<ul>
<li><strong>R-squared</strong> (R2), representing the squared correlation between the observed outcome values and the predicted values by the model. The higher the adjusted R2, the better the model.</li>
<li><strong>Root Mean Squared Error</strong> (RMSE), which measures the average prediction error made by the model in predicting the outcome for an observation. That is, the average difference between the observed known outcome values and the values predicted by the model. The lower the RMSE, the better the model.</li>
<li><strong>Mean Absolute Error</strong> (MAE), an alternative to the RMSE that is less sensitive to outliers. It corresponds to the average absolute difference between observed and predicted outcomes. The lower the MAE, the better the model</li>
</ul>
<p>In classification setting, the prediction error rate is estimated as the proportion of misclassified observations.</p>
<div class="block">
<p>
R2, RMSE and MAE are used to measure the regression model performance during <strong>cross-validation</strong>.
</p>
</div>
<p>In the following section, we’ll explain the basics of cross-validation, and we’ll provide practical example using mainly the <code>caret</code> R package.</p>
</div>
<div id="cross-validation-methods" class="section level2">
<h2>Cross-validation methods</h2>
<p>Briefly, cross-validation algorithms can be summarized as follow:</p>
<ol style="list-style-type: decimal">
<li>Reserve a small sample of the data set</li>
<li>Build (or train) the model using the remaining part of the data set</li>
<li>Test the effectiveness of the model on the the reserved sample of the data set. If the model works well on the test data set, then it’s good.</li>
</ol>
<p>The following sections describe the different cross-validation techniques.</p>
<div id="the-validation-set-approach" class="section level3">
<h3>The Validation set Approach</h3>
<p>The validation set approach consists of randomly splitting the data into two sets: one set is used to train the model and the remaining other set sis used to test the model.</p>
<p>The process works as follow:</p>
<ol style="list-style-type: decimal">
<li>Build (train) the model on the training data set</li>
<li>Apply the model to the test data set to predict the outcome of new unseen observations</li>
<li>Quantify the prediction error as the mean squared difference between the observed and the predicted outcome values.</li>
</ol>
<p>The example below splits the <code>swiss</code> data set so that 80% is used for training a linear regression model and 20% is used to evaluate the model performance.</p>
<pre class="r"><code># Split the data into training and test set
set.seed(123)
training.samples <- swiss$Fertility %>%
  createDataPartition(p = 0.8, list = FALSE)
train.data  <- swiss[training.samples, ]
test.data <- swiss[-training.samples, ]
# Build the model
model <- lm(Fertility ~., data = train.data)
# Make predictions and compute the R2, RMSE and MAE
predictions <- model %>% predict(test.data)
data.frame( R2 = R2(predictions, test.data$Fertility),
            RMSE = RMSE(predictions, test.data$Fertility),
            MAE = MAE(predictions, test.data$Fertility))</code></pre>
<pre><code>##     R2 RMSE  MAE
## 1 0.39 9.11 7.48</code></pre>
<p>When comparing two models, the one that produces the lowest test sample RMSE is the preferred model.</p>
<p>the RMSE and the MAE are measured in the same scale as the outcome variable. Dividing the RMSE by the average value of the outcome variable will give you the prediction error rate, which should be as small as possible:</p>
<pre class="r"><code>RMSE(predictions, test.data$Fertility)/mean(test.data$Fertility)</code></pre>
<pre><code>## [1] 0.128</code></pre>
<p>Note that, the validation set method is only useful when you have a large data set that can be partitioned. A disadvantage is that we build a model on a fraction of the data set only, possibly leaving out some interesting information about data, leading to higher bias. Therefore, the test error rate can be highly variable, depending on which observations are included in the training set and which observations are included in the validation set.</p>
</div>
<div id="leave-one-out-cross-validation---loocv" class="section level3">
<h3>Leave one out cross validation - LOOCV</h3>
<p>This method works as follow:</p>
<ol style="list-style-type: decimal">
<li>Leave out one data point and build the model on the rest of the data set</li>
<li>Test the model against the data point that is left out at step 1 and record the test error associated with the prediction</li>
<li>Repeat the process for all data points</li>
<li>Compute the overall prediction error by taking the average of all these test error estimates recorded at step 2.</li>
</ol>
<p>Practical example in R using the <code>caret</code> package:</p>
<pre class="r"><code># Define training control
train.control <- trainControl(method = "LOOCV")
# Train the model
model <- train(Fertility ~., data = swiss, method = "lm",
               trControl = train.control)
# Summarize the results
print(model)</code></pre>
<pre><code>## Linear Regression 
## 
## 47 samples
##  5 predictor
## 
## No pre-processing
## Resampling: Leave-One-Out Cross-Validation 
## Summary of sample sizes: 46, 46, 46, 46, 46, 46, ... 
## Resampling results:
## 
##   RMSE  Rsquared  MAE 
##   7.74  0.613     6.12
## 
## Tuning parameter &amp;#39;intercept&amp;#39; was held constant at a value of TRUE</code></pre>
<p>The advantage of the LOOCV method is that we make use all data points reducing potential bias.</p>
<p>However, the process is repeated as many times as there are data points, resulting to a higher execution time when n is extremely large.</p>
<p>Additionally, we test the model performance against one data point at each iteration. This might result to higher variation in the prediction error, if some data points are outliers. So, we need a good ratio of testing data points, a solution provided by the <strong>k-fold cross-validation method</strong>.</p>
</div>
<div id="k-fold-cross-validation" class="section level3">
<h3>K-fold cross-validation</h3>
<p>The k-fold cross-validation method evaluates the model performance on different subset of the training data and then calculate the average prediction error rate. The algorithm is as follow:</p>
<ol style="list-style-type: decimal">
<li>Randomly split the data set into k-subsets (or k-fold) (for example 5 subsets)</li>
<li>Reserve one subset and train the model on all other subsets</li>
<li>Test the model on the reserved subset and record the prediction error</li>
<li>Repeat this process until each of the k subsets has served as the test set.</li>
<li>Compute the average of the k recorded errors. This is called the cross-validation error serving as the performance metric for the model.</li>
</ol>
<p>K-fold cross-validation (CV) is a robust method for estimating the accuracy of a model.</p>
<p>The most obvious advantage of k-fold CV compared to LOOCV is computational. A less obvious but potentially more important advantage of k-fold CV is that it often gives more accurate estimates of the test error rate than does LOOCV <span class="citation">(James et al. 2014)</span>.</p>
<p>Typical question, is how to choose right value of k?</p>
<p>Lower value of K is more biased and hence undesirable. On the other hand, higher value of K is less biased, but can suffer from large variability. It is not hard to see that a smaller value of k (say k = 2) always takes us towards validation set approach, whereas a higher value of k (say k = number of data points) leads us to LOOCV approach.</p>
<div class="block">
<p>
In practice, one typically performs k-fold cross-validation using k = 5 or k = 10, as these values have been shown empirically to yield test error rate estimates that suffer neither from excessively high bias nor from very high variance.
</p>
</div>
<p>The following example uses 10-fold cross validation to estimate the prediction error. Make sure to set seed for reproducibility.</p>
<pre class="r"><code># Define training control
set.seed(123) 
train.control <- trainControl(method = "cv", number = 10)
# Train the model
model <- train(Fertility ~., data = swiss, method = "lm",
               trControl = train.control)
# Summarize the results
print(model)</code></pre>
<pre><code>## Linear Regression 
## 
## 47 samples
##  5 predictor
## 
## No pre-processing
## Resampling: Cross-Validated (10 fold) 
## Summary of sample sizes: 43, 42, 42, 41, 43, 41, ... 
## Resampling results:
## 
##   RMSE  Rsquared  MAE 
##   7.38  0.751     6.03
## 
## Tuning parameter &amp;#39;intercept&amp;#39; was held constant at a value of TRUE</code></pre>
</div>
<div id="repeated-k-fold-cross-validation" class="section level3">
<h3>Repeated K-fold cross-validation</h3>
<p>The process of splitting the data into k-folds can be repeated a number of times, this is called repeated k-fold cross validation.</p>
<p>The final model error is taken as the mean error from the number of repeats.</p>
<p>The following example uses 10-fold cross validation with 3 repeats:</p>
<pre class="r"><code># Define training control
set.seed(123)
train.control <- trainControl(method = "repeatedcv", 
                              number = 10, repeats = 3)
# Train the model
model <- train(Fertility ~., data = swiss, method = "lm",
               trControl = train.control)
# Summarize the results
print(model)</code></pre>
</div>
</div>
<div id="discussion" class="section level2">
<h2>Discussion</h2>
<p>In this chapter, we described 4 different methods for assessing the performance of a model on unseen test data.</p>
<p>These methods include: validation set approach, leave-one-out cross-validation, k-fold cross-validation and repeated k-fold cross-validation.</p>
<p>We generally recommend the (repeated) k-fold cross-validation to estimate the prediction error rate. It can be used in regression and classification settings.</p>
<p>Another alternative to cross-validation is the bootstrap resampling methods (Chapter @ref(bootstrap-resampling)), which consists of repeatedly and randomly selecting a sample of n observations from the original data set, and to evaluate the model performance on each copy.</p>
</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>Sun, 11 Mar 2018 10:22:00 +0100</pubDate>
			
		</item>
		
		<item>
			<title><![CDATA[Bootstrap Resampling Essentials in R]]></title>
			<link>https://www.sthda.com/english/articles/38-regression-model-validation/156-bootstrap-resampling-essentials-in-r/</link>
			<guid>https://www.sthda.com/english/articles/38-regression-model-validation/156-bootstrap-resampling-essentials-in-r/</guid>
			<description><![CDATA[<!-- START HTML -->

  <div id="rdoc">





<p>Similarly to cross-validation techniques (Chapter @ref(cross-validation)), the <strong>bootstrap resampling</strong> method can be used to measure the accuracy of a predictive model. Additionally, it can be used to measure the uncertainty associated with any statistical estimator.</p>
<p>Bootstrap resampling consists of repeatedly selecting a sample of n observations from the original data set, and to evaluate the model on each copy. An average standard error is then calculated and the results provide an indication of the overall variance of the model performance.</p>
<p>This chapter describes the basics of bootstrapping and provides practical examples in R for computing a model prediction error. Additionally, we’ll show you how to compute an estimator uncertainty using bootstrap techniques.</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">Example of data</a></li>
<li><a href="#bootstrap-procedure">Bootstrap procedure</a></li>
<li><a href="#evaluating-a-predictive-model-performance">Evaluating a predictive model performance</a></li>
<li><a href="#quantifying-an-estimator-uncertainty-and-confidence-intervals">Quantifying an estimator uncertainty and confidence intervals</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>
<li><code>caret</code> for easily computing cross-validation methods</li>
</ul>
<pre class="r"><code>library(tidyverse)
library(caret)</code></pre>
</div>
<div id="example-of-data" class="section level2">
<h2>Example of data</h2>
<p>We’ll use the built-in R <code>swiss</code> data, introduced in the Chapter @ref(regression-analysis), for predicting fertility score on the basis of socio-economic indicators.</p>
<pre class="r"><code># Load the data
data("swiss")
# Inspect the data
sample_n(swiss, 3)</code></pre>
</div>
<div id="bootstrap-procedure" class="section level2">
<h2>Bootstrap procedure</h2>
<p>The bootstrap method is used to quantify the uncertainty associated with a given statistical estimator or with a predictive model.</p>
<p>It consists of randomly selecting a sample of n observations from the original data set. This subset, called bootstrap data set is then used to evaluate the model.</p>
<p>This procedure is repeated a large number of times and the standard error of the bootstrap estimate is then calculated. The results provide an indication of the variance of the models performance.</p>
<p>Note that, the sampling is performed with replacement, which means that the same observation can occur more than once in the bootstrap data set.</p>
</div>
<div id="evaluating-a-predictive-model-performance" class="section level2">
<h2>Evaluating a predictive model performance</h2>
<p>The following example uses a bootstrap with 100 resamples to test a linear regression model:</p>
<pre class="r"><code># Define training control
train.control <- trainControl(method = "boot", number = 100)
# Train the model
model <- train(Fertility ~., data = swiss, method = "lm",
               trControl = train.control)
# Summarize the results
print(model)</code></pre>
<pre><code>## Linear Regression 
## 
## 47 samples
##  5 predictor
## 
## No pre-processing
## Resampling: Bootstrapped (100 reps) 
## Summary of sample sizes: 47, 47, 47, 47, 47, 47, ... 
## Resampling results:
## 
##   RMSE  Rsquared  MAE 
##   8.4   0.597     6.76
## 
## Tuning parameter &amp;#39;intercept&amp;#39; was held constant at a value of TRUE</code></pre>
<p>The output shows the average model performance across the 100 resamples.</p>
<p>RMSE (Root Mean Squared Error) and MAE(Mean Absolute Error), represent two different measures of the model prediction error. The lower the RMSE and the MAE, the better the model. The R-squared represents the proportion of variation in the outcome explained by the predictor variables included in the model. The higher the R-squared, the better the model. Read more on these metrics at Chapter @ref(regression-model-accuracy-metrics).</p>
</div>
<div id="quantifying-an-estimator-uncertainty-and-confidence-intervals" class="section level2">
<h2>Quantifying an estimator uncertainty and confidence intervals</h2>
<p>The bootstrap approach can be used to quantify the uncertainty (or standard error) associated with any given statistical estimator.</p>
<p>For example, you might want to estimate the accuracy of the linear regression beta coefficients using bootstrap method.</p>
<p>The different steps are as follow:</p>
<ol style="list-style-type: decimal">
<li>Create a simple function, <code>model_coef()</code>, that takes the <code>swiss</code> data set as well as the indices for the observations, and returns the regression coefficients.</li>
<li>Apply the function <code>boot_fun()</code> to the full data set of 47 observations in order to compute the coefficients</li>
</ol>
<p>We start by creating a function that returns the regression model coefficients:</p>
<pre class="r"><code>model_coef <- function(data, index){
  coef(lm(Fertility ~., data = data, subset = index))
}

model_coef(swiss, 1:47)</code></pre>
<pre><code>##      (Intercept)      Agriculture      Examination        Education 
##           66.915           -0.172           -0.258           -0.871 
##         Catholic Infant.Mortality 
##            0.104            1.077</code></pre>
<p>Next, we use the <code>boot()</code> function [boot package] to compute the standard errors of 500 bootstrap estimates for the coefficients:</p>
<pre class="r"><code>library(boot)
boot(swiss, model_coef, 500)</code></pre>
<pre><code>## 
## ORDINARY NONPARAMETRIC BOOTSTRAP
## 
## 
## Call:
## boot(data = swiss, statistic = model_coef, R = 500)
## 
## 
## Bootstrap Statistics :
##     original    bias    std. error
## t1*   66.915 -2.04e-01     10.9174
## t2*   -0.172 -5.62e-03      0.0639
## t3*   -0.258 -2.27e-02      0.2524
## t4*   -0.871  3.89e-05      0.2203
## t5*    0.104 -7.77e-04      0.0319
## t6*    1.077  4.45e-02      0.4478</code></pre>
<p>In the output above,</p>
<ul>
<li><code>original</code> column corresponds to the regression coefficients. The associated standard errors are given in the column <code>std.error</code>.</li>
<li>t1 corresponds to the intercept, t2 corresponds to <code>Agriculture</code> and so on…</li>
</ul>
<p>For example, it can be seen that, the standard error (SE) of the regression coefficient associated with <code>Agriculture</code> is 0.06.</p>
<p>Note that, the standard errors measure 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 a given coefficient b is defined as <code>b +/- 2*SE(b)</code>, where:</p>
<ul>
<li>the lower limits of b = <code>b - 2*SE(b) = -0.172 - (2*0.0680) = -0.308</code> (for Agriculture variable)</li>
<li>the upper limits of b = <code>b + 2*SE(b) = -0.172 + (2*0.0680) = -0.036</code> (for Agriculture variable)</li>
</ul>
<p>That is, there is approximately a 95% chance that the interval [-0.308, -0.036] will contain the true value of the coefficient.</p>
<p>Using the standard <code>lm()</code> function gives a slightly different standard errors, because the linear model make some assumptions about the data:</p>
<pre class="r"><code>summary(lm(Fertility ~., data = swiss))$coef</code></pre>
<pre><code>##                  Estimate Std. Error t value Pr(>|t|)
## (Intercept)        66.915    10.7060    6.25 1.91e-07
## Agriculture        -0.172     0.0703   -2.45 1.87e-02
## Examination        -0.258     0.2539   -1.02 3.15e-01
## Education          -0.871     0.1830   -4.76 2.43e-05
## Catholic            0.104     0.0353    2.95 5.19e-03
## Infant.Mortality    1.077     0.3817    2.82 7.34e-03</code></pre>
<p>The bootstrap approach does not rely on any of these assumptions made by the linear model, and so it is likely giving a more accurate estimate of the coefficients standard errors than is the summary() function.</p>
</div>
<div id="discussion" class="section level2">
<h2>Discussion</h2>
<p>This chapter describes bootstrap resampling method for evaluating a predictive model accuracy, as well as, for measuring the uncertainty associated with a given statistical estimator.</p>
<p>An alternative approach to bootstrapping, for evaluating a predictive model performance, is cross-validation techniques (Chapter @ref(cross-validation)).</p>
</div>


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


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