[R] optim problem, nls regression

Uwe Ligges ligges at statistik.uni-dortmund.de
Fri Apr 1 18:50:47 CEST 2005


Kervahu Anne wrote:

> Hi,
> 
> I try to fit a non linear regression by minimising the sum of the sum of
> squares.
> 
> The model is number[2]-(x/number[1])^number[3]
> Number [2] and number [1] change as the data changes but for all the set of
> data number[3] must be identical.
> 
> I have 3 set of data (x1,y1), (x2,y2), (x3,y3).
> 
> x_a<-c(0,0.5,1,1.5,2,3,4,6)
> y_a<-c(5.4,5,4.84,4.3,4,2,1.56,1.3)
> 
> x_b<-c(0,1,2,3,4,5,6,7,8,9,10,11,12)
> y_b<-c(5.34,4.98,4.66,4.06,3,3.4,2.7,2.9,2.6,2.6,1.9,1.3,1.4)
> 
> x_c<-c(0,3,6,8,10,12,14,16,18,20,24,26,28,30)
> y_c<-c(5.5,5.1,4.3,4,3.7,3.2,3.04,2.778,2.56,2.25,1.78,1.44,1.32,1.2)
> 
> x<-c(x_a,x_b,x_c)
> y<-c(y_a,y_b,y_c)
> long<-c(0,8,21,35)
> 
> Hence, the sum of squares is:
> 
> Sce= sum( sum((y- number[4]-(x/number[1])^number[7])^2)+
> 	    sum((y- number[5]-(x/number[2])^number[7])^2)+
> 	    sum((y- number[6]-(x/number[3])^number[7])^2)+
> 
> for minimising this sum, I compute the function "sce":
> 
> sce<-function(param){
>  sce_yest<-matrix(nrow=3,ncol=1)
>  for( i in 1:3){
> yy<-(y[((long[i]+1):long[i+1])])
> xx<-x[(long[i]+1):(long[i+1])]
>  y_est<-(param[i+2]-(xx/param[i])^param[(2*3)+1])
>  sce_yest[i,]<-sum((yy-y_est)^2)
>  }
>  return(sum(sce_yest))
>  }
> 
> Then, I use the fonction optim for obtaining a vector of 7 parameters which
> will minimise the fonction sce. I use initial parameters at random that I
> don't have a answer relating to a minimum local.
> 
> pinit=c(runif(3,min=0,max=10), runif(3,min=3,max=8),runif(1,min=0,max=4))
> optim(p=pinit,sce)
> 
> if I use the function as above, I got an answer but the value of the
> parameters is not in the interval I want. So I applied
> 
> optim(p=pinit,sce, method="L-BFGS-B", lower=c(0,0,0,0,0,0,0), upper=c(10,
> 10,10,10,10,10,4),control=list(maxit=20000000,temp=20000))
> 
> but the program does not run. And I get this message
> 
> "Error in optim(p = pinit, sce, method = "L-BFGS-B", lower = pinf, upper =
> psup,  : 
>         L-BFGS-B needs finite values of fn"

As it says, you get infinite values, probably from
(x/number[1]) ...


Uwe Ligges


> 
> And I don't understand why it doesn't work. Do I forget an option in optim
> computation. Or is there an other function instead of optim that I can use.
> 
> Thanks in advance
> 
> Anne Kervahu
> annekervahu at yahoo.fr
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html




More information about the R-help mailing list