[R] optim
Sundar Dorai-Raj
sundar.dorai-raj at pdf.com
Fri Nov 25 14:37:42 CET 2005
Adrienne Gret-Regamey wrote:
> Hello:
>
> I am trying to use optim() to estimate the maximum likelihood of a
> function a*x^b = y.
> Unfortunately, I always get the error, that there is no default value for b.
>
> Could you give me an example, on how to correctly optimize this function
> with input data x<-c(1,3,11,14).
>
> Thanks a lot,
>
> Adrienne
>
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
More information about the R-help
mailing list