[R] Using "optim" with exponential power distribution

Stefan Evert stefan.evert at uos.de
Fri Jan 16 18:43:00 CET 2009


> I know "optim" should do a minimisation, therefor I used as the
> optimisation function
>
> opt.power <- function(val, x, y) {
>   a <- val[1];
>   b <- val[2];
>   sum(y - b/(2*pi*a^2*gamma(2/b))*exp(-(x/a)^b));
> }
>
> I call: (with xm and ym the data from the table)
>
> a1 <- c(0.2, 100)
> opt <- optim(a1, opt.power, method="BFGS", x=xm, y=ym)
>
> but no optimisation of the parameter in a1 takes place.
> Any ideas?

It looks to me like your optimising the _average_ of the differences  
between y and the function, so as long as positive and negative  
differences balance out you get a cost value of 0 (and you can make it  
even smaller if the fitted function is much larger than the actual y  
values, so all differences are negative).

You probably wanted to minimise the squared errors:

	sum((y - b/(2*pi*a^2*gamma(2/b))*exp(-(x/a)^b)))^2)




Best regards,
Stefan Evert

[ stefan.evert at uos.de | http://purl.org/stefan.evert ]



More information about the R-help mailing list