Lab No. 2

Introduction to R
Renata Retkute
Department of Mathematics
2008
Contents
2 Lab: Binomial and Poisson distributions
2.1 The Binomial distribution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.2 The Poisson distribution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.3 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
17
18
18
20
22
Introduction to R
2
2.1
18
Lab: Binomial and Poisson distributions
The Binomial distribution
The general form of the binomial distribution with probability of success p and number of items n, is
given by
n x
f (x) =
p (1 − p)n−x
x
The mean of the binomial distribution is np and the variance is np(1 − p).
To visualize the distribution for particular values of n and p, follow the steps below:
> p <- 0.1
> n <- 4
> x <- 0:n
> px <- choose(n,x)*p^x*(1-p)^(n-x)
> plot(0:n, px, type=’h’, lwd=10)
The three distribution functions available for the binomial in R (density, cumulative probability
and random generation) are those:
dbinom(x, n, p)
pbinom(x, n, p)
rbinom(N, n, p)
where N is a number of random variables we want to simulate.
We want to plot p.d.f for three binomial distributions: X ∼ B(10, 0.25), X ∼ B(10, 0.50) and X ∼
B(10, 0.75). First, split the graphics window into three equal areas using the command par(mfrow
= c(1,3)) .Then use the command dbinom to calculate the p.d.f.s as shown:
Introduction to R
19
> par(mfrow=c(1,3))
> plot(0:10, dbinom(0:10,10,0.25), type=’h’, lwd=4)
> plot(0:10, dbinom(0:10,10,0.5), type=’h’, lwd=4)
> plot(0:10, dbinom(0:10,10,0.75), type=’h’, lwd=4)
Similarly, the command pbinom calculates the cumulative distribution function for the binomial distribution:
> par(mfrow=c(1,3))
> plot(0:10, pbinom(0:10,10,0.25), type=’S’)
> plot(0:10, pbinom(0:10,10,0.5), type=’S’)
> plot(0:10, pbinom(0:10,10,0.75), type=’S’)
To calculate probabilities for X ∼ B(n, p), use following table:
20
Introduction to R
Probability
P (X = x)
P (X ≤ x)
P (X < x)
P (X ≥ x)
P (X > x)
Command
dbinom(x,n,p)
pbinom(x,n,p)
pbinom(x-1,n,p)
1 - pbinom(x-1,n,p)
1 - pbinom(x,n,p)
To generate 10 realisations for r.v. X ∼ B(10, 0.2) and calculate mean and variance we use:
> data10 <- rbinom(10,10,0.2)
> data10
[1] 1 1 0 2 1 3 4 3 1 1
> mean(data10)
[1] 1.7
> var(data10)
[1] 1.566667
2.2
The Poisson distribution
The density function of the Poisson distribution shows the probability of obtaining a count of x when
the averaged count per unit is λ
e−λ λx
f (x) =
x!
Mean and variance of r.v. X ∼ P (λ) are equal to λ.
Functions for the density, cumulative distribution and random number generation of the Poisson distribution in R are obtained using following commands:
> dpois(x, λ)
> ppois(x, λ)
> rpois(n, λ)
We want to plot three Poisson distributions X ∼ P (5), X ∼ P (10) and X ∼ P (15).
> par(mfrow=c(1,3))
> plot(0:30, dpois(0:30,5), type=’h’, lwd=2)
> plot(0:30, dpois(0:30,10), type=’h’, lwd=2)
> plot(0:30, dpois(0:30,15), type=’h’, lwd=2)
21
Introduction to R
Similarly, the command ppois calculates the cumulative distribution function for the binomial distribution:
> par(mfrow=c(1,3))
> plot(0:30, ppois(0:30,5), type=’S’)
> plot(0:30, ppois(0:30,10), type=’S’)
> plot(0:30, ppois(0:30,15), type=’S’)
We can calculate probabilities for X ∼ P (λ), use following table:
Probability
P (X = x)
P (X ≤ x)
P (X < x)
P (X ≥ x)
P (X > x)
Command
dpois(x, λ)
ppois(x,λ)
ppois(x-1,λ)
1 - ppois(x-1, λ)
1 - ppois(x, λ)
To generate 10 realisations for r.v. X ∼ P (2.5) and calculate mean and variance we use:
Introduction to R
22
> data10 <- rpois(10,2.5)
> data10
[1] 4 3 6 3 1 5 1 5 5 4
> mean(data10)
[1] 3.7
> var(data10)
[1] 2.9
2.3
Exercises
1. On the basis of past experience, the probability that a certain electrical component will be
satisfactory is 0.98. The components are sampled item by item from continuous production. In
a sample of five components, what are the probabilities of finding (a) zero, (b) exactly one, (c)
exactly two, (d) two or more defectives?
Answer: 0.9039208; 0.09223682; 0.003764768; 0.003842387.
2. Biased coin has probability of tossing a head equal to 0.99. If we toss this coin 1,2,..., 499, 500
times, draw a graph of probability that outcome of these tosses are all heads.
3. In a survey of 15 manufacturing firms, the number of firms that use LIFO (a last-in first-out
accounting procedure for inventory) is a binomial random variable x with n=15 and p=0.2. a)
What is the probability that five or fewer firms will be found to use LIFO? (b) Is it unlikely that
more than 10 firms will be found to use LIFO?
Answer: 0.9389486; 0.0001132257.
4. The average number of collisions occurring in a week during the summer months at a particular
intersection is 2. Assume that the requirements of the Poisson distribution are satisfied.
a) What is the probability of no collisions in any particular week?
b) What is the probability that there will be exactly one collision in a week?
c) What is the probability of exactly two collisions in a week?
d) What is the probability of finding not more than two collisions in a week?
e) What is the probability of finding more than two collisions in a week?
f) What is the probability of exactly two collisions in a particular two-week interval?
Answer: 0.1353353; 0.2706706; 0.2706706; 0.6766764; 0.3233236; 0.1465251.
5. The number of meteors found by a radar system in any 30-second interval under specified conditions averages 1.81. Assume the meteors appear randomly and independently.
a) What is the probability that no meteors are found in a one-minute interval?
b) What is the probability of observing at least five but not more than eight meteors in two
minutes of observation?
Answer: 0.02678268; 0.5451062.
23
Introduction to R
6. A library employee shelves a large number of books every day. The average number of books
misshelved per day is estimated over a long period to be 2.5. Calculate the probability that
between five and fifteen books (including both limits) are misshelved in a four-day period.
Answer: 0.922007.
7. If r.v. X is distributed according to Poisson distribution with parameter λ = 1, then E[X] = 1.
Perform 5 simulations for each number of samples, taken from Poisson distribution, calculate
mean and write calculated value in the following table:
n
10
100
1000
10000
Simulation 1
Simulation 2
Simulation 3
Simulation 4
Simulation 5