Ronald Bialozyt
2009-Jan-16 16:19 UTC
[R] Using "optim" with exponential power distribution
Hello, I am trying to fit a exponential power distribution y = b/(2*pi*a^2*gamma(2/b))*exp(-(x/a)^b) to a bunch of data for x and y I have in a table.> datax y 1 25 27 2 75 59 3 125 219 ... 259 12925 1 260 12975 0 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? -- Ciao Ronald
> 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 ]