I know how to plot and fit a count model of a Poisson distribution of a particular random variable. However, I am interested in several count models, e.g. one for each day of the week. Each day has a unique Poisson fit since the waiting times for each day are different. Is there a way to combine or describe my data with a single probability model that is conditional on the day of the way of the week? Rather than keep track of 7 simpler Poisson distributions.
1 Answers
If you have one count for each day, independence between daily counts, and for $Y_t$ distributed poisson with a parameter $\mu_{\text{weekday}(t)}$ we can simply use poisson regression, which is a generalized linear model and can be fit in R as follows, with some simulated data:
set.seed(1001)
mu <- c(10, 20, 15, 14, 14, 5, 5)
names(mu) <- paste(c("sun", "mon", "tues", "wednes", "thurs", "fri", "satur"), "day", sep="")
n <- 52 # simulating data for 52 weeks
Y <- rpois(7*n, rep(mu, n))
x <- rep(names(mu), n)
simdata <- data.frame(x=factor(x, levels=names(mu)), Y=Y)
simmod <- glm(Y ~ 0+x, family=poisson(), data=simdata)
Then we can look at the results:
simmod
Call: glm(formula = Y ~ 0 + x, family = poisson(), data = simdata)
Coefficients:
xsunday xmonday xtuesday xwednesday xthursday xfriday
2.310 3.036 2.703 2.682 2.645 1.613
xsaturday
1.586
Degrees of Freedom: 364 Total (i.e. Null); 357 Residual
Null Deviance: 15100
Residual Deviance: 361.4 AIC: 1890
finally we can compare the exponentiated coefficients with the specified poisson means (mu):
cbind(exp(coef(simmod)), mu)
mu
xsunday 10.076923 10
xmonday 20.826923 20
xtuesday 14.923077 15
xwednesday 14.615385 14
xthursday 14.076923 14
xfriday 5.019231 5
xsaturday 4.884615 5
In this case, we have just fitted separate means for each weekday, in a roundabout way. Let us do it directly:
with(simdata, tapply(Y, x, FUN=mean))
sunday monday tuesday wednesday thursday friday saturday
10.076923 20.826923 14.923077 14.615385 14.076923 5.019231 4.884615
So in this simple case we can do without the poisson regression formulation, but if you have additional covariates it will be handy. For more details on poisson regression see Goodness of fit and which model to choose linear regression or Poisson
- 77,844