<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/probability-functions-advanced/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.r-exercises.com/2017/09/14/probability-functions-advanced/"> 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: 150px) 100vw, 150px" src="https://i0.wp.com/www.r-exercises.com/wp-content/uploads/2017/08/HS_part7-6-150x150.png?resize=150%2C150" alt="" class="wp-image-7699" data-recalc-dims="1"></a>In this set of exercises, we are going to explore some applications of probability functions and how to plot some density functions. The package <code>MASS</code> will be used in this set.</p>
<p>Note: We are going to use random numbers functions and random processes functions in R such as <code>runif</code>. A problem with these functions is that every time you run them you will obtain a different value. To make your results reproducible, you can specify the value of the seed using <code>set.seed(‘any number’)</code> before calling a random function. (If you are not familiar with seeds, think of them as the tracking number of your random number process.) For this set of exercises, we will use <code>set.seed(1)</code>. Don’t forget to specify it before every exercise that includes random numbers.</p>
<p>Answers to the exercises are available <a rel="nofollow" href="http://r-exercises.com/2017/09/14/probability-functions-advanced-solutions/" target="_blank">here.</a> If you obtained a different (correct) answer than those listed on the solutions page, please feel free to post your answer as a comment on that page.</p>
<p><strong>Exercise 1</strong></p>
<p><strong>Generating dice rolls</strong> Using the functions <code>runif</code> and <code>round</code>, simulate the results of 100 dice rolls.</p>
<p><strong>Exercise 2</strong></p>
<p>Let’s assume that we want to simulate a game in which we throw an unfair coin (success probability is 0.48) 10 times and you win $10 every time the result is tails and lose $10 when the result is heads. Simulate this game1000 time using <code>rbinom</code>, and find the expected amount of money you will gain or lose in this game using the simulated values.</p>
<p><strong>Exercise 3</strong></p>
<p>Simulate an experiment of throwing one dice 30 times using the function <code>rmultinom</code>, and find out how many 6’s are in the simulated sample.</p>
<p><strong>Exercise 4</strong></p>
<p>Obtain a vector that shows how many 1’s, 2’s,….6’s were obtained in the previous simulation.</p>
<p><strong>Exercise 5</strong></p>
<p>Simulate normal distribution values. Imagine a population in which the average height is 1.70 m with a standard deviation of 0.1. Use <code>rnorm</code> to simulate the height of 1000 people and save it in an object called <code>heights</code>.</p>
<p>a) Plot the density of the simulated values.<br>
b) Generate 10000 values with the same parameters and plot the respective density function on top of the previous plot in red to differentiate it.</p>
<p>This plot will show you how much a sample with 10000 simulations approximate to the real normal distribution.</p>
<p><strong>Exercise 6</strong></p>
<p>Find the 90% interval of a population with mean = 1.70 and standard deviation = .1</p>
<p><strong>Exercise 7</strong></p>
<p>Simulate 100000 people with height (cm) and weight (kg) using the function <code>mvrnorm</code> with <code> mu = c(170, 60) </code> and<br>
<code> Sigma = matrix(c(10,17,17,100), nrow = 2)</code>, and save it in an object called <code>population</code>.</p>
<p>Apply the function <code>summary</code> to <code>population</code> to get an idea of the values created.</p>
<div>
<aside><img src="http://www.r-exercises.com/2017/09/14/probability-functions-advanced/"></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 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 8</strong></p>
<p><strong>Plotting bivariate distribution</strong>. Use the function <code>kde2d</code> to generate a two-dimensional kernel density of the matrix <code>population</code> and plot the values using <code>persp</code>.</p>
<p><strong>Exercise 9</strong></p>
<p><strong>Simulating with a Bayesian approach</strong>. Unlike the frequentist statistics approach, Bayesian statistics assume the parameters of a distribution are a random variable with its own distribution. Let’s simulate a poisson variable. <img></p>
<p>a) Simulate a gamma variable with shape = 20 and scale = 0.5<br>
b) Simulate using the previous value a poisson random variable</p>
<p><strong>Exercise 10</strong></p>
<p>Simulating one variable doesn’t make sense if you want to know the properties of a certain distribution. Repeat the previous simulation but create 100 poisson variables and plot the distribution.</p>
<div>
<h3>Related exercise sets:</h3>
<ol>
<li><a rel="nofollow" href="http://www.r-exercises.com/2017/08/31/probabilty-functions-intermediate/" title="Probability functions intermediate" target="_blank">Probability functions intermediate </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/2017/07/25/hacking-statistics-exercises-part-4/" title="Hacking statistics or: How I Learned to Stop Worrying About Calculus and Love Stats Exercises (Part-4)" target="_blank">Hacking statistics or: How I Learned to Stop Worrying About Calculus and Love Stats Exercises (Part-4) </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/09/14/probability-functions-advanced/"> 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/TZFrEj2s9Eg" height="1"></a></div>