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.ev...@uos.de | http://purl.org/stefan.evert ]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to