We are most interested in the r*** functions in this lesson, but I encourage you to explore the others on your own.Ī binomial random variable represents the number of ‘successes’ (heads) in a given number of independent ‘trials’ (coin flips). ?rbinomĮach probability distribution in R has an r*** function (for “random”), a d*** function (for “density”), a p*** (for “probability”), and q*** (for “quantile”). Pull up the documentation for rbinom() using ?rbinom. sum(flips) # 70Ī coin flip is a binary outcome (0 or 1) and we are performing 100 independent trials (coin flips), so we can use rbinom() to simulate a binomial random variable. Count the actual number of 1s contained in flips using the sum() function. Since we set the probability of landing heads on any given flip to be 0.7, we’d expect approximately 70 of our coin flips to have the value 1. Assign the result to a new variable called flips. Since the coin is unfair, we must attach specific probabilities to the values 0 (tails) and 1 (heads) with a fourth argument, prob = c(0.3, 0.7). Use sample() to draw a sample of size 100 from the vector c(0,1), with replacement. Let the value 0 represent tails and the value 1 represent heads. This particular coin has a 0.3 probability of landing ‘tails’ and a 0.7 probability of landing ‘heads’. Now, suppose we want to simulate 100 flips of an unfair two-sided coin. ![]() When the ‘size’ argument to sample() is not specified, R takes a sample equal in size to the vector from which you are sampling. This is identical to taking a sample of size 26 from LETTERS, without replacement. For example, try sample(LETTERS) to permute all 26 letters of the English alphabet. The sample() function can also be used to permute, or rearrange, the elements of a vector. LETTERS is a predefined variable in R containing a vector of all 26 letters of the English alphabet. Since the last command sampled without replacement, no number appears more than once in the output. To sample without replacement, simply leave off the ‘replace’ argument. Now sample 10 numbers between 1 and 20, WITHOUT replacement. This is what we want here, since what you roll on one die shouldn’t affect what you roll on any of the others. Sampling with replacement simply means that each number is “replaced” after it is selected, so that the same number can show up more than once. Sample(1:6, 4, replace = TRUE) instructs R to randomly select four numbers between 1 and 6, WITH replacement. ![]() (The probability of rolling the exact same result is (1/6)^4 = 0.00077, which is pretty small!) sample(1:6, 4, replace = TRUE) # 5 4 6 4 Now repeat the command to see how your result differs. Let’s simulate rolling four six-sided dice: sample(1:6, 4, replace = TRUE). Use ?sample to pull up the documentation. The first function we’ll use to generate random numbers is sample(). ![]() Even if you have no prior experience with these concepts, you should be able to complete the lesson and understand the main ideas. This lesson assumes familiarity with a few common probability distributions, but these topics will only be discussed with respect to random number generation. One of the great advantages of using a statistical programming language like R is its vast collection of tools for simulating random numbers.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |