Titis in the Mist…
Homework Solutions!
Every Saturday morning, at the same time, a primatologist goes and sits in the forest to listen for titi monkey calls, counting the number of calls they hear in a 2 hour window from 5am to 7am. Based on previous knowledge, she believes that the mean number of calls she will hear in that time is exactly 15. Let X represent the appropriate Poisson random variable of the number of calls heard in each monitoring session.
- What is the probability that she will hear more than 8 calls during any given session?
- What is the probability that she will hear no calls in a session?
- What is the probability that she will hear exactly 3 calls in a session?
- Plot the relevant Poisson mass function over the values in range 0 ≤ x ≤ 30.
- Simulate 104 results from this distribution (i.e., 2 years of Saturday monitoring sessions).
- Plot the simulated results using hist() and use xlim() to set the horizontal limits to be from 0 to 30. How does your histogram compare to the shape of the probability mass function you plotted above?
Getting Started
The first thing we should do is find areas of the previous modules that might help with this data analysis. There are a few hints in the questions… First off, we need to know about the Poisson distribution, which is in Module 08. Next, we need to know how to run a simulation, which we did in Module 07 when discussing how to bootstrap data.
With this info, we should be able to come up with proper code to answer these questions!
According to the setup, this is a Poisson distributed dataset, which makes sense: Poisson is often used to model open ended counts of independently occurring events (which means we’re assuming each titi call is independent of each other). It’s defined by a single parameter, λ, which is the mean number of occurrences of the event in a given interval. In this case, λ = 15, our session is a two-hour window from 5 to 7 am, and X is our presumed number of calls…
Now, we know the probability mass function for Poisson from Module 08, and that for a given number or range of x that:
> poisPMF <- function(x, l) {
+ ((l^x) * exp(-l))/(factorial(x))
+ }
where l
is standing in for λ.
We could use this to solve our problem set, but it would be much easier to use the built-in Poisson functions. We can access these by searching for help around one of these functions we learned about in Module 08, dpois
:
> `?`(dpois)
Note that for any distribution function (e.g., Poisson, Binomial, Beta, etc), there are four functions available. We’ll use Poisson here as an example:
dpois
: the probability density function, this will give you a point estimate of the probability for a given valuex
for the entered parameters (in this case, λ)ppois
: the probability distribution function, this will give you the cumulative probability for a given valuex
for the entered parameters.qpois
: the quantile function, this will give the value ofx
for which a particular cumulative probability is associated.rpois
: drawsn
random observations from a specified Poisson distribution.
Question 1
- What is the probability that she will hear more than 8 calls during any given session?
In this case, counting from 9 through infinity might be daunting, but remember that probabilities are bounded from 0 to 1, which means that the cumulative probability of 0 to 8 calls will be the complement of our desired solution. Let’s start by finding the probability of hearing 0 to 8 calls (or, x=8 for cumulative probability) using the ppois
function, which will calculate the cumulative probability:
> x <- 8
> l <- 15
> 1 - ppois(q = x, lambda = l) #subtracting from 1 to get the complement
## [1] 0.9625535
In a previous class, student Lara Hakam did it this way, which is a little more efficient:
> ppois(8, 15, lower.tail = F)
## [1] 0.9625535
Question 2
- What is the probability that she will hear no calls in a session?
In this case, we’ll use dpois
to get the probability of a single outcome (0, because she heard no calls) for λ = 15, rather than the cumulative probability.
> dpois(0, 15)
## [1] 3.059023e-07
Question 3
- What is the probability that she will hear exactly 3 calls in a session?
We’ll use the same method here, but instead of 0, we’ll enter 3:
> dpois(3, 15)
## [1] 0.0001720701
Question 4
- Plot the relevant Poisson mass function over the values in range 0 ≤ x ≤ 30.
Ok, so in this case, we’ll need to plot a range (0 through 30) over the Poisson mass function where λ is 15 (so we’ll use dpois
, rather than ppois
):
> x <- 0:30
> l = 15
> barplot(dpois(x, l))
Question 5
- Simulate 104 results from this distribution (i.e., 2 years of Saturday monitoring sessions).
As Lara also suggested in a previous class, we’ll use rpois
for this:
> rpois(104, 15)
## [1] 16 13 11 19 20 13 15 14 19 18 15 14 11 12 14 13 18 19 9 15 15 16 11 19 14
## [26] 15 14 12 13 14 7 15 15 12 12 16 20 14 23 14 11 14 18 20 23 12 12 29 13 16
## [51] 18 12 11 19 15 15 12 20 11 15 14 15 17 16 13 17 19 17 15 18 16 12 11 13 14
## [76] 20 10 13 8 19 20 20 9 18 16 16 17 17 9 13 11 10 11 10 17 16 19 16 16 18
## [101] 17 17 10 13
Question 6
- Plot the simulated results using hist() and use xlim() to set the horizontal limits to be from 0 to 30. How does your histogram compare to the shape of the probability mass function you plotted above?
We’ll nest our rpois
results in a histogram using the base R graphics, as suggested in a previous class by Christian Gagnon (which I’ll make pretty using the col
command):
> y <- rpois(104, 15)
> hist(y, xlim = c(0, 30), xlab = "Number of Calls", main = "Call Monitoring Histogram",
+ breaks = 30, col = "forestgreen")
As you can see, our histogram is roughly the same shape as the PMF, but not quite. Our random sampling doesn’t perfectly fit the ideal (or population) distribution, but with a large enough sample (maybe 10 years of Saturdays?), it would!
This homework was lovingly inspired by the wonderful, brilliant (and dearly departed) Brazilian primatologist Anand Dacier, based on work she published in the journal Biotropica (2011).