10.07.2015 Views

Using R for Introductory Statistics : John Verzani

Using R for Introductory Statistics : John Verzani

Using R for Introductory Statistics : John Verzani

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Describing populations 147In R the family name <strong>for</strong> the binomial is binom, and the parameters are labeled size=<strong>for</strong> n and prob= <strong>for</strong> p.■ Example 5.5: Tossing ten coins Toss a coin ten times. Let X be the number ofheads. If the coin is fair, X has a Binomial(10,1/2) distribution.The probability that X=5 can be found directly from the distribution with the choose()function:> choose(10,5) * (1/2)^5 * (1/2)^(10–5)[1] 0.2461This work is better done using the “d” function, dbinom():> dbinom(5, size=10, prob=1/2)[1] 0.2461The probability that there are six or fewer heads, P(X≤6)=∑ k≤6 P(X=k), can be giveneither of these two ways:> sum(dbinom(0:6,size=10,prob=l/2))[1] 0.8281> pbinom(6,size=10,p=1/2)[1] 0.8281If we wanted the probability of seven or more heads, we could answer usingP(X≥7)=1−P(X≤6), or using the extra argument lower .tail=FALSE.This returns P(X>k) rather than P(X≤k).> sum(dbinom(7:10,size=10,prob=l/2))[1] 0.1719> pbinom(6,size=10,p=1/2)[1] 0.1719> pbinom(6,size=10,p=1/2, lower.tail=FALSE) # k=6 not7![1] 0.1719A spike plot (Figure 5.4) of the distribution can be produced using dbinom():> heights=dbinom(0:10,size=10,prob=1/2)> plot(0:10, heights, type="h",+ main="Spike plot of X", xlab="k", ylab="p.d.f.")> points(0:10, heights, pch=16,cex=2)■ Example 5.6: Binomial model <strong>for</strong> a public-opinion poll In a public-opinion poll, theproportion of “yes” respondents is used to make inferences about the populationproportion. If the respondents are chosen by sampling with replacement from thepopulation, and the “yes” responses are coded by a 1 and the “no”

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!