Arnaud Mosnier
2012-Apr-24 19:22 UTC
[R] Use of optim to fit two curves at the same time ?
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)) ###################################################################### [[alternative HTML version deleted]]
Arnaud Mosnier
2012-Apr-25 12:57 UTC
[R] Use of optim to fit two curves at the same time ?
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]]