<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-4-why-does-bias-occur-in-optimism-corrected-bootstrapping/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://intobioinformatics.wordpress.com/2018/12/28/part-4-more-bias-and-why-does-bias-occur-in-optimism-corrected-bootstrapping/"> R – intobioinformatics</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>In the previous parts of the series we demonstrated a positive results bias in optimism corrected bootstrapping by simply adding random features to our labels. This problem is due to an ‘information leak’ in the algorithm, meaning the training and test datasets are not kept seperate when estimating the optimism. Due to this, the optimism, under some conditions, can be very under estimated. Let’s analyse the code, it is pretty straightforward to understand then we can see where the problem originates.</p>
<ol>
<li>Fit a model M to entire data S and estimate predictive ability C. <strong>## this part is our overfitted estimation of performance (can be AUC, accuracy, etc)<br>
</strong></li>
<li>Iterate from b=1…B: <strong>## now we are doing resampling of the data to estimate the error</strong>
<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 <strong>## this will clearly give us another overfitted model performance, which is fine</strong></li>
<li>Use the bootstrap model M* to get predictive ability on S, C_orig <strong>## we are using the same data (samples) used to train the model to test it, therefore it is not surprising that we have values with better performance than expected. C_orig values will be too high.<br>
</strong></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>One way of correcting for this would be changing <strong>step 3 of the bootstrap</strong>, instead of testing on the original data, to test on the left out (unseen) data in the bootstrap. This way the training and test data is kept entirely seperate in terms of samples, thus eliminating our bias towards inflated model performance on null datasets with high features. There is probably no point of doing anyway this when we have methods such as LOOCV and K fold cross validation.</p>
<p>As p (features) >> N (samples) we are going to get better and better ability to get good model performance using the bootstrapped data on the original data. Why? Because the original data contains the same samples as the bootstrap and as we get more features, greater the chance we are going to get some randomly correlating with our response variable. When we test the bootstrap on the original data (plus more samples) it retains some of this random ability to predict the real labels. This is a typical overfitting problem when we have higher numbers of features, and the procedure is faulty.</p>
<p>Let’s take another experimental look at the problem, this code can be directly copied and pasted into R for repeating the analyses and plots. We have two implementations of the method, the first by me for glmnet (lasso logisitic regression), the second for glm (logisitic regression) from this website (<a rel="nofollow" href="http://cainarchaeology.weebly.com/r-function-for-optimism-adjusted-auc.html" target="_blank">http://cainarchaeology.weebly.com/r-function-for-optimism-adjusted-auc.html</a>). Feel free to try different machine learning algorithms and play with the parameters.</p>
<pre>

library(glmnet)
library(pROC)
library(caret)
library(ggplot)
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),50),]
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>So here are the results, as number of noise only features on the x axis increase our 'corrected' estimate of AUC (on y axis) also increases when we start getting enough to allow that noise to randomly predict the labels. So this shows the problem starts about 40-50 features, then gets worse until about 75+. This is with the 'glmnet' function.</p>
<p><img data-orig-file="https://intobioinformatics.files.wordpress.com/2018/12/glmnet_test_upto100-1.png?w=456" data-orig-size="9566,5314" data-medium-file="https://intobioinformatics.files.wordpress.com/2018/12/glmnet_test_upto100-1.png?w=456?w=300" data-attachment-id="161" data-image-meta="{" data-image-description="" src="https://intobioinformatics.files.wordpress.com/2018/12/glmnet_test_upto100-1.png?w=456" data-permalink="https://intobioinformatics.wordpress.com/2018/12/28/part-4-more-bias-and-why-does-bias-occur-in-optimism-corrected-bootstrapping/glmnet_test_upto100-2/" data-large-file="https://intobioinformatics.files.wordpress.com/2018/12/glmnet_test_upto100-1.png?w=456?w=620" alt="glmnet_test_upto100.png" data-image-title="glmnet_test_upto100" sizes="(max-width: 620px) 100vw, 620px" data-comments-opened="1" class="wp-image-161" data-recalc-dims="1"></p>
<p>Let’s look at the method using glm, we find the same trend, different implementation.</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>So there we have it, the method has a problem with it and should not be used with greater than about 40 features. This method unfortunately is currently being used for datasets with higher than this number of dimensions (40+) because people think it is a published method it is safe, unfortunately this is not how the world works R readers. Remember, the system is corrupt, science and statistics is full of lies, and if in doubt, do your own tests on positive and negative controls.</p>
<p>This is with the 'glm' function. Random features added on x axis, the corrected AUC on y axis.</p>
<p><img data-orig-file="https://intobioinformatics.files.wordpress.com/2018/12/glmt_test_upto100.png?w=456" data-orig-size="9566,5314" data-medium-file="https://intobioinformatics.files.wordpress.com/2018/12/glmt_test_upto100.png?w=456?w=300" data-attachment-id="163" data-image-meta="{" data-image-description="" src="https://intobioinformatics.files.wordpress.com/2018/12/glmt_test_upto100.png?w=456" data-permalink="https://intobioinformatics.wordpress.com/2018/12/28/part-4-more-bias-and-why-does-bias-occur-in-optimism-corrected-bootstrapping/glmt_test_upto100/" data-large-file="https://intobioinformatics.files.wordpress.com/2018/12/glmt_test_upto100.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-163" data-recalc-dims="1"></p>
<p>What if you don’t believe it? I mean this is a text book method. Well R readers if that is so I suggest code it your self and try the code here, run the experiments on null (random data only) datasets with increasing features.</p>
<p>This is the last part in the series on debunking the optimism corrected bootstrap method. I consider it, debunked.</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/28/part-4-more-bias-and-why-does-bias-occur-in-optimism-corrected-bootstrapping/"> 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/eMC1BoZw8EU" height="1"></a></div>