[R] MLE for bimodal distribution
Rubén Roa-Ureta
rroa at udec.cl
Wed Apr 8 22:24:44 CEST 2009
_nico_ wrote:
> Hello everyone,
>
> I'm trying to use mle from package stats4 to fit a bi/multi-modal
> distribution to some data, but I have some problems with it.
> Here's what I'm doing (for a bimodal distribution):
>
> # Build some fake binormally distributed data, the procedure fails also with
> real data, so the problem isn't here
> data = c(rnorm(1000, 3, 0.5), rnorm(500, 5, 0.3))
> # Just to check it's bimodal
> plot(density(data))
> f = function(m, s, m2, s2, w)
> {
> -log( sum(w*dnorm(data, mean=m, sd=s)) + sum((1-w)*dnorm(data, mean=m2,
> sd=s2)) )
> }
>
> res = mle(f, start=list("m"=3, "s"=0.5, "m2"=5, "s2"=0.35, "w"=0.6))
>
> This gives an error:
> Error in optim(start, f, method = method, hessian = TRUE, ...) :
> non-finite finite-difference value [2]
> In addition: There were 50 or more warnings (use warnings() to see the first
> 50)
> And the warnings are about dnorm producing NaNs
>
> So, my questions are:
> 1) What does "non-finite finite-difference value" mean? My guess would be
> that an Inf was passed somewhere where a finite number was expected...? I'm
> not sure how optim works though...
> 2) Is the log likelihood function I wrote correct? Can I use the same type
> of function for 3 modes?
>
I think it is correct but it is difficult to fit as others have pointed out.
As an alternative, you may discretise your variable so the mixture is
fit to counts on a histogram using a multinomial likelihood.
HTH
Rubén
More information about the R-help
mailing list