library(dplyr) # data manipulation
library(ggplot2) # graphs
library(cleanplots) # graph scheme
cleanplots_defaults()
# Simulate data to show different Poisson PDFs
set.seed(30004)
n <- 50000
sim <- data.frame(y0_8 = rpois(n, 0.8), y1_5 = rpois(n, 1.5), y3 = rpois(n, 3),
y4_5 = rpois(n, 4.5), y10 = rpois(n, 10))
# Observed proportion of each count, from 0 to maxk
pr_count <- function(y, maxk) data.frame(k = 0:maxk, pr = sapply(0:maxk, function(j) mean(y == j)))
pdfs <- bind_rows(
"Poisson PDF with mean = 0.8" = pr_count(sim$y0_8, 10),
"Poisson PDF with mean = 1.5" = pr_count(sim$y1_5, 10),
"Poisson PDF with mean = 3" = pr_count(sim$y3, 10),
"Poisson PDF with mean = 10" = pr_count(sim$y10, 20),
.id = "mean") |>
mutate(mean = factor(mean, levels = unique(mean)))
ggplot(pdfs, aes(x = k, y = pr)) +
geom_line() + geom_point() +
facet_wrap(~ mean, scales = "free_x") +
scale_x_continuous(breaks = seq(0, 20, 2)) +
scale_y_continuous(breaks = seq(0, .5, .1), limits = c(0, .5)) +
labs(x = "# of successes", y = "Pr(y = k)")