Hi, dear R users I am a newbie in R and I wantto use the method of meximum likelihood to fit a Weibull distribution to my survival data. I use "optim" as follows: optim(c(1, 0.25),weibull.like,mydata=mydata,method="L-BFGS-B",hessian = TRUE) My question is: how do I setup the constraints so that the two parametrs of Weibull to be pisotive? Or should I use other function like"nlm"? Many thanks! Any comments are greatly appreciated! Steven
As per ?optim> Usage: > > optim(par, fn, gr = NULL, ..., > method = c("Nelder-Mead", "BFGS", "CG", "L-BFGS-B", "SANN"), > lower = -Inf, upper = Inf, > control = list(), hessian = FALSE)Note that the optimx() function on R-forge http://r-forge.r-project.org/R/?group_id=395 is a wrapper that allows some other methods too. JN> Message: 72 > Date: Sun, 6 Dec 2009 02:53:55 -0800 (PST) > From: Steven <ytsteven at gmail.com> > Subject: [R] optim with constraints > To: r-help at r-project.org > Message-ID: > <5e293933-91f2-48f7-98e5-bc87905c59d2 at x25g2000prf.googlegroups.com> > Content-Type: text/plain; charset=ISO-8859-1 > > Hi, dear R users > > I am a newbie in R and I wantto use the method of meximum likelihood > to fit a Weibull distribution to my survival data. I use "optim" as > follows: > > > optim(c(1, 0.25),weibull.like,mydata=mydata,method="L-BFGS-B",hessian > = TRUE) > > My question is: how do I setup the constraints so that the two > parametrs of Weibull to be pisotive? Or should I use other function > like"nlm"? > > Many thanks! Any comments are greatly appreciated! > > Steven
Read the help page for `optim'. You already seem to be aware that "L-BFGS-B" should be used. How much more harder is to read the help page further to figure out how to supply the constraints? There are also various other algorithms, but "L-BFGS-B" should be able to do the job. Ravi. ____________________________________________________________________ Ravi Varadhan, Ph.D. Assistant Professor, Division of Geriatric Medicine and Gerontology School of Medicine Johns Hopkins University Ph. (410) 502-2619 email: rvaradhan at jhmi.edu ----- Original Message ----- From: Steven <ytsteven at gmail.com> Date: Sunday, December 6, 2009 5:54 am Subject: [R] optim with constraints To: r-help at r-project.org> Hi, dear R users > > I am a newbie in R and I wantto use the method of meximum likelihood > to fit a Weibull distribution to my survival data. I use "optim" as > follows: > > > optim(c(1, 0.25),weibull.like,mydata=mydata,method="L-BFGS-B",hessian > = TRUE) > > My question is: how do I setup the constraints so that the two > parametrs of Weibull to be pisotive? Or should I use other function > like"nlm"? > > Many thanks! Any comments are greatly appreciated! > > Steven > > ______________________________________________ > R-help at r-project.org mailing list > > PLEASE do read the posting guide > and provide commented, minimal, self-contained, reproducible code.
Hi, Prof Nash Thanks for your comment! I modified my code to be (added an extra parametr): optim(c(1.14,0.25,0.06), weibull.like, mydata=mydata, method="L-BFGS- B", hessian = TRUE, lower = c(0, 0, 0), upper = c(Inf, Inf, 1)) But I had the following error: Error in optim(c(1.14, 0.25, 0.06), weibull.like, mydata = mydata, method = "L-BFGS-B", : non-finite finite-difference value [2] What does that mean? Much appreciate your help! Steven On Dec 7, 1:15?am, "Prof. John C Nash" <nas... at uottawa.ca> wrote:> As per ??optim > > > Usage: > > > ? ? ?optim(par, fn, gr = NULL, ..., > > ? ? ? ? ? ?method = c("Nelder-Mead", "BFGS", "CG", "L-BFGS-B", "SANN"), > > ? ? ? ? ? ?lower = -Inf, upper = Inf, > > ? ? ? ? ? ?control = list(), hessian = FALSE) > > Note that the optimx() function on R-forgehttp://r-forge.r-project.org/R/?group_id=395 > is a wrapper that allows some other methods too. > > JN > > > > > > > Message: 72 > > Date: Sun, 6 Dec 2009 02:53:55 -0800 (PST) > > From: Steven <ytste... at gmail.com> > > Subject: [R] optim with constraints > > To: r-h... at r-project.org > > Message-ID: > > ? ?<5e293933-91f2-48f7-98e5-bc87905c5... at x25g2000prf.googlegroups.com> > > Content-Type: text/plain; charset=ISO-8859-1 > > > Hi, dear R users > > > I am a newbie in R and I wantto use the method of meximum likelihood > > to fit a Weibull distribution to my survival data. I use "optim" as > > follows: > > > optim(c(1, 0.25),weibull.like,mydata=mydata,method="L-BFGS-B",hessian > > = TRUE) > > > My question is: how do I setup the constraints so that the two > > parametrs of Weibull to be pisotive? Or should I use other function > > like"nlm"? > > > Many thanks! Any comments are greatly appreciated! > > > Steven > > ______________________________________________ > R-h... at r-project.org mailing listhttps://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.- Hide quoted text - > > - Show quoted text -
Steven, A quick trick to deal with problems of this nature (i.e. forcing parameters to stay positive although the parameter search isn't bound to non-negatives or positives) without resorting to another package is to re-parametrize the density function. Notice, x \in (-\infty,\infty) for x \in (-\infty, \infty) but, exp(x) \in (0, \infty) for x \in (\-infty, \infty). Thus, you could replace your "scale" and "shape" parameters with "exp^ (notscale)" and "exp^(notshape)". You goal would be to optimize over possible values of "notshape" and "notscale". Of course, the output is giving you values of "notshape" and "notscale", which aren't the quantities of interest. As such, "scale=exp^(notscale)" and "shape=exp^ (notshape)". To be fair, this approach may not be desirable. It changes some things with regard to the sampling distributions. Whereas you would have immediately talked about the standard error for, say, the scale parameter and you could have said that the sampling distribution of it was asymptotically Normal, your sampling distribution of notscale is asy. Normal, but scale=exp^(notscale) is not dist. asy. Normal. Hope that is of some use, JPO On Dec 6, 5:53?am, Steven <ytste... at gmail.com> wrote:> Hi, dear R users > > I am a newbie in R and I wantto use the method of meximum likelihood > to fit a Weibull distribution to my survival data. I use "optim" as > follows: > > optim(c(1, 0.25),weibull.like,mydata=mydata,method="L-BFGS-B",hessian > = TRUE) > > My question is: how do I setup the constraints so that the two > parametrs of Weibull to be pisotive? Or should I use other function > like"nlm"? > > Many thanks! Any comments are greatly appreciated! > > Steven > > ______________________________________________ > R-h... at r-project.org mailing listhttps://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Without the data / script, I'm guessing that it is likely an attempt to evaluate the loss function at an inadmissible point e.g., at the constraint where there is a log(0). Different optimization tools handle things differently, and there are a couple of us working (very slowly due to other things) on trying to provide a nice wrapper to catch these exceptions so that they can be handled better. JN> Message: 56 > Date: Sun, 6 Dec 2009 17:32:54 -0800 (PST) > From: Steven <ytsteven at gmail.com> > Subject: Re: [R] optim with constraints > To: r-help at r-project.org > Message-ID: > <88d5c01d-c30c-4e91-900c-2b825d5e3be8 at z4g2000prh.googlegroups.com> > Content-Type: text/plain; charset=ISO-8859-1 > > Hi, Prof Nash > > Thanks for your comment! > > I modified my code to be (added an extra parametr): > > optim(c(1.14,0.25,0.06), weibull.like, mydata=mydata, method="L-BFGS- > B", hessian = TRUE, lower = c(0, 0, 0), upper = c(Inf, Inf, 1)) > > But I had the following error: > > Error in optim(c(1.14, 0.25, 0.06), weibull.like, mydata = mydata, > method = "L-BFGS-B", : > non-finite finite-difference value [2] > > What does that mean? Much appreciate your help! > > Steven >