Bayesian First Aid: Poisson Test
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
As the normal distribution is sort of the default choice when modeling continuous data (but not necessarily the best choice), the Poisson distribution is the default when modeling counts of events. Indeed, when all you know is the number of events during a certain period it is hard to think of any other distribution, whether you are modeling the number of deaths in the Prussian army due to horse kicks or the numer of goals scored in a football game. Like the t.test
in R there is also a poisson.test
that takes one or two samples of counts and spits out a p-value. But what if you have some counts, but don’t significantly feel like testing a null hypothesis? Stay tuned!
Bayesian First Aid is an attempt at implementing reasonable Bayesian alternatives to the classical hypothesis tests in R. For the rationale behind Bayesian First Aid see the original announcement. The development of Bayesian First Aid can be followed on GitHub. Bayesian First Aid is a work in progress and I’m grateful for any suggestion on how to improve it!
The Model
The original poisson.test
function that comes with R is rather limited, and that makes it fairly simple to construct the Bayesian alternative. However, at first sight poisson.test
may look more limited than it actually is. The one sample version just takes one count of events $x$ and the number of periods $T$ during which the number of events were counted. If your ice cream truck sold 14 ice creams during one day you would call the function like poisson.test(x = 14, T = 1)
. This seems limited, what if you have a number of counts, say you sell ice cream during a whole week, what to do then? The trick here is that you can add up the counts and the number of time periods and this will be perfectly fine. The code below will still give you an estimate for the underlying rate of ice cream sales per day:
ice_cream_sales = c(14, 16, 9, 18, 10, 6, 13) poisson.test(x = sum(ice_cream_sales), T = length(ice_cream_sales))
Note that this only works if the counts are well modeled by the same Poisson distribution. If the ice cream sales are much higher on the weekends, adding up the counts might not be a good idea. poisson.test
is also limited in that it can only handle two counts; you can compare the performance of your ice cream truck with just one competitor’s, no more. As the Bayesian alternative accepts the same input as poisson.test
it inherits some of it’s limitations (but it can easily be extended, read on!). The model for the Bayesian First Aid alternative to the one sample possion.test
is:
Here $x$ is again the count of events, $T$ is the number of periods, and $lambda$ is the parameter of interest, the underlying rate at which the events occur. In the two sample case the one sample model is just separately fitted to each sample.
As $x$ is assumed to be Poisson distributed, all that is required to turn this into a fully Bayesian model is a prior on $lambda$. In the the literature there are two common recommendations for an objective prior for the rate of a Poisson distribution. The first one is $lambda propto 1 / lambda$ which is the same as $log(lambda) propto text{const}$ and is proposed, for example, by Villegas (1977). While it can be argued that this prior is as non-informative as possible, it is problematic in that it will result in an improper posterior when the number of events is zero ($x = 0$). I feel that seeing zero events should tell the model something and, at least, not cause it to blow up. The second proposal is Jeffreys prior $lambda propto 1 / sqrt{lambda}$, (as proposed by the great BUGS Book) which has a slight positive bias compared to the former prior but handles counts of zero just fine. The difference between these two priors is very small and will only matter when you have very few counts. Therefore Bayesian First Aid alternative to poisson.test
uses Jeffreys prior.
So if the model uses Jeffreys prior, what is the $lambda sim text{Gamma}(0.5, 0.00001)$ doing in the model definition? Well, the computational framework underlying Bayesian First Aid is JAGS and in JAGS you build your model using probability distributions. The Jeffreys prior is not a proper probability distribution but it turns out that it can be reasonably well approximated by ${Gamma}(0.5, epsilon)$ with $epsilon rightarrow 0$ (in the same way as $lambda propto 1 / lambda$ can be approximated by ${Gamma}(epsilon, epsilon)$ with $epsilon rightarrow 0$).
The bayes.poisson.test
Function
The bayes.poisson.test
function accepts the same arguments as the original poisson.test
function, you can give it one or two counts of events. If you just ran poisson.test(x=14, T=1)
, prepending bayes.
runs the Bayesian First Aid alternative and prints out a summary of the model result (like bayes.poisson.test(x=14, T=1)
). By saving the output, for example, like fit <- bayes.poisson.test(x=14, T=1)
you can inspect it further using plot(fit)
, summary(fit)
and diagnostics(fit)
.
To demonstrate the use of bayes.poisson.test
I will use data from Boice and Monson (1977) on the number of women diagnosed with breast cancer in one group of 1,047 tuberculosis patients that had received on average 102 X-ray exams and one group of 717 tuberculosis patients whose treatment had not required a large number of X-ray exams. Here is the full data set:
Here WY stand for woman-years (as if woman-years would be different from man-years, or person-years…). While the data is from a relatively old article we are going to replicate a more recent reanalysis of that data from the article Testing the Ratio of Two Poisson Rates by Gu et al. (2008). They tested the alternative hypothesis that the rate of breast cancer per person-year would be 1.5 times greater in the group that was X-rayed compared to the control group. They tested it like this:
no_cancer_cases <- c(41, 15) # person-centuries rather than person-years to get the estimated rate # on a more interpretable scale. person_centuries <- c(28.011, 19.025) poisson.test(no_cancer_cases, person_centuries, r = 1.5, alternative = "greater")