[R] add constraints to nls or use another function

PtitBleu ptit_bleu at yahoo.fr
Thu Jun 28 09:37:29 CEST 2012


Hello, 

I'm trying to fit experimental data with a model and nls.
For some experiments, I have data with x from 0 to 1.2 and the fit is quite
good.
But it can happen that I have data only the [0,0.8] range (see the example
below) and, then, the fit is not correct.

I would like to add a constraint, for example : the second derivative must
be positive.
But I don't know how to add this to nls and, in fact,  I'm not sure that it
is possible.
If not, could you give me a package and/or a function (and/or a complete
solution, which will be great ...) ?

Thanks in advance for your help,
Have a good day,
Ptit Bleu.

-------------------------------------------------------------------------------------------------------------
#The experimental data :
http://r.789695.n4.nabble.com/file/n4634705/nlstest_data.txt
nlstest_data.txt 

#The script
library(lattice)
dftest<-read.table("nlstest_data.txt", sep=";", dec=".", as.is=T, header=T)

nlsfit<-coefficients(nls(y ~
F+A*(x-1)+B*log(x)+C*(x-1)*(x-1)+D*log(x)*log(x), data=dftest, start =
list(A=-0.06,B=0.48,C=0.89,D=0.03,F=1), control=list(maxiter=200,
tol=0.01)))
# Coefficients :
#          A                         B                  C                      
D                     F 
# -0.06098888  0.48632458  0.89397997  0.02978641  5.00743745

plot(dftest$x,dftest$y,pch=20, col="red", xlim=c(0,1.2))
seqx<-seq(0,1.2,by=0.0125)
# There must me a way with predict, but I didn't manage to use it
points(seqx,nlsfit[5]+nlsfit[1]*(seqx-1)+nlsfit[2]*log(seqx)+nlsfit[3]*(seqx-1)*(seqx-1)+nlsfit[4]*log(seqx)*log(seqx),pch=20,col="black",type="l",lwd=2)
 
#The result of the fit :
http://r.789695.n4.nabble.com/file/n4634705/nlstest_graph.png   

--
View this message in context: http://r.789695.n4.nabble.com/add-constraints-to-nls-or-use-another-function-tp4634705.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list