R Tutorial Flipping coins in R
>> YOUR LINK HERE: ___ http://youtube.com/watch?v=W-u0LoYHjT4
Want to learn more? Take the full course at https://learn.datacamp.com/courses/fo... at your own pace. More than a video, you'll learn hands-on coding quickly apply skills to your daily work. • --- • Many DataCamp courses teach the process of statistical inference. That's the process where you have some observed data, and you use it to build an underlying model. This is essential in data analysis, but it's only part of a useful statistical understanding. Probability is the study of how data can be generated from a model. This serves as the foundation for inference, and understanding both of these directions will make you a better statistician. • I'm Dave Robinson, and I'll be your instructor for this course, where we'll be talking about one of the simplest models for generating random data: a coin flip. By exploring coin flipping with the R programming language, you'll learn the basic laws and methods of probability. • Each time I flip a fair coin, it has a 50% chance of being heads and a 50% chance of being tails. Before I look at this coin, it is a random variable. We'll call one case of simulating from this random variable, a draw . • If you don't have a coin handy, you can use R to simulate this random event. Specifically, you can use the rbinom() function. This is named because it's a random draw from a binomial distribution, which you'll learn about in a moment. • rbinom() takes three arguments: first is the number of random draws we're doing: a draw is a single outcome from a random variable. Second is the number of coins we're flipping on each draw, which is also just one. Third is the probability of a heads , which for a fair coin is 50%. • There are two possible outcomes of this function: 0 and 1. In this case we got a result of 1 - throughout this course, we're going to interpret a 1 as heads . (Recall that the starting bracket one bracket simply indicates that this is a vector in R: we can completely ignore it). • This is a random process, which means that if run the same line of code again, we might get 0, which we will interpret to mean tails. If we ran it a third time, we could get either 1, heads, or 0, tails, unpredictably. • In this course we're usually not going to be flipping one coin, we're going to be flipping multiple. It's a hassle to run the same line of code many times to flip a sequence of coins, so you can flip many in a row by changing the first argument. For example, here we flipped ten coins to get a series of draws. In this case the results were six ones, or heads , and four zeroes, or tails. • Let's run that again. This time, the ten flips resulted in only three heads, and seven tails. Each time we do a set of flips, we'll see a different outcome. • Right now each draw has one coin flip. Rather than counting the heads in this way, we can take just one draw, and use the second argument to specify how many coins should be flipped within each. At this point the function will simply return the number of heads. Here, we did one random draw and got a result of 4. • We could also say that we want to do multiple draws, and flip multiple coins within each, by setting both of the arguments. In this case, we're saying we want to perform 10 draws, and flip ten coins within each draw. This shows us that when we flip 10 coins, the resulting number of heads might be 3, 6, 5, or so on. Notice that the resulting number of heads tends to be between 3 and 8, and that the most common result is 5: that will be relevant later. • Until now we've been talking about a coin that has a 50% chance of heads and a 50% chance of tails. But not all random events have an equal likelihood of happening. In this course we'll often work with biased coins: ones that are more likely to result in heads or in tails. We can control this probability by setting the third parameter. • When the third parameter is point-8, that means each flip has an 80% chance of resulting in heads. When we flip one of these unfair coins ten times, we notice the number of heads tends to be around seven to nine. When the third parameter is point-2, each flip has only a 20% chance of being heads, and out of ten flips, each of our draws has around 1 to 3 heads. • Consider this process of flipping a number of biased coins and counting the resulting heads. Each outcome, X, is a random variable that follows a binomial distribution. A probability distribution is a mathematical description of the possible outcomes of a random variable. We describe the binomial distribution based on two parameters: the size, or number of flips, and p, the probability that each is heads. • In this course, you'll learn to reason and make predictions about this distribution, based on these two parameters. • • #DataCamp #RTutorial #Foundations #Probability
#############################
