<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-7/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.r-exercises.com/2017/08/26/hacking-statistics-exercises-part-7/"> 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://i2.wp.com/www.r-exercises.com/wp-content/uploads/2017/08/HS_part7-6.png" target="_blank"><img sizes="(max-width: 300px) 100vw, 300px" src="https://i0.wp.com/www.r-exercises.com/wp-content/uploads/2017/08/HS_part7-6-300x300.png?resize=300%2C300" alt="" class="wp-image-7699" 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>Until now, we used random variable simulation and bootstrapping to test hypothesis and compute statistics of a single sample. In today’s set, we’ll learn how to use permutation to test hypothesis about two different samples and how to adapt bootstrapping to this situation.</p>
<p>Answers to the exercises are available <a rel="nofollow" href="http://www.r-exercises.com/2017/08/26/hacking-statistics-solutions-part-7/" target="_blank">here</a>.</p>
<p><strong>Exercise 1</strong></p>
<ol>
<li>Generate 500 points from a beta distribution of parameter a=2 and b=1.5, then store the result in a vector named beta1.</li>
<li>Generate 500 points from the same distribution and store those points in a vector named beta2.</li>
<li>Concatenate both vectors to create a vector called beta.data.</li>
<li>Plot the ecdf of beta1 and beta2.</li>
<li>Sample 500 points from beta.data and plot the ecdf of this sample. Repeat this process 5 times.</li>
<li>Does all those samples share the same distribution and if the answer is yes, what is the distribution?</li>
</ol>
<p><strong>Exercise 2</strong><br>
When we test an hypothesis, we suppose that this hypothesis is true, we simulate what would happen if that’s the case and if our initial observation happen less that α percent of the time we reject the hypothesis. Now, from the first exercise, we know that if two samples share the same distribution, we can assume that any sample drawn from those samples will follow the same distribution. In particular, if we shuffle the observations from a sample of size n1 and those of a sample of size n2, shuffle them and draw two new samples of size n1 and n2, they all should have a similar CDF. We can use this fact to test the hypothesis that two samples have the same distribution. This is process is called a permutation test.</p>
<p>Load this <a rel="nofollow" href="http://www.r-exercises.com/wp-content/uploads/2017/08/data.ex2_.csv" target="_blank">dataset</a> where each column represents a variable and we want to know if they are identically distributed. Each exercise below follow a step of a permutation test.</p>
<ol>
<li>What are the null and alternative hypotheses for this test?</li>
<li>Concatenate both samples into a new vector called data.ex.2.</li>
<li>Write a function that take data.ex.2 and the size of both sample as arguments, create a temporary vector by permuting data.ex.2 and return two new samples. The first sample has the same number of observations than the first column of the dataset, the second is made from the rest of the observations. Name this function permutation.sample (we will used it in the next exercise.) Why do we want the function to return samples of those size?</li>
<li>Plot the ECDF of both initial variables in black.</li>
<li>Use the function permutation.sample 100 times to generate permuted samples, then compute the ECDF of those samples and add the plot of those curve to the previous plot. Use the color red for the first batch of samples and green for the second batch.</li>
<li>By looking at the plot, can you tell if the null hypothesis is true?</li>
</ol>
<p><strong>Exercise 3</strong><br>
A business analyst think that the daily returns of the apple stocks follow a normal distribution with mean of 0 and a standard deviation of 0.1. Use this <a rel="nofollow" href="http://www.r-exercises.com/wp-content/uploads/2017/08/data.apple_.csv" target="_blank">dataset</a> of the daily return of those stocks for the last 10 years to test this hypothesis.</p>
<div>
<aside><img src="http://www.r-exercises.com/2017/08/26/hacking-statistics-exercises-part-7/"></aside>
<div><strong>Learn more</strong> about probability functions in the online course <a rel="nofollow" href="http://www.r-exercises.com/product/statistics-with-r-advanced-level/" target="_blank">Statistics with R – Advanced Level</a>. In this course you will learn how to:
<ul>
<li>Work with about different binomial and logistic regression techniques</li>
<li>Know how to compare regression models and choose the right fit</li>
<li>And much more</li>
</ul>
</div>
</div>
<p><strong>Exercise 4</strong><br>
Permutation test can help us verify if two samples come from the same distribution, but if this is true, we can conclude that both sample share the same statistics. As a consequence permutation test can also be used to test if statistic of two sample are the same. One really useful application of this is to test if two mean are the same or significantly different (as you have probably realized by now, statistician are obsessed with mean and love to spend time studying it!). In this situation, the question is to determine if the difference of mean in two sample are random or a consequence of a difference of distribution.</p>
<p>You should be quite familiar with tests by now, so how would you proceed to do a permutation test to verify if two means are equals? Used that process to test the equality of the mean of both sample in this <a rel="nofollow" href="http://www.r-exercises.com/wp-content/uploads/2017/08/data.ex4_.csv" target="_blank">dataset</a>.</p>
<p><strong>Exercise 5</strong><br>
Looking at the average annual wage of the United States and Switzerland both country have relatively the same level of wealth since those statistics are of 60154 and 60124 US dollar respectively. In <a rel="nofollow" href="http://www.r-exercises.com/wp-content/uploads/2017/08/data.average.wage-Copie.csv" target="_blank">this dataset</a>, you will find simulated annual wage from citizen of both countries. Test the hypothesis that both the American and the Swiss have the same average annual wage based on those samples at a level of 5%.</p>
<p><strong>Exercise 6</strong><br>
To test if two samples from different distribution have the same statistics, we cannot use the permutation test: we instead will use bootstrapping. To test if two sample as the same mean, for example, you should follow those steps:</p>
<ol>
<li>Formulate a null and an alternative hypothesis.</li>
<li>Set a significance level.</li>
<li>Compute the difference of mean of both samples. This will be the reference value we will use to compute the p-value.</li>
<li>Concatenate both samples and compute the mean of this new dataset.</li>
<li>Shift both samples so that they share the mean of the concatenated dataset.</li>
<li>Use bootstrap to generate an estimate of the mean of both shifted samples.</li>
<li>Compute the difference of both means.</li>
<li>Repeat the last two steps at least 1000 times.</li>
<li>Compute the p-value and draw a conclusion.</li>
</ol>
<p>Use the dataset from last exercise to see if the USA and Switzerland have the same average wage at a level of 5%.</p>
<p><strong>Exercise 7</strong><br>
Test the hypothesis that both samples in <a rel="nofollow" href="http://www.r-exercises.com/wp-content/uploads/2017/08/data.ex7_.csv" target="_blank">this dataset</a> have the same mean.</p>
<p><strong>Exercise 8</strong><br>
R have functions that use analytic methods to test if two samples have an equal mean.</p>
<ol>
<li>Use the <a rel="nofollow" href="https://stat.ethz.ch/R-manual/R-devel/library/stats/html/t.test.html" target="_blank">t.test()</a> function to test the equality of the mean of the samples of the last exercise.</li>
<li>Use this function to test the hypothesis that the average wage in the US are bigger than in Switzerland.</li>
</ol>
<p><strong>Exercise 9</strong><br>
The <a rel="nofollow" href="http://www.r-exercises.com/wp-content/uploads/2017/08/data.cluster.csv" target="_blank">globular cluster luminosity dataset</a> list measurement about the luminosity of cluster of stars in different region of the milky way galaxy and the Andromeda galaxy. Test the hypothesis that the average luminosity in both galaxy have a difference of 24,78.</p>
<p><strong>Exercise 10</strong><br>
A company that mold aluminum for auto parts has bought a smaller company to increase the amount of parts they can produce each year. In their factory, the smaller company used the standard equipment, but used a different factory layout, had a different supply line and managed their employees work schedules in a completely different manner that their new parent company. Before changing the company culture, the engineer in the parent company are interested to know which of the approach is the more effective. To do so they measure the time it took to make an auto part in each factory, 150 times and created <a rel="nofollow" href="http://www.r-exercises.com/wp-content/uploads/2017/08/data.ex10.csv" target="_blank">this dataset</a> where the first column represent the sample of the small factory.</p>
<ol>
<li>Does the average time it takes to make a part is the same in both factory?</li>
<li>Does the production time follow the same distribution in both factory?</li>
<li>If the engineer want to minimize the percentage of part that take more than one hour to be made, which setup they should implement in both their factory: the one of the parent company or the one of the smaller company?</li>
</ol>
<div>
<h3>Related exercise sets:</h3>
<ol>
<li><a rel="nofollow" href="http://www.r-exercises.com/2017/08/13/hacking-statistics-exercises-part-6/" title="Hacking statistics or: How I Learned to Stop Worrying About Calculus and Love Stats Exercises (Part-6)" target="_blank">Hacking statistics or: How I Learned to Stop Worrying About Calculus and Love Stats Exercises (Part-6) </a></li>
<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/08/01/hacking-statistics-exercises-part-5/" title="Hacking statistics or: How I Learned to Stop Worrying About Calculus and Love Stats Exercises (Part-5)" target="_blank">Hacking statistics or: How I Learned to Stop Worrying About Calculus and Love Stats Exercises (Part-5) </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/08/26/hacking-statistics-exercises-part-7/"> 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/2W4MeuIfW_w" height="1"></a></div>