Re: [R] Error singular gradient matrix at initial parameter estimates in nls
On 2011-06-30 06:14, Niklaus Hurlimann wrote: Greetings, I am struggling a bit with a non-linear regression. The problem is described below with the known values r and D inidcated. I tried to alter the start values but get always following error message: Error in nlsModel(formula, mf, start, wts): singular gradient matrix at initial parameter estimates Calls: nls - switch - nlsModel I might be missing something with regard to the different algorithms or I just have to try again with some different start values. If anyone finds some time to have a look at that, any advice would be very much appreciated. I don't know anything about a Brice-Model, but I doubt that you have it right. 4*pi*390*Na is about 3*10^27 and that doesn't seem right to me. In addition, do put your data into a data.frame (containing variables r and D) and learn to use dput for posting data. Peter Ehlers ##Brice-Model bricemod-nls(log10(D) ~ log10(Do)*exp(((-4*pi*En*Na)*((ro/2)*(r-ro)^2 +(1/3)*(r-ro)^3))/(R*T)), start=list(Do=0.8, En=390, ro=1.03),trace=TRUE) Na-6.0221415*10^23 # Avrogadro's number T-1010 # Temp in K R-8.3144 #Gas constant [J mol^-1 K^-1] r-data.matrix(PRr[c(r)]) r La 1.160 Ce 1.143 Pr 1.109 Nd 1.093 Sm1.079 Eu 1.066 Gd 1.053 Tb 1.040 Dy 1.027 Ho 1.015 Er 1.004 Tm 0.994 Yb 0.985 Lu 0.977 D-data.matrix(PRr[c(D)]) D La 0.1806551 Ce 0.2703113 Pr 0.3757225 Nd 0.5271811 Sm 0.8665835 Eu 1.0812568 Gd 1.0612762 Tb 1.0726612 Dy 1.1679270 Ho 1.1910920 Er 1.1336938 Tm 1.1215107 Yb 0.9619603 Lu 0.8315467 Niklaus __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Error singular gradient matrix at initial parameter estimates in nls
I think that these aren't good initial values. If you do a plot of data and add a curve, the curve don't approximate the data. Frequently I use interactive procedures to get good initial values. Using playwith() you can handle sliders to adjust values and use in nls(), look the following r - c(1.16,1.143,1.109,1.093,1.079,1.066,1.053,1.040,1.027,1.015,1.004,0.994,0.985,0.977) D - c(0.1806551,0.2703113,0.3757225,0.5271811,0.8665835,1.0812568,1.0612762,1.0726612, 1.167927,1.191092,1.1336938,1.1215107,0.9619603,0.8315467) names - c(La,Ce,Pr,Nd,Sm,Eu,Gd,Tb,Dy,Ho,Er,Tm,Yb,Lu) plot(log(D)~r) Do - 0.8; En - 390; ro - 1.03 curve(log10(Do)*exp(((-4*pi*En*Na)*((ro/2)*(x-ro)^2+(1/3)*(x-ro)^3))/(R*T)), add=TRUE) Na - 6.0221415*10^23 # Avrogadro's number T - 1010 # Temp in K R - 8.3144 # Gas constant [J mol^-1 K^-1] ## Brice-Model bricemod - nls(log10(D)~log10(Do)*exp(((-4*pi*En*Na)*((ro/2)*(r-ro)^2+(1/3)*(r-ro)^3))/(R*T)), start=list(Do=0.8, En=390, ro=1.03), trace=TRUE) require(playwith) start - list() playwith( { plot(log(D)~r) curve(log10(Do)*exp(((-4*pi*En*Na)*((ro/2)*(x-ro)^2+(1/3)*(x-ro)^3))/(R*T)), add=TRUE) start - list(Do=Do, En=En, ro=ro) }, parameters=list( Do=seq(0.7,0.8,by=0.001), En=seq(350,450,by=1), ro=seq(0.5,1.5,by=0.1))) start bricemod - nls(log10(D)~log10(Do)*exp(((-4*pi*En*Na)*((ro/2)*(r-ro)^2+(1/3)*(r-ro)^3))/(R*T)), start=start, trace=TRUE) Bests. Walmes. == Walmes Marques Zeviani LEG (Laboratório de Estatística e Geoinformação, 25.450418 S, 49.231759 W) Departamento de Estatística - Universidade Federal do Paraná fone: (+55) 41 3361 3573 VoIP: (3361 3600) 1053 1173 e-mail: wal...@ufpr.br twitter: @walmeszeviani homepage: http://www.leg.ufpr.br/~walmes linux user number: 531218 == [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Error singular gradient matrix at initial parameter estimates in nls
Greetings, I am struggling a bit with a non-linear regression. The problem is described below with the known values r and D inidcated. I tried to alter the start values but get always following error message: Error in nlsModel(formula, mf, start, wts): singular gradient matrix at initial parameter estimates Calls: nls - switch - nlsModel I might be missing something with regard to the different algorithms or I just have to try again with some different start values. If anyone finds some time to have a look at that, any advice would be very much appreciated. ##Brice-Model bricemod-nls(log10(D) ~ log10(Do)*exp(((-4*pi*En*Na)*((ro/2)*(r-ro)^2 +(1/3)*(r-ro)^3))/(R*T)), start=list(Do=0.8, En=390, ro=1.03),trace=TRUE) Na-6.0221415*10^23 # Avrogadro's number T-1010 # Temp in K R-8.3144 #Gas constant [J mol^-1 K^-1] r-data.matrix(PRr[c(r)]) r La 1.160 Ce 1.143 Pr 1.109 Nd 1.093 Sm1.079 Eu 1.066 Gd 1.053 Tb 1.040 Dy 1.027 Ho 1.015 Er 1.004 Tm 0.994 Yb 0.985 Lu 0.977 D-data.matrix(PRr[c(D)]) D La 0.1806551 Ce 0.2703113 Pr 0.3757225 Nd 0.5271811 Sm 0.8665835 Eu 1.0812568 Gd 1.0612762 Tb 1.0726612 Dy 1.1679270 Ho 1.1910920 Er 1.1336938 Tm 1.1215107 Yb 0.9619603 Lu 0.8315467 Niklaus -- Niklaus Hürlimann Doctorant-(cand.PhD) Université de Lausanne Institut de Minéralogie et Géochimie L'Anthropole CH-1015 Lausanne Suisse E-mail: niklaus.hurlim...@unil.ch Tel:+41(0)21 692 4452 [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Error singular gradient matrix at initial parameter estimates in nls
If you have a perfect fit, you have zero residuals. But in the nls manual page we have: Warning: *Do not use ‘nls’ on artificial zero-residual data.* So this is a case of complaining that your diesel car is broken because you ignored the Diesel fuel only sign on the filler cap and put in gasoline. However I've not been happy with this choice in the code of nls -- it's been there a long time -- and my own codes from 1974 onwards have always handled zero residual cases. I do believe that the code could at least give a better diagnostic message. Zero residuals -- perfect fits -- arise when one is interested more or less in an interpolating function rather than doing statistics, and I can understand the reluctance of statisticians to countenance such a use of nls. And Bert's comment on overparametrization is almost certainly valid also. JN __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Error singular gradient matrix at initial parameter estimates in nls
Dear JN, Bert, 1) It is not a perfect fit. I do not think I have ever said that. I said that an external algorithms fits the model without any problems: with ~ 500,000 data points and 19 paramters (ki in the original equation), it fits the model in less than 1 second. The data are not artificial data. The variables are independent (pi in the original model). The solution is unique and the rapidity of convergence is practically independent from the selection of start conditions (with a reasonable selection of start conditions at least). The resulting residuals are approximately normally distributed with mean 0 and sd ~ 4.23. 2) I agree with the comment of Bert on over-parametrization, but again the model is not overparamterised, and it is identifiable (in part answered already in (1)) Regards Prof. John C Nash wrote: If you have a perfect fit, you have zero residuals. But in the nls manual page we have: Warning: *Do not use ‘nls’ on artificial zero-residual data.* So this is a case of complaining that your diesel car is broken because you ignored the Diesel fuel only sign on the filler cap and put in gasoline. However I've not been happy with this choice in the code of nls -- it's been there a long time -- and my own codes from 1974 onwards have always handled zero residual cases. I do believe that the code could at least give a better diagnostic message. Zero residuals -- perfect fits -- arise when one is interested more or less in an interpolating function rather than doing statistics, and I can understand the reluctance of statisticians to countenance such a use of nls. And Bert's comment on overparametrization is almost certainly valid also. JN -- Corrado Topi PhD Researcher Global Climate Change and Biodiversity Area 18,Department of Biology University of York, York, YO10 5YW, UK Phone: + 44 (0) 1904 328645, E-mail: ct...@york.ac.uk __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Error singular gradient matrix at initial parameter estimates in nls
Try the function called `nls.lm' which is contained in the minpack.lm package. See if it solves your problem. Ravi. -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Corrado Sent: Wednesday, March 31, 2010 9:13 AM Cc: r-help@r-project.org Subject: Re: [R] Error singular gradient matrix at initial parameter estimates in nls Dear JN, Bert, 1) It is not a perfect fit. I do not think I have ever said that. I said that an external algorithms fits the model without any problems: with ~ 500,000 data points and 19 paramters (ki in the original equation), it fits the model in less than 1 second. The data are not artificial data. The variables are independent (pi in the original model). The solution is unique and the rapidity of convergence is practically independent from the selection of start conditions (with a reasonable selection of start conditions at least). The resulting residuals are approximately normally distributed with mean 0 and sd ~ 4.23. 2) I agree with the comment of Bert on over-parametrization, but again the model is not overparamterised, and it is identifiable (in part answered already in (1)) Regards Prof. John C Nash wrote: If you have a perfect fit, you have zero residuals. But in the nls manual page we have: Warning: *Do not use ‘nls’ on artificial zero-residual data.* So this is a case of complaining that your diesel car is broken because you ignored the Diesel fuel only sign on the filler cap and put in gasoline. However I've not been happy with this choice in the code of nls -- it's been there a long time -- and my own codes from 1974 onwards have always handled zero residual cases. I do believe that the code could at least give a better diagnostic message. Zero residuals -- perfect fits -- arise when one is interested more or less in an interpolating function rather than doing statistics, and I can understand the reluctance of statisticians to countenance such a use of nls. And Bert's comment on overparametrization is almost certainly valid also. JN -- Corrado Topi PhD Researcher Global Climate Change and Biodiversity Area 18,Department of Biology University of York, York, YO10 5YW, UK Phone: + 44 (0) 1904 328645, E-mail: ct...@york.ac.uk __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Error singular gradient matrix at initial parameter estimates in nls
Corrado: Over parameterization/non-identifiability is not determined by the ratio of the number of data values to the number of parameters: if you try to fit a scatter of a zillion points that lie near a straight line to a model with curvature -- the 4p logistic function, say -- you're over-parameterized. Period. I am not an expert on nonlinear optimization, but I would think that the correlation matrix of the parameters would be one thing to check; another would be the change in the fits when some of the model terms are dropped. But model selection for nonlinear fitting is not a trivial issue, and those with real expertise, not me, could be more helpful. The fact that other algorithms converge while R's did not is also not particularly informative, I believe. This could occur merely due to the choice of convergence criteria (step size, choice of objective function, etc.). Or the convergence could be to a local minimum, not a global one. Comparison of performance of optimization algorithms is, again, a cottage industry for which serious expertise is required. (This should not be taken as a defense of R, either. My optimization ignorance cuts both ways). So I do not think your protestations of innocence are necessarily accurate. Nor, I agree, are my accusations of guilt. I **still** believe that your convergence difficulties are due to over-parameterization, and that this is something that needs to be carefully investigated; but that is a prior, not a posterior. Examining lots of plots is probably a good place to begin. Cheers, Bert Bert Gunter Genentech Nonclinical Biostatistics -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Corrado Sent: Wednesday, March 31, 2010 6:13 AM Cc: r-help@r-project.org Subject: Re: [R] Error singular gradient matrix at initial parameter estimates in nls Dear JN, Bert, 1) It is not a perfect fit. I do not think I have ever said that. I said that an external algorithms fits the model without any problems: with ~ 500,000 data points and 19 paramters (ki in the original equation), it fits the model in less than 1 second. The data are not artificial data. The variables are independent (pi in the original model). The solution is unique and the rapidity of convergence is practically independent from the selection of start conditions (with a reasonable selection of start conditions at least). The resulting residuals are approximately normally distributed with mean 0 and sd ~ 4.23. 2) I agree with the comment of Bert on over-parametrization, but again the model is not overparamterised, and it is identifiable (in part answered already in (1)) Regards Prof. John C Nash wrote: If you have a perfect fit, you have zero residuals. But in the nls manual page we have: Warning: *Do not use 'nls' on artificial zero-residual data.* So this is a case of complaining that your diesel car is broken because you ignored the Diesel fuel only sign on the filler cap and put in gasoline. However I've not been happy with this choice in the code of nls -- it's been there a long time -- and my own codes from 1974 onwards have always handled zero residual cases. I do believe that the code could at least give a better diagnostic message. Zero residuals -- perfect fits -- arise when one is interested more or less in an interpolating function rather than doing statistics, and I can understand the reluctance of statisticians to countenance such a use of nls. And Bert's comment on overparametrization is almost certainly valid also. JN -- Corrado Topi PhD Researcher Global Climate Change and Biodiversity Area 18,Department of Biology University of York, York, YO10 5YW, UK Phone: + 44 (0) 1904 328645, E-mail: ct...@york.ac.uk __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Error singular gradient matrix at initial parameter estimates in nls
I am using nls to fit a non linear function to some data. The non linear function is: y= 1- exp(-(k0+k1*p1+ + kn*pn)) I have chosen algorithm port, with lower boundary is 0 for all of the ki parameters, and I have tried many start values for the parameters ki (including generating them at random). If I fit the non linear function to the same data using an external algorithm, it fits perfectly and finds the parameters. As soon as I come to my R installation (2.10.1 on Kubuntu Linux 910 64 bit), I keep getting the error: Error in nlsModel(formula, mf, start, wts, upper) : singular gradient matrix at initial parameter estimates I have read all the previous postings and the documentation, but to no avail: the error is there to stay. I am sure the problem is with nls, because the external fitting algorithm perfectly fits it in less than a second. Also, if my n is 4, then the nls works perfectly (but that excludes all the k5 kn). Can anyone help me with suggestions? Thanks in advance. Alternatively, what do you suggest I should do? Shall I abandon nls in favour of optim? Regards -- Corrado Topi PhD Researcher Global Climate Change and Biodiversity Area 18,Department of Biology University of York, York, YO10 5YW, UK Phone: + 44 (0) 1904 328645, E-mail: ct...@york.ac.uk __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Error singular gradient matrix at initial parameter estimates in nls
You could try method=brute-force in the nls2 package to find starting values. On Tue, Mar 30, 2010 at 7:03 AM, Corrado ct...@york.ac.uk wrote: I am using nls to fit a non linear function to some data. The non linear function is: y= 1- exp(-(k0+k1*p1+ + kn*pn)) I have chosen algorithm port, with lower boundary is 0 for all of the ki parameters, and I have tried many start values for the parameters ki (including generating them at random). If I fit the non linear function to the same data using an external algorithm, it fits perfectly and finds the parameters. As soon as I come to my R installation (2.10.1 on Kubuntu Linux 910 64 bit), I keep getting the error: Error in nlsModel(formula, mf, start, wts, upper) : singular gradient matrix at initial parameter estimates I have read all the previous postings and the documentation, but to no avail: the error is there to stay. I am sure the problem is with nls, because the external fitting algorithm perfectly fits it in less than a second. Also, if my n is 4, then the nls works perfectly (but that excludes all the k5 kn). Can anyone help me with suggestions? Thanks in advance. Alternatively, what do you suggest I should do? Shall I abandon nls in favour of optim? Regards -- Corrado Topi PhD Researcher Global Climate Change and Biodiversity Area 18,Department of Biology University of York, York, YO10 5YW, UK Phone: + 44 (0) 1904 328645, E-mail: ct...@york.ac.uk __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.