# [R] Weibull distribution

Valentin Dimitrov vsdimitrov at yahoo.com
Sat Jul 22 02:39:14 CEST 2006

```It seems to me that not for all values of mu and sd
there is a Weibull distribution with mean=mu and
variance=sd^2.
the programm with optimize(f.fn) finds always a
solution, but this is not necessarily what we need,
because the minimum of (abs(f(x)) is not always 0.
Suppose f(x)=2+x^2, then optimize(x) finds x=0, but
x=0 is not a root of f(x)=0.
That's why I agree with Thomas Lumley, that uniroot()
could be more appropriate than optim and optimize.

Best regards,
Valentin

--- Leaf Sun <leaflovesun at yahoo.ca> wrote:

> Thanks for the suggestion! I switched to optimize(),
> al <- optimize(f.fn, lower = 0.1, upper
> =100,tol=0.001);
> the warnings were gone and it works stably.
> But when I tried  al <- uniroot(f.fn, lower = 0.1,
> upper =100,tol=0.001);
> error occured: f() values at end points not of
> opposite sign. The error seems to me like there is
> no root found within the interval. I was not able to
> solve this problem.
>
> Thanks!
>
> Leaf
>
>
>
>
>
> ----- Original Message -----
>
> From: Thomas Lumley,  tlumley at u.washington.edu
> Sent: 2006-07-21,  09:35:11
> To: Valentin Dimitrov, vsdimitrov at yahoo.com
> Subject:  Re: [R] Weibull distribution
>
> 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
> optimize().
>
>
>   -thomas
>

```