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. [[alternative HTML version deleted]]
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
the following work for me: x <- rlnorm(1000, 5, 3) fn <- function(parms, dat) -sum(dlnorm(dat, parms[1], parms[2], log = TRUE)) optim(c(5, 3), fn, dat = x) library(MASS) fitdistr(x, "log-normal", list(meanlog = 5, sdlog = 3)) I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/16/336899 Fax: +32/16/337015 Web: med.kuleuven.be/biostat student.kuleuven.ac.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Carsten Steinhoff" <carsten.steinhoff at stud.uni-goettingen.de> To: <r-help at stat.math.ethz.ch> Sent: Wednesday, June 29, 2005 5:19 PM Subject: [R] MLE with optim> 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. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > R-project.org/posting-guide.html >