[R] optim
M.Kondrin
mkondrin at hppi.troitsk.ru
Sun Mar 2 10:32:02 CET 2003
Hello!
Although I agree that the functions used are a little tricky to deal
with, it seems to me to be an odd way to use optim(). Wouldn't it be
better just use uniroot?
What I have in mind is:
THETA[2] may be easily excluded from the equations. So we are left with
2 functions and 2 unknowns (THETA[1,3]).
Here is simple example of how system with two equations and two unknowns
can be solved. An idea is to find root along one variable and use this
value to find root along other.
> f1 <- function(x,y){
+ return(x*y-1)
+ }
> f2 <- function(x,y){
+ return(x-y-2)
+ }
> ff <- function(y,x){
+ Zero <- uniroot(f1,interval=c(-1,3),y=y)
+ return(f2(x=Zero$root,y=y))
+ }
>
> uniroot(ff,interval=c(0.4,0.6),x=0.8)
$root # y
[1] 0.4142135
$f.root
[1] 2.670726e-07
$iter
[1] 4
$estim.prec
[1] 6.103516e-05
Although it will take some time to find "right" intervals.
Good luck ...
ripley at stats.ox.ac.uk wrote:
> Do read the help page. It says:
>
> `fnscale' An overall scaling to be applied to the value of `fn'
> and `gr' during optimization. If negative, turns the problem
> into a maximization problem. Optimization is performed on
> `fn(par)/fnscale'.
>
> `parscale' A vector of scaling values for the parameters.
> Optimization is performed on `par/parscale' and these should
> be comparable in the sense that a unit change in any element
> produces about a unit change in the scaled value.
>
> You have not used either, AFAICS.
>
> Also, I doubt if the function value returned by calls to integrate is a
> smooth function of the parameters, so scaling is particularly important
> here, and you may need to supply ndeps too.
>
> Attempting to optimize blindly without supplying derivatives is asking far
> too much of a computer program.
>
>
> On Fri, 28 Feb 2003, Remigijus Lapinskas wrote:
>
>
>>Dear all,
>>
>>I have a function MYFUN which depends on 3 positive parameters TETA[1],
>>TETA[2], and TETA[3]; x belongs to [0,1].
>>I integrate the function over [0,0.1], [0.1,0.2] and
>>[0.2,0.3] and want to choose the three parameters so that
>>these three integrals are as close to, resp., 2300, 4600 and 5800 as
>>possible. As I have three equations with three unknowns, I expect the
>>exact fit, i.e., the SS (see below) to be zero. However, the optim
>>function never gives me what I expect, the minimal SS value(=res$value)
>>never comes close to zero, the estimates of the parameters, res$par,
>>wildly depends on init etc.
>>I would be grateful for any comments on this miserable situation.
>>
>>aa <- c(2300,4600,5800)
>>init <- c(2.5,8000,0.84) # initial values of parameters
>>print(init)
>>###################
>>myfun <- function(x,TETA) TETA[2]*(((1-x)^(-1/TETA[3]))-
>>1)^(1/TETA[1])
>>###################
>>x <- seq(0,0.3,by=0.01)
>>plot(x,myfun(x,init),type="l")
>>###################
>>LSS <- function(teta,aa)
>>{
>>integr <- numeric(3)
>> for(i in 1:3)
>> {integr[i] <- 10*integrate(myfun,
>> lower=(i-1)/10,upper=i/10,TETA=teta)$value
>> }
>>SS <- sum((integr-aa)^2) # SS=Sum of Squares
>>SS
>>}
>>####################
>>res <- optim(init,LSS,aa=aa,
>>method = "L-BFGS-B",lower=c(0,0,0.5))
>>print(res$par)
>>print(res$value)
>>
>>
>>
>>>source("C:/Program Files/R/integral.R")
>>
>>[1] 2.50 7000.00 0.84 # initial
>>[1] 2.3487221 6999.9999823 0.5623628 # final
>>[1] 75613.05 # minSS
>>
>>>source("C:/Program Files/R/integral.R")
>>
>>[1] 2.5 15000 0.84 # initial
>>[1] 2.125804 14999.999747 2.241179 # final
>>[1] 50066.35 # minSS
>>
>>
>>
>>Best regards,
>>Remigijus mailto:remigijus.lapinskas at maf.vu.lt
>>
>>______________________________________________
>>R-help at stat.math.ethz.ch mailing list
>>http://www.stat.math.ethz.ch/mailman/listinfo/r-help
>>
>
>
More information about the R-help
mailing list