Sunday, February 1, 2015

R4. Binomial Distribution in R

The function rbinom gives the binomial distribution


The test distribution is based on 1000 trials with 100 in each trial and success probability of 0.5.


A distribution based on the uniform distribution is tested with that based on the rbinom function.

# ex4.R
num_trials <- 1000
each_trial <- 100
p <- 0.5
sig <- numeric(num_trials)
sig1 <- rbinom(size = each_trial, prob = p, n = num_trials)
for (t in 1:num_trials) {
  tr <- runif(each_trial)
  sig[t] <- sum(tr<=p)
}
hist(sig, breaks = 20, xlab = 'Number of Successes', 
     ylab = 'Frequency', main = 'Binomial Distribution', col='red')
hist(sig1, breaks = 20, , add = T, col=rgb(0, 1, 0, 0.5))

Output:

No comments:

Post a Comment