Solved – how to calculate the parameter lambda in Poisson distribution

maximum likelihoodr

Let's say there is a sequence:

a <- c(1,2,3,1,2,1,1,3,1,2,3,5)

This conforms to a Poisson distribution, the formula of which is shown as:

enter image description here

Now I want to calculate the parameter lambda of Poisson. Usually we need to use maximum likelihood estimation to do this. But how to do it in R?

This is my first time to do statistical analysis in R, so please provide as many as details as possible.

Best Answer

the easy way

Since we know the mean is the MLE for $\lambda$:

mean(a)  ## 2.0833

fitting distributions

MASS::fitdistr is a built-in method for ML estimation of the parameters of a variety of distributions.

MASS::fitdistr(a,"Poisson")

brute force: optim

Define a function that returns the negative log-likelihood for a given value of $\lambda$:

f <- function(lambda) {
    -sum(dpois(a,lambda=lambda,log=TRUE))
}
optim(par=1, ## starting value
      fn=f,
      method="Brent",   ## need to specify for 1-D optimization
      lower=0.001, upper=10)

mle2

The bbmle::mle2() function (a variant of stats4::mle()) does the same optimization, but has more features for doing things with the results (e.g. computing likelihood profiles, comparing models via Likelihood Ratio Test). (mle2 uses BFGS instead of Nelder-Mead optimization by default, which works in 1-D, so we don't need the method="Brent" from above [we could use it if we wanted].)

library(bbmle)
mle2(minuslogl=f,start=list(lambda=1))

mle2 with formula

mle2 also allows some shortcuts:

mle2(a ~ dpois(lambda),
     data=data.frame(a),
     start=list(lambda=1))

glm

As @glen_b points out in comments, this is also a special case of a generalized linear model. Since the Poisson model uses a log link by default, we have to be a little bit careful.

coef(glm(a~1,family=poisson(link="identity")))
exp(coef(glm(a~1,family=poisson)))