Skip to content

Instantly share code, notes, and snippets.

@statwonk

statwonk/rootograms.R

Last active Sep 28, 2019
Embed
What would you like to do?
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
You can’t perform that action at this time.