Solved – Fitting Mixture of Poissons Without GLM

mixture-distributionr

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,

Best Answer

You can use any GLM model (see more about GLM in here) in univariate case, if the general case is

$$ Y = \beta_0 + \beta_1 X + \varepsilon $$

then you can use intercept-only model

$$ Y = \beta_0 + \varepsilon $$

(or in R formula Y ~ 1). Such model simply estimates the mean, e.g.

> mean(mtcars$mpg)
[1] 20.09062
> lm(mpg ~ 1, mtcars)

Call:
lm(formula = mpg ~ 1, data = mtcars)

Coefficients:
(Intercept)  
      20.09  

This can be extended into other cases, like in the one you described. In fact, if you look deeper into flexmix documentation (see also both JSS papers here and here and multiple papers by Bettina GrĂ¼n and Friedrich Leisch that are available online), you'll see that multiple examples deal with such intercept-only formulas where the FLX... part is used for more advanced features of the model.

Related Question