count.data <- data.frame(events=0:13, count=c(26069, 30175, 18997, 8136, 2934, 820, 250, 54, 16, 4, 7, 1, 0, 1))
count.data <- data.frame(events=0:13, count=c(26069, 30175,
18997, 8136, 2934, 820, 250, 54, 16,
4, 7, 1, 0, 1))
raw.data <- rep(count.data$events, count.data$count)
raw.data <- rep(count.data$events, count.data$count)
poisson.lambda.mle <- mean(raw.data)
poisson.lambda.mle <- mean(raw.data)
fitted.poisson.counts <- dpois(count.data$events, poisson.lambda.mle) * sum(count.data$count)
fitted.poisson.counts <- dpois(count.data$events,
poisson.lambda.mle) * sum(count.data$count)
library(MASS)
negbin.fitted.object <- MASS::fitdistr(raw.data, "Negative Binomial")
library(MASS)
negbin.fitted.object <- MASS::fitdistr(raw.data,
"Negative Binomial")
negbin.size.mle <- negbin.fitted.object$estimate['size']
negbin.mu.mle <- negbin.fitted.object$estimate['mu']
fitted.negbin.counts <- dnbinom(count.data$events, size=negbin.size.mle, mu=negbin.mu.mle) * sum(count.data$count)
negbin.size.mle <- negbin.fitted.object$estimate['size']
negbin.mu.mle <- negbin.fitted.object$estimate['mu']
fitted.negbin.counts <- dnbinom(count.data$events,
size=negbin.size.mle, mu=negbin.mu.mle) *
sum(count.data$count)
count.comparison <- transform(count.data,
fitted.poisson.counts=fitted.poisson.counts, fitted.negbin.counts=fitted.negbin.counts)
print(round(count.comparison, 0), row.names=FALSE)
count.comparison <- transform(count.data,
fitted.poisson.counts=fitted.poisson.counts,
fitted.negbin.counts=fitted.negbin.counts)
print(round(count.comparison, 0), row.names=FALSE)
events count fitted.poisson.counts fitted.negbin.counts
0 26069 24712 26149
1 30175 31234 30178
2 18997 19739 18727
3 8136 8316 8291
4 2934 2628 2933
5 820 664 881
6 250 140 233
7 54 25 56
8 16 4 12
9 4 1 3
10 7 0 0
11 1 0 0
12 0 0 0
13 1 0 0
events count fitted.poisson.counts fitted.negbin.counts
0 26069 24712 26149
1 30175 31234 30178
2 18997 19739 18727
3 8136 8316 8291
4 2934 2628 2933
5 820 664 881
6 250 140 233
7 54 25 56
8 16 4 12
9 4 1 3
10 7 0 0
11 1 0 0
12 0 0 0
13 1 0 0
plot(count ~ events, count.data, 'b', col='black', main='Comparing actual & fitted counts')
lines(count.data$events, fitted.poisson.counts, 'b', col='red')
lines(count.data$events, fitted.negbin.counts, 'b', col='blue')
grid()
legend("topright", legend=c('Actual counts', 'Fitted Poisson counts', 'Fitted Negative Binomial counts'), lty=1, col=c('black', 'red', 'blue'))
plot(count ~ events, count.data, 'b', col='black',
main='Comparing actual & fitted counts')
lines(count.data$events, fitted.poisson.counts, 'b',
col='red')
lines(count.data$events, fitted.negbin.counts, 'b',
col='blue')
grid()
legend("topright", legend=c('Actual counts',
'Fitted Poisson counts',
'Fitted Negative Binomial counts'), lty=1,
col=c('black', 'red', 'blue'))