[R] Weibull distribution

Thomas Lumley tlumley at u.washington.edu
Fri Jul 21 17:35:01 CEST 2006

On Fri, 21 Jul 2006, Valentin Dimitrov wrote:

> Dear Leaf,
> I modified your code as follows:
> gamma.fun <- function(mu,sd,start=100)
> {
> f.fn <- function(alpha)
> {abs(sd^2-mu^2/(gamma(1+1/alpha))^2*(gamma(1+2/alpha)-(gamma(1+1/alpha))^2))}
> alpha <- optim(start, f.fn)
> beta <- mu/gamma(1+1/alpha$par)
> return(list=c(a=alpha$par,b=beta));
> }
> Now it works properly.
> First, I added an abs(). You tried to solve an
> equation by means of the R-function optim(), which
> finds a minimum. That's why you can find the solution
> of f(x)=a through minimization of abs(f(x)-a).
> Second, I deleted the optim-method BFGS from the
> optim() function, because it is not appropriate in
> this case.

optim() is not appropriate at all in this case -- its help page says to 
use optimize() for one-dimensional problems.

In fact, in one dimension there isn't any need to resort to optimization 
when you really want root-finding, and uniroot() is more appropriate than 


More information about the R-help mailing list