<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/hacking-statistics-or-how-i-learned-to-stop-worrying-about-calculus-and-love-stats-exercises-part-3/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.r-exercises.com/2017/07/18/hacking-statistics-exercises-part-3/?utm_source=rss&utm_medium=rss&utm_campaign=hacking-statistics-exercises-part-3"> R-exercises</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a rel="nofollow" href="https://i0.wp.com/www.r-exercises.com/wp-content/uploads/2017/07/HS_part3-5.png" target="_blank"><img sizes="(max-width: 150px) 100vw, 150px" src="https://i0.wp.com/www.r-exercises.com/wp-content/uploads/2017/07/HS_part3-5-150x150.png?resize=150%2C150" alt="" class="wp-image-6941" data-recalc-dims="1"></a>Statistics are often taught in school by and for people who like Mathematics. As a consequence, in those class emphasis is put on leaning equations, solving calculus problems and creating mathematics models instead of building an intuition for probabilistic problems. But, if you read this, you know a bit of R programming and have access to a computer that is really good at computing stuff! So let’s learn how we can tackle useful statistic problems by writing simple R query and how to think in probabilistic terms.</p>
<p>In the first two part of this series, we’ve seen how to identify the distribution of a random variable by <a rel="nofollow" href="http://www.r-exercises.com/2017/07/01/hacking-statistics-exercises-part-1/" target="_blank">plotting the distribution of a sample<a> and by estimating <a rel="nofollow" href="http://www.r-exercises.com/2017/07/08/hacking-statistics-exercises-part-2/" target="_blank">statistic</a>. We also seen that it can be tricky to identify a distribution from a small sample of data. Today, we’ll see how to estimate the confidence interval of a statistic in this situation by using a powerful method called <a rel="nofollow" href="https://en.wikipedia.org/wiki/Bootstrapping_(statistics)" target="_blank">bootstrapping.</a>
<p>Answers to the exercises are available <a rel="nofollow" href="http://www.r-exercises.com/2017/07/18/hacking-statistics-solutions-part-3/" target="_blank">here</a>.</p>
<p><strong>Exercise 1</strong><br>
Load <a rel="nofollow" href="http://www.r-exercises.com/wp-content/uploads/2017/07/Exercise1.HS3_.csv" target="_blank">this dataset</a> and draw the histogram, the ECDF of this sample and the ECDF of a density who’s a good fit for the data.</p>
<p><strong>Exercise 2</strong><br>
Write a function that takes a dataset and a number of iterations as parameter. For each iteration this function must create a sample with replacement of the same size than the dataset, calculate the mean of the sample and store it in a matrix, which the function must return.</p>
<p><strong>Exercise 3</strong><br>
Use the <code>t.test()</code> to compute the 95% confidence interval estimate for the mean of your dataset.</p>
<div>
<aside><img src="http://www.r-exercises.com/2017/07/18/hacking-statistics-exercises-part-3/?utm_source=rss&utm_medium=rss&utm_campaign=hacking-statistics-exercises-part-3"></aside>
<div><strong>Learn more</strong> about bootstrapping functions in the online course <a rel="nofollow" href="http://www.r-exercises.com/product/structural-equation-modeling-sem-with-lavaan/" target="_blank">Structural equation modeling (SEM) with lavaan</a>. In this course you will learn how to:
<ul>
<li>Learn how to develop bootstrapped confidence intervals</li>
<li>Go indepth into the lavaan package for modelling equations</li>
<li>And much more</li>
</ul>
</div>
</div>
<p><strong>Exercise 4</strong><br>
Use the function you just wrote to estimate the mean of your sample 10,000 times. Then draw the histogram of the results and the sampling mean of the data.</p>
<p>The probability distribution of the estimation of a mean is a normal distribution centered around the real value of the mean. In other words, if we take a lot of samples from a population and compute the mean of each sample, the histogram of those mean will look like one of a normal distribution center around the real value of the mean we try to estimate. We have recreated artificially this process by creating a bunch of new sample from the dataset, by resampling it with replacement and now we can do a point estimation of the mean by computing the average of the sample of means or compute the confidence interval by finding the correct percentile of this distribution. This process is basically what is called bootstrapping.</p>
<p><strong>Exercise 5</strong><br>
Calculate the value of the 2.5 and 97.5 percentile of your sample of 10,000 estimates of the mean and the mean of this sample. Compare this last value to the value of the sample mean of your data.</p>
<p><strong>Exercise 6</strong><br>
Bootstrapping can be used to compute the confidence interval of all the statistics of interest, but you don’t have to write a function for each of them! You can use the <code>boot()</code> function from the library of the same name and pass the statistic as argument to compute the bootstrapped sample. Use this function with 10,000 replicates to compute the median of the dataset.</p>
<p><strong>Exercise 7</strong><br>
Look at the structure of your result and plot his histogram. On the same plot, draw the value of the sample median of your dataset and plot the 95% confidence interval of this statistic by adding two vertical green lines at the lower and higher bounds of the interval.</p>
<p><strong>Exercise 8</strong><br>
Write functions to compute by bootstrapping the following statistics:</p>
<ul>
<li>Variance</li>
<li>kurtosis</li>
<li>Max</li>
<li>Min</li>
</ul>
<p><strong>Exercise 9</strong><br>
Use the functions from last exercise and the boot function with 10,000 replicates to compute the following statistics:</p>
<ul>
<li>Variance</li>
<li>kurtosis</li>
<li>Max</li>
<li>Min</li>
</ul>
<p>Then draw the histogram of the bootstrapped sample and plot the 95% confidence interval of the statistics.</p>
<p><strong>Exercise 10</strong><br>
Generate 1000 points from a normal distribution of mean and standard deviation equal to the one of the dataset. Use the bootstrap method to estimate the 95% confidence interval of the mean, the variance, the kurtosis, the min and the max of this density. Then plot the histograms of the bootstrap samples for each of the variable and draw the 95% confidence interval as two red vertical line.</p>
<p>Two bootstrap estimate of the same statistic of two sample who are distributed by the same density should be pretty similar. When we compare those last plots with the confidence interval we drawn before we see that they are. More importantly, the confidence interval computed in exercise 10 overlap the confidence interval of the statistics of the first dataset. As a consequence, we can’t conclude that the two sample come from different density distribution and in practice we could use a normal distribution with a mean of 0.4725156 and a standard deviation of 1.306665 to simulate this random variable.</p>
<div>
<h3>Related exercise sets:</h3>
<ol>
<li><a rel="nofollow" href="http://www.r-exercises.com/2017/07/08/hacking-statistics-exercises-part-2/" title="Hacking statistics or: How I Learned to Stop Worrying About Calculus and Love Stats Exercises (Part-2)" target="_blank">Hacking statistics or: How I Learned to Stop Worrying About Calculus and Love Stats Exercises (Part-2) </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/2017/07/01/hacking-statistics-exercises-part-1/" title="Hacking statistics or: How I Learned to Stop Worrying About Calculus and Love Stats Exercises (Part-1)" target="_blank">Hacking statistics or: How I Learned to Stop Worrying About Calculus and Love Stats Exercises (Part-1) </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/2017/02/13/data-science-for-doctors-part-3-distributions/" title="Data Science for Doctors – Part 3 : Distributions" target="_blank">Data Science for Doctors – Part 3 : Distributions </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/start-here-to-learn-r/" target="_blank">Explore all our (>1000) R exercises</a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/r-courses/" target="_blank">Find an R course using our R Course Finder</a> directory</li>
</ol>
</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://www.r-exercises.com/2017/07/18/hacking-statistics-exercises-part-3/?utm_source=rss&utm_medium=rss&utm_campaign=hacking-statistics-exercises-part-3"> R-exercises</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/fbpZsBjTMGY" height="1"></a></div></a></a>