Search code examples
rsimulateprobability-density

sample() function in R


My question is quite simple, I'm trying to simulate 500 draws from any distribution using sample().

Let's take binomial distribution B(10,0.5) for example. Using the sample() function in R.

This is what I did:

draw = 1:500
data = sample(x=draw, size=10, replace=TRUE, prob=rep(0.5, each=500))

However whenever I draw the historgram, it looks like it's random and doesn't have a binomial distribution. What am I doing wrong?

Note: I know there is rbinom() function in r that does this. I am trying to understand how the sample() function works.


Solution

  • sample(x = c(1,0),size = 10,replace=TRUE,prob = c(0.5,0.5))
    

    You may want to histogram the sum of this vector generated multiple times to see your binomial distribution.

    draws=c()
    for(i in 1:500){
      draws=c(draws,sum(sample(x = c(1,0),size = 10,replace=TRUE,prob = c(0.5,0.5))))
    }
    hist(draws)
    

    In this example sample is returning 10 (size = 10) samples of the values 1 or 0 (x = c(1,0)), with equal probability for each (prob = c(0.5,0.5)). replace=TRUE just means that either item can be draw more than once. These 1's and 0's are the results of 10 bernoulli trials with probability 0.5. The binomial distribution is the probability distribution of the number of successes (1's) in a series of n Bernoulli trials each with probability p. So (n=10 and p=0.5). Calling sample once gives the 10 draws, summing that vector gives a draw from the binomial. We take 500 draws from that binomial distribution and draw a histogram.