Skip to content

optim

3 messages · Adrienne Gret-Regamey, Sundar Dorai-Raj, Martin Maechler

#
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
#
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
1 day later
#
Further note, that the standard package  'stats4'  has a
function  mle()  which was written exactly for problems like
yours {and mle() internally calls optim() !}.

But are we sure we are not solving your homework assignment here?

        
Sundar> 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

        
Sundar> 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

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

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

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

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

    Sundar> --sundar