Click on the R icon
If no R icon, go to Start and then All Programs to find R
Every distribution that R handles has four functions. There is a root
name, for example, the root name for the normal distribution
is norm
. This root is prefixed by one of the letters
p
for "probability", the cumulative distribution function (c. d. f.)
q
for "quantile", the inverse c. d. f.
d
for "density", the density function (p. f. or p. d. f.)
r
for "random", a random variable having the specified distribution
For the binomial distribution,
these functions are
pbinom
,
qbinom
,
dbinom
, and
rbinom
.
For help, use the help function on one of the four functions above.
What arguments are needed to specify the parameters?
pbinom
is
the R function that calculates the c.d.f. of the binomial
distribution.
Dr. Dribble has a probability of .8 of making free throws each time he shoots.
Assume his shots are independent of each other.
Let X be the number of free throws made. X is has a Binomial(10, 0.8) distribution. What does this look like?
Let's generate a large number of Binomial(n,p) random variables and look at the histogram.
> set.seed(1) > bindat <- rbinom(n=10000, size=10, prob=0.8) > hist(bindat, breaks=seq(2, 10, 1), freq=F)We want to find the probability of making at least 8 out of 10 free throws.
Let's calculate P(X <= 7) when X is has the Binomial(10, 0.8) distribution using the pbinom function.
> pbinom(7, 10, 0.8)
Now, to answer the question: P(X >= 8) = 1 - P(X <= 7) = 1-0.3222005 = 0.6777995
How does this answer compare to the answer using Table 2 Appendix A?
Question: Suppose widgits produced at Acme Widgit Works have probability 0.005 of being defective. Suppose widgits are shipped in cartons containing 25 widgits. What is the probability that a randomly chosen carton contains no more than one defective widgit?
Question Rephrased: What is P(X <= 1) when X has the Bin(25, 0.005) distribution?
(For this example Heads is a success or 1)
for( index in values) { expressions }where the curly brackets are optional when only one expression is specified.
for() loops may also be used to construct data objects by using index as a subscript for the variable being created. In this case, the variable must be initiated outside the for() loop.
The program is:
nreps <- 10 temp <- rep(NA,nreps) set.seed(5) for (i in 1:nreps){ temp[i] <- sample(c(1,0), size=1, replace=T) }We actually do not need loops to perform 10 Bernoulli trials, we can
set.seed(5) temp2 <- sample(c(1,0), size=10, replace=T)How would you simulate a coin toss when the probability of success is 0.25?
Now write a loop that simulates Dr. Dribbles free throws and Y is the number
of successes for n=10, 20, 50, 100, and 500 free throw attempts.
Use the sample function.
Now use a R binom function!
Here is the code and output for 2 different loops in drdribble_sim.r
Here is a nice introduction to Random Number Generators
What is the seed in the above introduction?