<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/part-5-code-corrections-to-optimism-corrected-bootstrapping-series/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://intobioinformatics.wordpress.com/2018/12/29/part-5-corrections-to-optimism-corrected-bootstrapping-series-but-it-still-is-problematic/"> R – intobioinformatics</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>The truth is out there R readers, but often it is not what we have been led to believe. The previous post examined the strong positive results bias in optimism corrected bootstrapping (a method of assessing a machine learning model’s predictive power) with increasing p (completely random features). There were 2 implementations of the method given, 1 has a slight error, 2 seems fine. The trend is still the same with the corrected code, but the problem with my code is I did not set ‘replace=TRUE’ in the call to the ‘sample’ function.</p>
<p>Thanks to ECOQUANT for pointing out the error.</p>
<p>Let’s just recap <strong>what bootstrapping is</strong> and <strong>what optimism corrected bootstrapping is</strong> before we redo the experiments:</p>
<p>This is from Jason’s excellent blog (<a rel="nofollow" href="https://machinelearningmastery.com/a-gentle-introduction-to-the-bootstrap-method/" target="_blank">https://machinelearningmastery.com/a-gentle-introduction-to-the-bootstrap-method/</a>), bootstrapping is:</p>
<ol>
<li>Choose a number of bootstrap samples to perform</li>
<li>Choose a sample size</li>
<li>For each bootstrap sample (b=1 … B)
<ol>
<li>Draw a sample with replacement with the chosen size</li>
<li>Calculate the statistic on the sample</li>
</ol>
</li>
<li>Calculate the mean of the calculated sample statistics.</li>
</ol>
<p>The with replacement part means we have to put each individual sample back when getting our sample in the bth bootstrap iteration. Thus, we usually have duplicate samples in our sample of the data when doing bootstrapping.</p>
<p>This is the optimism corrected bootstrapping algorithm:</p>
<ol>
<li>Fit a model M to entire data S and estimate predictive ability C.</li>
<li>Iterate from b=1…B:
<ol>
<li>Take a resample from the original data, S*</li>
<li>Fit the bootstrap model M* to S* and get predictive ability, C_boot</li>
<li>Use the bootstrap model M* to get predictive ability on S, C_orig</li>
</ol>
</li>
<li>Optimism O is calculated as mean(C_boot – C_orig)</li>
<li>Calculate optimism corrected performance as C-O.</li>
</ol>
<p>Since we use the same data in step 3 of the bootstrap to train and test the model (an information leak), we would expect increasing bias (C_orig should be too high, thus O too small) when more and more random features are added. See the <a rel="nofollow" href="https://intobioinformatics.wordpress.com/2018/12/28/part-4-more-bias-and-why-does-bias-occur-in-optimism-corrected-bootstrapping/" target="_blank">previous post</a> for more explanation on this. Another point is, the optimism corrected bootstrap is done with a sample size of N instead of just a fraction of N, usually. I found the following quote to support this:</p>
<p>“The small data set was repeatedly re-sampled to produce b replicated data sets, each the same size as the original. We used b = 200. The predictive model was fitted to each of<br>
the b replicated data sets in turn. Each fitted model was then applied both to the resampled data set from which it was generated and to the original data set.”</p>
<p>Smith, Gordon CS, et al. “Correcting for optimistic prediction in small data sets.” <i>American journal of epidemiology</i> 180.3 (2014): 318-324.</p>
<p>I have tried reducing the re-sampling size, which reduces the bias somewhat, but it is still there. This makes sense due to the information leak in this method which results in an under estimation of the optimism (O).</p>
<p>Your welcome to experiment with this code yourselves.</p>
<p>This code can be directly copied and pasted into R to repeat the experiments.</p>
<p><strong>Experiment 1: my implementation – glmnet (lasso logistic regression)</strong></p>
<pre>

library(glmnet)
library(pROC)
library(caret)
library(ggplot2)
library(kimisc)

### TEST 1: bootstrap optimism with glmnet
cc &lt;- c()
for (zz in seq(2,100,1)){
print(zz)
## set up test data
test &lt;- matrix(rnorm(100*zz, mean = 0, sd = 1),
nrow = 100, ncol = zz, byrow = TRUE)
labelsa &lt;- as.factor(c(rep('A',50),rep('B',50)))
colnames(test) &lt;- paste('Y',seq(1,zz),sep='')
row.names(test) &lt;- paste('Z',seq(1,100),sep='')

### my own implementation of optimism corrected bootstrapping

## 1. fit model to entire test data (i.e. overfit it)

orig &lt;- glmnet(test,y=labelsa,alpha=1,family = &quot;binomial&quot;)
preds &lt;- predict(orig,newx=test,type='response',s=0.01)
auc &lt;- roc(labelsa,as.vector(preds))
original_auc &lt;- as.numeric(auc$auc)

## 2. take resample of data and try to estimate optimism

test2 &lt;- cbind(test,labelsa)
B &lt;- 50
results &lt;- matrix(ncol=2,nrow=B)
for (b in seq(1,B)){
## get the bootstrapped resampled data
boot &lt;- test2[sample(row.names(test2),100,replace=TRUE),]
labelsb &lt;- boot[,ncol(boot)]
boot &lt;- boot[,-ncol(boot)]
## use the bootstrapped model to predict its own labels
bootf &lt;- glmnet(boot,y=labelsb,alpha=1,family = &quot;binomial&quot;)
preds &lt;- predict(bootf,newx=boot,type='response',s=0.01)
auc &lt;- roc(labelsb,as.vector(preds))
boot_auc &lt;- as.numeric(auc$auc)
## use bootstrap model to predict original labels
preds &lt;- predict(bootf,newx=test,type='response',s=0.01)
auc &lt;- roc(labelsa,as.vector(preds))
boot_original_auc &lt;- as.numeric(auc$auc)
## add the data to results
results[b,1] &lt;- boot_auc
results[b,2] &lt;- boot_original_auc
}
## calculate the optimism
O &lt;- mean(results[,1]-results[,2])
## calculate optimism corrected measure of prediction (AUC)
corrected &lt;- original_auc-O
##
cc &lt;- c(cc,corrected)
print(cc)
}

## print it
df &lt;- data.frame(p=seq(2,100,1),optimism_corrected_boot_AUC=cc)
p1 &lt;- ggplot(df, aes(x=p, y=optimism_corrected_boot_AUC)) +
geom_line() + ggtitle('glmnet - random data only gives positive result with optimism corrected bootstrap')
print(p1)
png('glmnet_test_upto100.png', height = 15, width = 27, units = 'cm',
res = 900, type = 'cairo')
print(p1)
dev.off()

</pre>
<p>Here are the results with 100 samples and 50 bootstrap iterations from 2 to 100 random features from a Gaussian distribution. We are re-sampling using the original sample size (N=100).</p>
<p>Random features are being added iteratively on the X axis, and on the Y, we have AUC. The AUC should be 0.5 to reflect the data has no real predictive power, but it is highly inflated.</p>
<p><img data-orig-file="https://intobioinformatics.files.wordpress.com/2018/12/glmnet_test_upto100-2.png?w=456" data-orig-size="9566,5314" data-medium-file="https://intobioinformatics.files.wordpress.com/2018/12/glmnet_test_upto100-2.png?w=456?w=300" data-attachment-id="174" data-image-meta="{" data-image-description="" src="https://intobioinformatics.files.wordpress.com/2018/12/glmnet_test_upto100-2.png?w=456" data-permalink="https://intobioinformatics.wordpress.com/2018/12/29/part-5-corrections-to-optimism-corrected-bootstrapping-series-but-it-still-is-problematic/glmnet_test_upto100-3/" data-large-file="https://intobioinformatics.files.wordpress.com/2018/12/glmnet_test_upto100-2.png?w=456?w=620" alt="glmnet_test_upto100" data-image-title="glmnet_test_upto100" sizes="(max-width: 620px) 100vw, 620px" data-comments-opened="1" class="wp-image-174" data-recalc-dims="1"></p>
<p><strong>Experiment 2: another implementation – glm (logistic regression)<br>
</strong></p>
<pre>

## TEST2
auc.adjust &lt;- function(data, fit, B){
fit.model &lt;- fit
data$pred.prob &lt;- fitted(fit.model)
# get overfitted AUC
auc.app &lt;- roc(data[,1], data$pred.prob, data=data)$auc # require 'pROC'
auc.boot &lt;- vector (mode = &quot;numeric&quot;, length = B)
auc.orig &lt;- vector (mode = &quot;numeric&quot;, length = B)
o &lt;- vector (mode = &quot;numeric&quot;, length = B)
for(i in 1:B){
boot.sample &lt;- sample.rows(data, nrow(data), replace=TRUE) # require 'kimisc'
fit.boot &lt;- glm(formula(fit.model), data = boot.sample, family = &quot;binomial&quot;)
boot.sample$pred.prob &lt;- fitted(fit.boot)
# get bootstrapped AUC
auc.boot[i] &lt;- roc(boot.sample[,1], boot.sample$pred.prob, data=boot.sample)$auc
# get original data boot AUC
data$pred.prob.back &lt;- predict.glm(fit.boot, newdata=data, type=&quot;response&quot;)
auc.orig[i] &lt;- roc(data[,1], data$pred.prob.back, data=data)$auc
# calculated optimism corrected version
o[i] &lt;- auc.boot[i] - auc.orig[i]
}
auc.adj &lt;- auc.app - (sum(o)/B)
return(auc.adj)
}
cc &lt;- c()
for (zz in seq(2,100,1)){
print(zz)
## set up test data
test &lt;- matrix(rnorm(100*zz, mean = 0, sd = 1),
nrow = 100, ncol = zz, byrow = TRUE)
labelsa &lt;- as.factor(c(rep('A',50),rep('B',50)))
colnames(test) &lt;- paste('Y',seq(1,zz),sep='')
row.names(test) &lt;- paste('Z',seq(1,100),sep='')
test2 &lt;- data.frame(cbind(labelsa,test))
test2$labelsa &lt;- as.factor(test2$labelsa)
## 1. make overfitted model
model &lt;- glm(labelsa ~., data = test2, family = &quot;binomial&quot;)
## 2. estimate optimism and correct
d &lt;- auc.adjust(test2, model, B=200)
cc &lt;- c(cc,d)
}
## print it
df &lt;- data.frame(p=seq(2,100,1),optimism_corrected_boot_AUC=cc)
p2 &lt;- ggplot(df, aes(x=p, y=optimism_corrected_boot_AUC)) +
geom_line() + ggtitle('glm - random data only gives positive result with optimism corrected bootstrap')
print(p2)
png('glmt_test_upto100.png', height = 15, width = 27, units = 'cm',
res = 900, type = 'cairo')
print(p2)
dev.off()

</pre>
<p><img data-orig-file="https://intobioinformatics.files.wordpress.com/2018/12/glmt_test_upto100-1.png?w=456" data-orig-size="9566,5314" data-medium-file="https://intobioinformatics.files.wordpress.com/2018/12/glmt_test_upto100-1.png?w=456?w=300" data-attachment-id="175" data-image-meta="{" data-image-description="" src="https://intobioinformatics.files.wordpress.com/2018/12/glmt_test_upto100-1.png?w=456" data-permalink="https://intobioinformatics.wordpress.com/2018/12/29/part-5-corrections-to-optimism-corrected-bootstrapping-series-but-it-still-is-problematic/glmt_test_upto100-2/" data-large-file="https://intobioinformatics.files.wordpress.com/2018/12/glmt_test_upto100-1.png?w=456?w=620" alt="glmt_test_upto100.png" data-image-title="glmt_test_upto100" sizes="(max-width: 620px) 100vw, 620px" data-comments-opened="1" class="wp-image-175" data-recalc-dims="1"></p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://intobioinformatics.wordpress.com/2018/12/29/part-5-corrections-to-optimism-corrected-bootstrapping-series-but-it-still-is-problematic/"> R – intobioinformatics</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/9FCgvh__VVI" height="1"></a></div>