Nice, thank you !
I particularly appreciated the list of possible explanations for "it does
not work" ! :-)

Arnaud

Le 26 avril 2012 13:49, Greg Snow <538280@gmail.com> a écrit :

> The phrase "does not work" is not very helpful, it can mean quit a few
> things including:
>
> * Your computer exploded.
> * No explosion, but smoke is pouring out the back and microsoft's
> "NoSmoke" utility is not compatible with your power supply.
> * The computer stopped working.
> * The computer sits around on the couch all day eating chips and
> watching talk shows.
> * The computer has started picketing your house shouting catchy
> slogans and demanding better working conditions and an increase in
> memory.
> * Everything went dark and you cannot check the cables on the back of
> the computer because the lights are off due to the power outage.
> * R crashed, but the other programs are still working.
> * R gave an error message and stopped processing your code after
> running for a while.
> * R gave an error message without running any of your code (and is
> waiting for your next command).
> * R is still running your code and the time has exceeded your patience
> so you think it has hung.
> * R completed and returned a result, but also gave warnings.
> * R completed your command, but gave an incorrect answer.
> * R completed your command but the answer is different from what you
> expect (but is correct according to the documentation)
>
> There are probably others.
>
> Running your code I think the answer is the last one.  The criteria
> for optim finishing is a small improvement relative to previous
> improvements, not a guarantee of an exactly correct answer.  Since
> your starting points were very different from the "True" values this
> means that what appears to be a good improvement can still be far from
> the answer that you know to be true.  There are various arguments that
> you can give to optim to improve the fitting process, but sometimes
> the easiest thing to do is to just run optim again using the previous
> results as the new starting values.  Try running the following:
>
> out4 <- optim( par=out3$par, fn=function(x){optfunc3(x,dfxy,dfxy2)})
> (out4 <- optim( par=out4$par, fn=function(x){optfunc3(x,dfxy,dfxy2)}))
>
> and see if it "works".  Run the last line a couple more times to see
> how well it works (at least it worked for me, if this does not work
> for you, tell us what "does not work" means).
>
> On Wed, Apr 25, 2012 at 6:57 AM, Arnaud Mosnier <a.mosnier@gmail.com>
> wrote:
> > Dear list,
> >
> > In order to find a solution to my problem, I created a third objective
> > function including both calculations done in the previous cases. This
> > function return a value (i.e. the value to be minimize by optim) equal to
> > the sum of the two sum of squares, but it does not work (see the code
> added
> > at the end of my previous script).
> >
> > Any suggestion ?
> >
> > Arnaud
> >
> >
> > Dear list,
> >>
> >> Here is a small example code that use optim and optimize in order to fit
> >> two functions.
> >> Is it possible to fit two functions (like those two for example) at the
> >> same time using optim ... or another function in R ?
> >>
> >> Thanks
> >>
> >> Arnaud
> >>
> >> ######################################################################
> >> ## function 1
> >> x1 <- 1:100
> >> y1 <- 5.468 * x + 3 # + rnorm(100,0, 10)
> >> dfxy <- cbind(x1,y1)
> >>
> >> # Objective function
> >> optfunc <- function(x, dfxy){
> >>   a <- x[1]
> >>   b <- x[2]
> >>   xtest <- dfxy[,1]
> >>   yobs <- dfxy[,2]
> >>   ysim <- a*xtest + b
> >>   sum((ysim - yobs)^2)
> >> }
> >>
> >> out<- optim(par=c(0.2,5), fn=function(x){optfunc(x, dfxy)}, method =
> >> "Nelder-Mead", hessian = F)
> >>
> >>
> >> ## function 2
> >>
> >> x2 <- seq(0.01, 0.1, length=100)
> >> y2 <- exp(30*x2)
> >> dfxy2 <- cbind(x2,y2)
> >>
> >> # objective function
> >> optfunc2 <- function(x, dfxy){
> >>   a <- x[1]
> >>   xtest <- dfxy[,1]
> >>   yobs <- dfxy[,2]
> >>   ysim <- exp(a*xtest)
> >>   sum((ysim - yobs)^2)
> >> }
> >>
> >> out<- optimize(f=function(x){optfunc2(x, dfxy2)}, interval=c(0,500))
> >>
> >> ######################################################################
> >>
> >>
> >
> > optfunc3 <- function(x,  dfxy, dfxy2){
> >
> >  a <- x[1]
> >  b <- x[2]
> >  xtest <- dfxy[,1]
> >  yobs <- dfxy[,2]
> >  ysim <- a*xtest + b
> >  obj1 <- sum((ysim - yobs)^2)
> >
> >  c <- x[3]
> >  xtest2 <- dfxy2[,1]
> >  yobs2 <- dfxy2[,2]
> >  ysim2 <- exp(c*xtest2)
> >  obj2 <- sum((ysim2 - yobs2)^2)
> >
> > obj1 + obj2
> > }
> >
> > out3<- optim(par=c(0.2,5, 500), fn=function(x){optfunc3(x, dfxy, dfxy2)},
> > method = "Nelder-Mead", hessian = F)
> >
> >        [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help@r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
>
>
>
> --
> Gregory (Greg) L. Snow Ph.D.
> 538280@gmail.com
>

	[[alternative HTML version deleted]]

