Skip to content

Instantly share code, notes, and snippets.

@tomsing1
Created June 1, 2022 02:02
Show Gist options
  • Save tomsing1/9701d638e6cd2287e1307ffb280f1ac5 to your computer and use it in GitHub Desktop.
Save tomsing1/9701d638e6cd2287e1307ffb280f1ac5 to your computer and use it in GitHub Desktop.
R script demonstrating how to plot rootograms in R
library(vcd)
library(patchwork)
set.seed(123)
# we make 4 independent draws from a poisson
# distribution, fit the poisson distribution
# and then draw the rootogram to compare observed
# and fitted values.
plots <- lapply(1:4, function(draw) {
counts <- rpois(100, 1)
fit <- vcd::goodfit(counts, type = "poisson", method = "ML")
suppressWarnings(
vcd::rootogram(fit, scale = "raw", main = sprintf("Draw %s", draw),
return_grob = TRUE
)
)
})
patchwork::wrap_plots(plots)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment