Skip to content

Instantly share code, notes, and snippets.

@statwonk
Last active September 28, 2019 07:11
Show Gist options
  • Save statwonk/0bff890bbdc78fabd28db536f88093a3 to your computer and use it in GitHub Desktop.
Save statwonk/0bff890bbdc78fabd28db536f88093a3 to your computer and use it in GitHub Desktop.
A gist that shows how a rootogram helps find that the zero-inflated negative binomial was the data generating mechanism.
# install.packages("countreg", repos="http://R-Forge.R-project.org")
# https://www.fromthebottomoftheheap.net/2016/06/07/rootograms/
# https://channel9.msdn.com/Events/useR-international-R-User-conferences/useR-International-R-User-2017-Conference/countreg-Tools-for-count-data-regression
library(countreg)
rzinbinom(3e3, size = 4, mu = 20, pi = 0.05) -> x
table(x)
hist(x, col = "orange")
rootogram(glm(x ~ 1, family = "poisson")) # zeros under fit
rootogram(zeroinfl(x ~ 1, dist = "poisson")) # low counts under fit, high counts over fit
rootogram(zeroinfl(x ~ 1, dist = "negbin")) # zero fit well, low counts fit well, high counts fit well.
sort(ls("package:countreg"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment