Context
In this blog the author suggests using Tarone's Z-statistic to test for overdispersion in a binomial model to determine whether or not it is necessary to use a beta-binomial model instead. In their example they generate some synthetic data from binomial and beta-binomial distributions and then calculate the Z-statistic's for each and plot them, along with a theoretical curve of the null distribution to demonstrate that this metric works.
Question
How do I actually calculate/use this to test for overdispersion? I found the author code difficult to follow and I don't quite understand how I could use this to formally test for over-dispersion.
I have searched around but all I can turn up about Tarone's Z-statistic are the two links I have included.
I am working in R
using the lme4
and glmmTMB
packages and I would greatly appreciate an answer in this form. I know this question kind of straddles the bounds of CV and stackoverflow, but I considered this a "non-trivial problem" - If the community disagrees I am happy to migrate it!
Update:
I have managed to adapt C.A. Kapourani's code and write a function for calculating Z-statistics for my models (see below), but I still have the problem of how to compare two values. Is it sensible to find the Z-score having a cumulative probability equal to the critical probability (i.e. 0.05) like with other Z-scores? If so, can anyone recommend how I might do this in R?
taronesZStat <- function(model){
#Extracting model residuals
res <- residuals(model)
#Number of residuals
n <- length(res)
#Calculating Tarone's Z-statistic
p_hat <- sum(res)/n
S <- sum((res - n * p_hat)^2 / (p_hat * (1 - p_hat)))
Z_score <- (S - sum(n)) / sqrt(2 * sum(n * (n - 1)))
return(Z_score)
}
DHARMa
which also has tests for over/under-dispersion of glm(m) models, as well as a bunch of other useful tests. I hope this is useful to anyone stumbling across this now: cran.r-project.org/web/packages/DHARMa/vignettes/DHARMa.html $\endgroup$