<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-4/"></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/25/hacking-statistics-exercises-part-4/?utm_source=rss&utm_medium=rss&utm_campaign=hacking-statistics-exercises-part-4"> 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/07/HS_part4-7.png" target="_blank"><img sizes="(max-width: 150px) 100vw, 150px" src="https://i2.wp.com/www.r-exercises.com/wp-content/uploads/2017/07/HS_part4-7-150x150.png?resize=150%2C150" alt="" class="wp-image-7168" 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, in this series of exercise sets, we have used only continuous probability distributions, which are functions defined on all the real numbers on a certain interval. As a consequence, random variable who have those distributions can assume an infinity of values. However, a lot of random situations only have a finite amount of possible outcome and using a continuous probability distributions to analyze them is not really useful. In today set, we’ll introduce the concept of discrete probability functions, which can be used in those situations and some examples of problems in which they can be used.</p>
<p>Answers to the exercises are available <a rel="nofollow" href="http://www.r-exercises.com/2017/07/25/hacking-statistics-solutions-part-4/" target="_blank">here</a>.</p>
<p><strong>Exercise 1</strong><br>
Just as continuous probability distributions are characterized by a <a rel="nofollow" href="http://www.r-exercises.com/2017/07/01/hacking-statistics-exercises-part-1/" target="_blank">probability density function</a> discrete probability functions are characterized by a probability mass function which gives the probability that a random variable is equal to one value.</p>
<p>The first probability mass function we will use today is the binomial distribution, which is used to simulate n iterations of a random process who can either result in a success, with a probability of p, or a failure, with a probability of (1-p). Basically, if you want to simulate something like a coins flip, the binomial distribution is the tool you need.</p>
<p>Suppose you roll a 20 sided dice 200 times and you want to know the probability to get a 20 exactly five times on your rolls. Use the <code>dbinom(n, size, prob)</code> function to compute this probability.</p>
<p><strong>Exercise 2</strong><br>
For the binomial distribution, the individual events are independents, meaning that the probability of realization of two events can be calculated by adding the probability of realization of both event. This principle can be generalize to any number of events. For example, the probability of getting three tails or less when you flip a coins 10 time is equal to the probability of getting 1 tails plus the probability of getting 2 tails plus the probability of getting 3 tails.</p>
<p>Knowing this, use the <code>dbinom()</code> function to compute the probability of getting six correct responses at a test made of 10 questions which have true or false for answer if you answer randomly. Then, use the <code>pbinom()</code> function to compute the cumulative probability function of the binomial distribution in that situation.</p>
<p><strong>Exercise 3</strong><br>
Another consequence of the independence of events is that if we know the probability of realization of a set of events we can compute the probability of realization of one of his subset by subtracting the probability of the unwanted event. For example, the probability of getting two or three tails when you flip a coins 10 time is equal to the probability of getting at least 3 tails minus the probability of getting 1 tails.</p>
<p>Knowing this, compute the probability of getting 6 or more correct answer on the test described in the previous exercise.</p>
<div>
<aside><img src="http://www.r-exercises.com/2017/07/25/hacking-statistics-exercises-part-4/?utm_source=rss&utm_medium=rss&utm_campaign=hacking-statistics-exercises-part-4"></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>
Let’s say that in an experiment a success is defined as getting a 1 if you roll a 20 sided die. Use the <code>barplot()</code> function to represent the probability of getting from 0 to 10 success if you roll the die 10 times. What happened to the barplot if you roll a 10 sided die instead? If you roll a 3 sided die?</p>
<p><strong>Exercise 5</strong><br>
Another discrete probability distribution close to the binomial distribution is the Poisson distribution, which give the probability of a number of events to occur during a fixed amount of time if we know the average rate of his occurrence. For example, we could use this distribution to estimate the amount of visitor who goes on a website if we know the average number of visitor per second. In this case, we must assume two things: first that the website has visitor from around the world since the rate of visitor must be constant around the day and two that when a visitor is coming on the site he is not influenced by the last visitor since a process can be expressed by the Poisson distribution if the events are independent from each other.</p>
<p>Use the <code>dpois()</code> function to estimate the probability of having 85 visitors on a website in the next hour if in average 80 individual connect on the site per hour. What is the probability of getting 2000 unique visitors on the website in a day?</p>
<p><strong>Exercise 6</strong><br>
Poisson distribution can be also used to compute the probability of an event occurring in an amount of space, as long as the unit of the average rate is compatible with the unit of measure of the space you use. Suppose that a fishing boat catch 1/2 ton of fish when his net goes through 5 squares kilometers of sea. If the boat combed 20 square kilometer, what is the probability that they catch 5 tons of fish?</p>
<p><strong>Exercise 7</strong><br>
Until now, we used the Poisson distribution to compute the probability of observing precisely n occurrences of an event. In practice, we are often interested in knowing the probability that an event occur n times or less. To do so we can use the <code>ppois()</code> function to compute the cumulative Poisson distribution. If we are interested in knowing what is the probability of observing strictly more than n occurrences, we can use this function and set the parameter <code>lower</code> to <code>FALSE</code>.</p>
<p>In the situation of exercise 5, what is the probability that the boat caught 5 tons of fish or less? What is the probability that the caught more than 5 tons of fish?</p>
<p>Note that, just as in a binomial experiment, the events in a Poisson process are independant, so you can add or subtract probability of event to compute the probability of a particular set of events.<br>
<strong>Exercise 8</strong><br>
Draw the Poisson distribution for average rate of 1,3,5 and 10.</p>
<p><strong>Exercise 9</strong><br>
The last discrete probability distribution we will use today is the negative binomial distribution which give the probability of observing a certain number of success before observing a fixed number of failures. For example, imagine that a professional football player will retire at the end of the season. This player has scored 495 goals in his career and would really want to meet the 500 goal mark before retiring. If he is set to play 8 games until the end of the season and score one goal every three games in average, we can use the negative binomial distribution to compute the probability that he will meet his goal on his last game, supposing that he won’t score more than one goal per game.</p>
<p>Use the <code>dnbinom()</code> function to compute this probability. In this case, the number of success is 5, the probability of success is 1/3 and the number of failures is 3.</p>
<p><strong>Exercise 10</strong><br>
Like for the Poisson distribution, R give us the option to compute the cumulative negative binomial distribution with the function <code>pnbinom()</code>. Again, the <code>lower.tail</code> parameter than give you the option to compute the probability of realizing more than n success if he is set to TRUE.</p>
<p>In the situation of the last exercise, what is the probability that the football player will score at most 5 goals in before the end of his career.</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/2016/03/23/lets-begin-with-something-sample/" title="Lets Begin with something sample" target="_blank">Lets Begin with something sample </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/25/hacking-statistics-exercises-part-4/?utm_source=rss&utm_medium=rss&utm_campaign=hacking-statistics-exercises-part-4"> 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/qT_MceTh2c0" height="1"></a></div>