<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/estimating-mean-variance-and-mean-absolute-bias-of-a-regression-tree-by-bootstrapping-using-foreach-and-rpart-packages/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://blog.revolutionanalytics.com/2017/10/bias-bootstrap-foreach.html"> Revolutions</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<p><em>by <a rel="nofollow" href="https://www.linkedin.com/in/b%C5%82a%C5%BCej-moska-9a316113a/" target="_blank">Błażej Moska</a>, computer science student and data science intern</em> </p>
<p>One of the most important thing in predictive modelling is how our algorithm will cope with various datasets, both training and testing (previously unseen). This is strictly connected with the concept of <a rel="nofollow" href="http://www.cs.cmu.edu/~wcohen/10-601/bias-variance.pdf" target="_blank">bias-variance tradeoff</a>.</p>
<p>Roughly speaking, variance of an estimator describes, how do estimator value ranges from dataset to dataset. It's <a rel="nofollow" href="https://en.wikipedia.org/wiki/Bias%E2%80%93variance_tradeoff" target="_blank">defined as follows</a>:</p>
<p>\[ \textrm{Var}[ \widehat{f} (x) ]=E[(\widehat{f} (x)-E[\widehat{f} (x)])^{2} ] \]</p>
<p>\[ \textrm{Var}[ \widehat{f} (x)]=E[(\widehat{f} (x)^2]-E[\widehat{f} (x)]^2 \]</p>
<p>Bias is <a rel="nofollow" href="https://en.wikipedia.org/wiki/Bias_of_an_estimator" target="_blank">defined as follows</a>:</p>
<p>\[ \textrm{Bias}[ \widehat{f} (x)]=E[\widehat{f}(x)-f(x)]=E[\widehat{f}(x)]-f(x) \]</p>
<p>One could think of a Bias as an ability to approximate function. Typically, reducing bias results in increased variance and vice versa.</p>
<p>\(E[X]\) is an expected value, this could be estimated using a mean, since <a rel="nofollow" href="https://onlinecourses.science.psu.edu/stat414/node/192" target="_blank">mean is an unbiased estimator of the expected value</a>.</p>
<p>We can estimate variance and bias by bootstrapping original training dataset, that is, by sampling with replacement indexes of an original dataframe, then drawing rows which correspond to these indexes and obtaining new dataframes. This operation was repeated over <code>nsampl</code> times, where <code>nsampl</code> is the parameter describing number of bootstrap samples.</p>
<p>Variance and Bias is estimated for one value, that is to say, for one observation/row of an original dataset (we calculate variance and bias over rows of predictions made on bootstrap samples). We then obtain a vector containing variances/biases. This vector is of the same length as the number of observations of the original dataset. For the purpose of this article, for each of these two vectors a mean value was calculated. We will treat these two means as our estimates of mean bias and mean variance. If we don't want to measure direction of the bias, we can <a rel="nofollow" href="http://viceroy.eeb.uconn.edu/estimates/EstimateSPages/EstSUsersGuide/References/WaltherAndMoore2005.pdf" target="_blank">take absolute values of bias</a>.</p>
<p>Because bias and variance could be controlled by parameters sent to the <code>rpart</code> function, we can also survey how do these parameters affect tree variance. The most commonly used parameters are <code>cp</code> (complexity parameter), which describe how much each split must decrease overall variance of a decision variable in order to be attempted, and <code>minsplit</code>, which defines minimum number of observations needed to attempt a split.</p>
<p>Operations mentioned above is rather exhaustive in computational terms: we need to create <em>nsampl</em> bootstrap samples, grow <code>nsampl</code> trees, calculate <code>nsampl</code> predictions, <code>nrow</code> variances, <code>nrow</code> biases and repeat those operations for the number of parameters (length of the vector <code>cp</code> or <code>minsplit</code>). For that reason the <a rel="nofollow" href="https://mran.microsoft.com/package/foreach/" target="_blank">foreach package</a> was used, to take advantage of parallelism. The above procedure still can't be considered as fast, but It was much faster than without using the foreach package.</p>
<p>So, summing up, the procedure looks as follows:</p>
<ol>
<li>Create bootstrap samples (by bootstrapping original dataset)</li>
<li>Train model on each of these bootstrap datasets</li>
<li>Calculate mean of predictions of these trees (for each observation) and compare these predictions with values of the original datasets (in other words, calculate bias for each row)</li>
<li>Calculate variance of predictions for each row (estimate variance of an estimator-regression tree)</li>
<li>Calculate mean bias/absolute bias and mean variance </li>
</ol>
<h3>R Code</h3>
<p></div>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://blog.revolutionanalytics.com/2017/10/bias-bootstrap-foreach.html"> Revolutions</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 width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/bm9LRYBws3g" height="1"></a></div>