Skip to content

Instantly share code, notes, and snippets.

@statwonk
Created September 7, 2019 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save statwonk/beab6f60e70369bf1f9f379dd3033f6e to your computer and use it in GitHub Desktop.
Save statwonk/beab6f60e70369bf1f9f379dd3033f6e to your computer and use it in GitHub Desktop.
The relationship between intra-arrival times and count models.
library(tidyverse)
# https://www.kellogg.northwestern.edu/faculty/weber/decs-430/Notes%20on%20the%20Poisson%20and%20exponential%20distributions.pdf
# http://www.mscs.mu.edu/~jsta/issues/11(3)/JSTA11(3)p2.pdf
# Case 1: memoryless intra-arrival times. They're not correlated.
tibble::tibble(
durations_between = rexp(3e4, rate = 20)
) %>%
mutate(time = cumsum(durations_between)) %>%
mutate(time_interval = cut(time, breaks = seq(0, 1e4, 1), labels = FALSE)) %>%
count(time_interval) %>%
pull(n) %>%
head(-1) %>%
fitdistrplus::fitdist("pois") %>%
plot()
# Case 2: same, Gamma(shape = 1, rate) ~ Exp(rate)
tibble::tibble(
durations_between = rgamma(3e4, shape = 1, rate = 20) # equivalent
) %>%
mutate(time = cumsum(durations_between)) %>%
mutate(time_interval = cut(time, breaks = seq(0, 1e4, 1), labels = FALSE)) %>%
count(time_interval) %>%
pull(n) %>%
head(-1) %>%
fitdistrplus::fitdist("pois") %>%
plot()
# Case 3: clustered intra-arrival times.
tibble::tibble(
durations_between = rgamma(3e4, shape = 0.5, rate = 20) # now with clustered times
) %>%
mutate(time = cumsum(durations_between)) %>%
mutate(time_interval = cut(time, breaks = seq(0, 1e4, 1), labels = FALSE)) %>%
count(time_interval) %>%
pull(n) %>%
head(-1) %>%
fitdistrplus::fitdist("nbinom") %>% # notice negative binomial
plot()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment