[R] optim

Martin Maechler maechler at stat.math.ethz.ch
Sat Nov 26 17:13:52 CET 2005


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" == Sundar Dorai-Raj <sundar.dorai-raj at pdf.com>
>>>>>     on Fri, 25 Nov 2005 07:37:42 -0600 writes:

    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" == Sundar Dorai-Raj <sundar.dorai-raj at pdf.com>
>>>>>     on Fri, 25 Nov 2005 07:37:42 -0600 writes:

    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




More information about the R-help mailing list