-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_nbinom_to_poisson.R
69 lines (58 loc) · 1.89 KB
/
plot_nbinom_to_poisson.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
plot_negative_binomial_convergence_to_poisson <- function(lambda, step = 0.01) {
# this function plots how the negative binomial distribution converges to a
# Poisson distribution as the parameter 'p' in 'nbinom' approaches 1 from
# zero
# set up vector of 'p'/probability values, then calculate corresponding 'n's
# given 'lambda', which is the mean of Poisson distribution and its
# equivalent negative binomial distribution
ps <- seq(0.05, 0.95, step)
ps <- append(ps, 0.99)
ns <- vector("numeric", length(ps))
for (i in seq_along(ps)) {
p <- ps[i]
n <- (lambda * p) / (1 - p)
ns[i] <- n
}
# plot Poisson distribution
x <- 1:(lambda * 2)
y <- dpois(x, lambda)
plot(y ~ x, pch = 16)
# plot the converging negative binomial distributions
for (i in 5:length(ps)) {
p <- ps[i]
n <- ns[i]
d <- dnbinom(x, n, p)
# NOTE: negative binomial generates discrete data, not continuous data, but
# data are plotted continuously here to make curves visually clearer
points(d ~ x, col = 3, pch = 16, type = "l")
}
title_text <- paste(
"Poisson distribution (black dots), lambda = ", lambda,
"\nNegative Binomial distributions (green curves)"
)
title(main = title_text)
}
lambda <- 3
filename <- paste(
"negative_binomial_converge_to_poisson_lambda_", lambda, ".png",
sep = ""
)
png(filename, width = 640, height = 400)
plot_negative_binomial_convergence_to_poisson(lambda, 0.01)
dev.off()
lambda <- 8
filename <- paste(
"negative_binomial_converge_to_poisson_lambda_", lambda, ".png",
sep = ""
)
png(filename, width = 640, height = 400)
plot_negative_binomial_convergence_to_poisson(lambda, 0.01)
dev.off()
lambda <- 20
filename <- paste(
"negative_binomial_converge_to_poisson_lambda_", lambda, ".png",
sep = ""
)
png(filename, width = 640, height = 400)
plot_negative_binomial_convergence_to_poisson(lambda, 0.01)
dev.off()