[R] Nonlinear Least Squares nls() programming help

MathZero thk3421 at gmail.com
Sun Jul 12 04:42:17 CEST 2009


Hi, I am trying to use the nls() function to closely approximate a vector of
values, colC and I'm running into trouble.  I am not sure how if I am asking
the program to do what I think its doing, because the same minimization in
Excel's Solver does not run into problems.  If anyone can tell me what is
going wrong, and why I'm getting a singular convergence(7) error, please
tell me.  I have also included, after the R code, the optimal answers
according to Excel.  The reason I'm using R is because of the (relative)
ease of getting the standard errors on the coefficients, which I don't
believe Excel Solver does.  I'm new to nonlinear optimization, so please
forgive any obvious things I've overlooked.  Thank you very much for taking
a look!

Here is the R code, and error message:

ColumnA.data<-read.csv(file.choose()) #select ColA.csv
ColumnB.data<-read.csv(file.choose()) #select ColB.csv
ColumnC.data<-read.csv(file.choose()) #select ColC.csv
colA<-ColumnA.data[0:3600,]
colB<-ColumnB.data[0:3600,]
colC<-ColumnC.data[0:3600,]

i<-1:3600

cor.model<-nls(colC ~ exp( - beta2 * abs( colB[i] - colA[i] ) / 12 ) - ( 1 -
exp( - beta2 * abs(  colB[i] - colA[i] ) / 12 ) ) * exp( - beta1 / ( min(
colB[i], colA[i] ) / 12 ) ) , start=list(beta1 = 0.37, beta2 = 0.06), trace
= T, control=nls.control(minFactor=1/4096) , alg="port",
lower=list(beta1=0.35,beta2=0.05)) 

  0:     7.7890438: 0.370000 0.0600000
  1:     7.4408010:  3.96197 0.0597763
  2:     4.5308657:  3.96197 0.0500000
  3:     4.5308657:  3.96197 0.0500000
Error in nls(colC ~ exp(-beta2 * abs(colB[i] - colA[i])/12) - (1 -
exp(-beta2 *  : 
  Convergence failure: singular convergence (7)




The optimal answers, according to Excel are:
beta2= 0.0670690912936098
beta1=0.398341074464919

when I try to check the residuals myself with the following R code, I get
the same answer as when I calculate them in Excel:

colE<-function(i) {(colC[i]-exp( - beta2 * abs( colB[i] - colA[i] ) / 12 ) -
( 1 - exp( - beta2 * abs(  colB[i] - colA[i] ) / 12 ) ) * exp( - beta1 / (
min( colB[i], colA[i] ) / 12 ) ) )^2}

Any ideas?  Thank you for your help!


-- 
View this message in context: http://www.nabble.com/Nonlinear-Least-Squares-nls%28%29-programming-help-tp24445472p24445472.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list