[R] Nonlinear Least Squares nls() programming help
spencerg
spencer.graves at prodsyse.com
Tue Jul 14 23:11:25 CEST 2009
1. What does "i" in your formula represent? Have you worked the
examples in the "nls" help page, and do you understand how it works?
"nls" tries to do vector computations.
2. Unfortunately, "nls" often quits with errors like "singular
convergence". A standard way around that problem is to use a general
purpose optimizer like "optim" to get the answer, then feed that answer
to "nls". I also use "fit <- optim(..., hessian=TRUE)" and then compute
"eigen(fit$hessian, symmetric=TRUE)": If the smallest eigenvalue is
less than roughly 0.0001 times the largest, it suggests that there may
not be sufficient information in the data to estimate all the
parameters. Then looking at the eigenvector corresponding to the
largest eigenvalue should help you identify a parameter than can be
fixed in the model to produce a model that for which all the parameters
are reasonably estimable.
3. If this still doesn't solve your problem, please post another
question. First, however, I suggest you try to generate a commented,
minimal, self-contained, reproducible example, as suggested in the
posting guide "www.R-project.org/posting-guide.html". Often the effort
of doing so will lead you to an answer to your question. If not, the
simpler example will often increase the probability of a useful reply.
Hope this helps.
Spencer Graves
MathZero wrote:
> 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!
>
>
>
More information about the R-help
mailing list