Skip to content
Prev 81568 / 398502 Next

optim

Adrienne Gret-Regamey wrote:
Problems such as these will have more meaningful responses if you post 
an example (see the posting guide). What's being parameterized here? a 
or b or both? If just 'a', then fix 'b' at the desired value. If both, 
then give optim a starting value. And what is 'y'?

fn <- function(par, y, x) {
   a <- par[1]
   b <- par[2]
   sum((y - a * x^b)^2)
}

x <- c(1, 3, 11, 14)
y <- exp(rnorm(length(x)))
## y ~ a * x^b
## log(y) ~ log(a) + b * log(x)
v <- coef(lm(log(y) ~ log(x)))
optim(c(exp(v[1]), v[2]), fn, y = y, x = x)

Again, I may be way off. Please provide an example if this doesn't cover 
what you need. And define all the variables need to run your script.

--sundar