Re: [R] MLE with optim

From: Sundar Dorai-Raj <sundar.dorai-raj_at_pdf.com>
Date: Thu 30 Jun 2005 - 01:28:43 EST

Carsten Steinhoff wrote:
> Hello,
>
> I tried to fit a lognormal distribution by using optim. But sadly the output
> seems to be incorrect.
> Who can tell me where the "bug" is?
>
> test = rlnorm(100,5,3)
> logL = function(parm, x,...) -sum(log(dlnorm(x,parm,...)))
> start = list(meanlog=5, sdlog=3)
> optim(start,logL,x=test)$par
>
> Carsten.
>

You are only supplying the meanlog argument to dlnorm. Also you can set log = TRUE in dlnorm to avoid the log computation after calling dlnorm.

set.seed(1)
x <- rlnorm(100, 5, 3)
logL <- function(par, x) -sum(dlnorm(x, par[1], par[2], TRUE)) start <- list(meanlog = 5, sdlog = 2)
optim(start, logL, x = x)

Why not just use MASS::fitdistr instead?

library(MASS)
fitdistr(x, dlnorm, start)

HTH, --sundar



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Thu Jun 30 01:32:35 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:33:07 EST