I am looking for a way to fit a mixture of 2 univariate Poisson distributions, but the R packages I checked, mixtools
and flexmix
, assume a GLM model. They require a "formula", etc.. How can I fit a non-GLM model to my data?
In Hidden Markov Models for Time Series: An Introduction Using R by MacDonald et al. one exercise suggests to fit Poisson distributions using nlm
of optim
; I guess that is possible too, the error function could be tied to the likelihood of the data, the routine would optimize on that.
Note: Per @Tim's comment, I used
library("flexmix")
y <- c(4,5,4,1,0,4,3,4,0,6,3,3,4,0,2,6,3,3,5,4,5,3,
1,4,4,1,5,5,3,4,2,5,2,2,3,4,2,1,3,2,2,1,1,1,
1,3,0,0,1,0,1,1,0,0,3,1,0,3,2,2,0,1,1,1,0,1,
0,1,0,0,0,2,1,0,0,0,1,1,0,2,3,3,1,1,2,1,1,1,
1,2,4,2,0,0,0,1,4,0,0,0,1,0,0,0,0,0,1,0,0,1,0,1)
df <- data.frame(y)
res <- flexmix(y ~ 1, data = df, k = 2, model = FLXMRglm(family = "poisson"))
print (summary(res))
print (posterior(res))
This seemed to work; my goal was fitting two Poisson distributions to coal mining accident data to find a switch point between two different regimes.
Thanks,