[R] optim with constraints

JPO jpolmsted at gmail.com
Mon Dec 7 14:26:06 CET 2009


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.




More information about the R-help mailing list