title: Maximum likelihood in R with mle and fitdistr
body: <p>I have a problem with maximum likelihood in R, that I hope you can help me with. In the code Nt stands for observed claims counts and vt for corresponding volumes. </p>
  
  <p>First I assume a Poisson dist. so I have estimated lambda with mle and got 0.10224. Then I tried to estimate lambda with fitdistr, and the result was 1022.4. How come it is a difference between these two functions? How do I correct it?</p>
  
  <p>Later I tried, with the same data, to estimate the parameters of a Negative binomial. But I haven't got it to work. Can you see what I have done wrong? The start values for lambda and gamma are just random numbers I picked.</p>
  
  <pre><code>library(stats4)
  library(MASS) 
  
  Nt &lt;- c(1000, 997, 985, 989, 1056, 1070, 994, 986, 1093, 1054)
  vt &lt;- rep(10000, 10) 
  n &lt;- length(Nt)
  
  obs.freq &lt;- Nt/vt
  overall.freq &lt;- sum(Nt)/sum(vt)
  
  #####
  # mle - poisson
  #####
  
  fit.poisson &lt;- function(lamda){
  -sum(dpois(Nt, lamda*vt, log=TRUE))
  }
  
  mle.poisson &lt;- mle(fit.poisson, start=list(lamda=overall.freq), nobs = NROW(Nt))
  mle.poisson.summary &lt;- summary(mle.poisson)
  
  fitdistr(Nt, "Poisson")   
  
  #####
  # mle - negative binomial
  #####
  
  fit.negbin &lt;- function(lamda, gamma){
  SUM &lt;- numeric(n)
  for(i in 1:n){
  SUM[i] &lt;- lfactorial(Nt[i]+gamma-1) - lfactorial(Nt[i]) - lfactorial(gamma-1) + gamma*log(1-lamda*vt[i]/(gamma+lamda*vt[i])) + Nt[i]*log(lamda*vt[i]/(gamma+lamda*vt[i]))
  }
  -sum(SUM)
  }
  
  mle.negbin &lt;- mle(fit.negbin, start=list(lamda=0.1, gamma=2), nobs=n)
  mle.negbin.summary &lt;- summary(mle.negbin)
  
  fitdistr(Nt,"Negative binomial")
  </code></pre>