Re: [R] nls fitting
Lorenzo Isella wrote: Dear All, I may look ridiculous, but I am puzzled at the behavior of the nls with a fitting I am currently dealing with. My data are: x N 1 346.4102 145.428256 2 447.2136 169.530634 3 570.0877 144.081627 4 721.1103 106.363316 5 894.4272 130.390552 6 1264.9111 36.727069 7 1788.8544 52.848587 8 2449.4897 25.128742 9 3464.1016 7.531766 10 4472.1360 8.827367 11 6123.7244 6.600603 12 8660.2540 4.083339 I would like to fit N as a function of x according to a function depending on 9 parameters (A1,A2,A3,mu1,mu2,mu3,myvar1,myvar2,myvar3), namely N ~ (log(10)*A1/sqrt(2*pi)/log(myvar1)*exp(-((log(x/mu1))^2)/2/log(myvar1)/log(myvar1)) +log(10)*A2/sqrt(2*pi)/log(myvar2)*exp(-((log(x/mu2))^2)/2/log(myvar2)/log(myvar2)) +log(10)*A3/sqrt(2*pi)/log(myvar3)*exp(-((log(x/mu3))^2)/2/log(myvar3)/log(myvar3))) (i.e. N is to be seen as a sum of three bells whose parameters I need to determine). So I tried: out-nls(N ~ (log(10)*A1/sqrt(2*pi)/log(myvar1)*exp(-((log(x/mu1))^2)/2/log(myvar1)/log(myvar1)) +log(10)*A2/sqrt(2*pi)/log(myvar2)*exp(-((log(x/mu2))^2)/2/log(myvar2)/log(myvar2)) +log(10)*A3/sqrt(2*pi)/log(myvar3)*exp(-((log(x/mu3))^2)/2/log(myvar3)/log(myvar3))) ,start=list(A1 = 85, A2=23,A3=4,mu1=430,mu2=1670,mu3=4900,myvar1=1.59,myvar2=1.5,myvar3=1.5 ) ,algorithm = port ,control=list(maxiter=2,tol=1) ,lower=c(A1=0.1,A2=0.1,A3=0.1,mu1=0.1,mu2=0.1,mu3=0.1,myvar1=0.1,myvar2=0.1,myvar3=0.1) ) getting the error message: Error in nls(N ~ (log(10) * A1/sqrt(2 * pi)/log(myvar1) * exp(-((log(x/mu1))^2)/2/log(myvar1)/log(myvar1)) + : Convergence failure: singular convergence (7) I tried to adjust tol maxiter, but unsuccessfully. If I try fitting N with only two bells, then nls works: out-nls(N ~ (log(10)*A1/sqrt(2*pi)/log(myvar1)*exp(-((log(x/mu1))^2)/2/log(myvar1)/log(myvar1)) +log(10)*A2/sqrt(2*pi)/log(myvar2)*exp(-((log(x/mu2))^2)/2/log(myvar2)/log(myvar2)) ) ,start=list(A1 = 85, A2=23,mu1=430,mu2=1670,myvar1=1.59,myvar2=1.5 ) ,algorithm = port ,control=list(maxiter=2,tol=1) ,lower=c(A1=0.1,A2=0.1,mu1=0.1,mu2=0.1,myvar1=0.1,myvar2=0.1) ) out Nonlinear regression model model: N ~ (log(10) * A1/sqrt(2 * pi)/log(myvar1) * exp(-((log(x/mu1))^2)/2/log(myvar1)/log(myvar1)) + log(10) * A2/sqrt(2 * pi)/log(myvar2) * exp(-((log(x/mu2))^2)/2/log(myvar2)/log(myvar2))) data: parent.frame() A1 A2mu1mu2 myvar1 myvar2 84.920085 40.889968 409.656404 933.081936 1.811560 2.389215 residual sum-of-squares: 2394.876 Any idea about how to get nls working with the whole model? I had better luck with the nls.lm package, but it does not allow to introduce any constrain on my fitting parameters. I was also suggested to try other packages like optim to do the same fitting, but I am a bit unsure about how to set up the problem. Any suggestions? BTW, I am working with R Version 2.2.1 Lorenzo __ R-help@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 apart from the fact that fitting 9 parameters to 12 points quite genereally will not yield satisfactory results (at least estimates will have huge uncertainties), total failure in your case seems obviouus if you plot your data: it's not even obvious where the three peaks (means of the gaussians) should be: all below x=2000 or is there one peak at about x=4500 and one of the 'peaks' below x=2000 is spurious? if you can't decide, nls has problems. moreover: how should reliable estimates of the standard deviations (width) of the gaussian result if the peaks essentially consist of exactly one point? in short: I believe, you either need much more data points or you must put in substantial a priori information (e.g. either means or standard deviations of the gaussians). __ R-help@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
[R] nls fitting
Dear All, I may look ridiculous, but I am puzzled at the behavior of the nls with a fitting I am currently dealing with. My data are: x N 1 346.4102 145.428256 2 447.2136 169.530634 3 570.0877 144.081627 4 721.1103 106.363316 5 894.4272 130.390552 6 1264.9111 36.727069 7 1788.8544 52.848587 8 2449.4897 25.128742 9 3464.1016 7.531766 10 4472.1360 8.827367 11 6123.7244 6.600603 12 8660.2540 4.083339 I would like to fit N as a function of x according to a function depending on 9 parameters (A1,A2,A3,mu1,mu2,mu3,myvar1,myvar2,myvar3), namely N ~ (log(10)*A1/sqrt(2*pi)/log(myvar1)*exp(-((log(x/mu1))^2)/2/log(myvar1)/log(myvar1)) +log(10)*A2/sqrt(2*pi)/log(myvar2)*exp(-((log(x/mu2))^2)/2/log(myvar2)/log(myvar2)) +log(10)*A3/sqrt(2*pi)/log(myvar3)*exp(-((log(x/mu3))^2)/2/log(myvar3)/log(myvar3))) (i.e. N is to be seen as a sum of three bells whose parameters I need to determine). So I tried: out-nls(N ~ (log(10)*A1/sqrt(2*pi)/log(myvar1)*exp(-((log(x/mu1))^2)/2/log(myvar1)/log(myvar1)) +log(10)*A2/sqrt(2*pi)/log(myvar2)*exp(-((log(x/mu2))^2)/2/log(myvar2)/log(myvar2)) +log(10)*A3/sqrt(2*pi)/log(myvar3)*exp(-((log(x/mu3))^2)/2/log(myvar3)/log(myvar3))) ,start=list(A1 = 85, A2=23,A3=4,mu1=430,mu2=1670,mu3=4900,myvar1=1.59,myvar2=1.5,myvar3=1.5 ) ,algorithm = port ,control=list(maxiter=2,tol=1) ,lower=c(A1=0.1,A2=0.1,A3=0.1,mu1=0.1,mu2=0.1,mu3=0.1,myvar1=0.1,myvar2=0.1,myvar3=0.1) ) getting the error message: Error in nls(N ~ (log(10) * A1/sqrt(2 * pi)/log(myvar1) * exp(-((log(x/mu1))^2)/2/log(myvar1)/log(myvar1)) + : Convergence failure: singular convergence (7) I tried to adjust tol maxiter, but unsuccessfully. If I try fitting N with only two bells, then nls works: out-nls(N ~ (log(10)*A1/sqrt(2*pi)/log(myvar1)*exp(-((log(x/mu1))^2)/2/log(myvar1)/log(myvar1)) +log(10)*A2/sqrt(2*pi)/log(myvar2)*exp(-((log(x/mu2))^2)/2/log(myvar2)/log(myvar2)) ) ,start=list(A1 = 85, A2=23,mu1=430,mu2=1670,myvar1=1.59,myvar2=1.5 ) ,algorithm = port ,control=list(maxiter=2,tol=1) ,lower=c(A1=0.1,A2=0.1,mu1=0.1,mu2=0.1,myvar1=0.1,myvar2=0.1) ) out Nonlinear regression model model: N ~ (log(10) * A1/sqrt(2 * pi)/log(myvar1) * exp(-((log(x/mu1))^2)/2/log(myvar1)/log(myvar1)) + log(10) * A2/sqrt(2 * pi)/log(myvar2) * exp(-((log(x/mu2))^2)/2/log(myvar2)/log(myvar2))) data: parent.frame() A1 A2mu1mu2 myvar1 myvar2 84.920085 40.889968 409.656404 933.081936 1.811560 2.389215 residual sum-of-squares: 2394.876 Any idea about how to get nls working with the whole model? I had better luck with the nls.lm package, but it does not allow to introduce any constrain on my fitting parameters. I was also suggested to try other packages like optim to do the same fitting, but I am a bit unsure about how to set up the problem. Any suggestions? BTW, I am working with R Version 2.2.1 Lorenzo __ R-help@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
Re: [R] nls fitting
You have nearly as many parameters as data points which may cause fundamental problem singularity problems but one thing to try, just in case, is to transform it to unconstrained. For example, let A1 = 0.1 + A1x^2 and then substitute A1 with the right hand side so that it becomes a function of the parameter A1x instead of A1 and similarly for the other parameters so that it becomes unconstrained. On 5/21/06, Lorenzo Isella [EMAIL PROTECTED] wrote: Dear All, I may look ridiculous, but I am puzzled at the behavior of the nls with a fitting I am currently dealing with. My data are: x N 1 346.4102 145.428256 2 447.2136 169.530634 3 570.0877 144.081627 4 721.1103 106.363316 5 894.4272 130.390552 6 1264.9111 36.727069 7 1788.8544 52.848587 8 2449.4897 25.128742 9 3464.1016 7.531766 10 4472.1360 8.827367 11 6123.7244 6.600603 12 8660.2540 4.083339 I would like to fit N as a function of x according to a function depending on 9 parameters (A1,A2,A3,mu1,mu2,mu3,myvar1,myvar2,myvar3), namely N ~ (log(10)*A1/sqrt(2*pi)/log(myvar1)*exp(-((log(x/mu1))^2)/2/log(myvar1)/log(myvar1)) +log(10)*A2/sqrt(2*pi)/log(myvar2)*exp(-((log(x/mu2))^2)/2/log(myvar2)/log(myvar2)) +log(10)*A3/sqrt(2*pi)/log(myvar3)*exp(-((log(x/mu3))^2)/2/log(myvar3)/log(myvar3))) (i.e. N is to be seen as a sum of three bells whose parameters I need to determine). So I tried: out-nls(N ~ (log(10)*A1/sqrt(2*pi)/log(myvar1)*exp(-((log(x/mu1))^2)/2/log(myvar1)/log(myvar1)) +log(10)*A2/sqrt(2*pi)/log(myvar2)*exp(-((log(x/mu2))^2)/2/log(myvar2)/log(myvar2)) +log(10)*A3/sqrt(2*pi)/log(myvar3)*exp(-((log(x/mu3))^2)/2/log(myvar3)/log(myvar3))) ,start=list(A1 = 85, A2=23,A3=4,mu1=430,mu2=1670,mu3=4900,myvar1=1.59,myvar2=1.5,myvar3=1.5 ) ,algorithm = port ,control=list(maxiter=2,tol=1) ,lower=c(A1=0.1,A2=0.1,A3=0.1,mu1=0.1,mu2=0.1,mu3=0.1,myvar1=0.1,myvar2=0.1,myvar3=0.1) ) getting the error message: Error in nls(N ~ (log(10) * A1/sqrt(2 * pi)/log(myvar1) * exp(-((log(x/mu1))^2)/2/log(myvar1)/log(myvar1)) + : Convergence failure: singular convergence (7) I tried to adjust tol maxiter, but unsuccessfully. If I try fitting N with only two bells, then nls works: out-nls(N ~ (log(10)*A1/sqrt(2*pi)/log(myvar1)*exp(-((log(x/mu1))^2)/2/log(myvar1)/log(myvar1)) +log(10)*A2/sqrt(2*pi)/log(myvar2)*exp(-((log(x/mu2))^2)/2/log(myvar2)/log(myvar2)) ) ,start=list(A1 = 85, A2=23,mu1=430,mu2=1670,myvar1=1.59,myvar2=1.5 ) ,algorithm = port ,control=list(maxiter=2,tol=1) ,lower=c(A1=0.1,A2=0.1,mu1=0.1,mu2=0.1,myvar1=0.1,myvar2=0.1) ) out Nonlinear regression model model: N ~ (log(10) * A1/sqrt(2 * pi)/log(myvar1) * exp(-((log(x/mu1))^2)/2/log(myvar1)/log(myvar1)) + log(10) * A2/sqrt(2 * pi)/log(myvar2) * exp(-((log(x/mu2))^2)/2/log(myvar2)/log(myvar2))) data: parent.frame() A1 A2mu1mu2 myvar1 myvar2 84.920085 40.889968 409.656404 933.081936 1.811560 2.389215 residual sum-of-squares: 2394.876 Any idea about how to get nls working with the whole model? I had better luck with the nls.lm package, but it does not allow to introduce any constrain on my fitting parameters. I was also suggested to try other packages like optim to do the same fitting, but I am a bit unsure about how to set up the problem. Any suggestions? BTW, I am working with R Version 2.2.1 Lorenzo __ R-help@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 __ R-help@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
Re: [R] nls fitting problems (singularity)
Have a look at optim (which supports a number of different algorithms via the method= arg) and segmented in package segmented which does segmented regression. For example, ss - function(par) { b - par[1]; c1 - par[2]; c2 - par[3]; d - par[4] x - df1$x; y - df1$y sum((y - (d+(x-b)*c1*(x-b0)+(x-b)*c2*(x-b=0)))^2) } optim(sl,ss) Karl Knoblick karlknoblich at yahoo.de writes: : : Hallo! : : I have a problem with fitting data with nls. The first : example with y1 (data frame df1) shows an error, the : second works fine. : : Is there a possibility to get a fit (e.g. JMP can fit : also data I can not manage to fit with R). Sometimes I : also got an error singularity with starting : parameters. : : # x-values : x-c(-1,5,8,11,13,15,16,17,18,19,21,22) : # y1-values (first data set) : y1=c(-55,-22,-13,-11,-9.7,-1.4,-0.22,5.3,8.5,10,14,20) : # y2-values (second data set) : y2=c(-92,-42,-15,1.3,2.7,8.7,9.7,13,11,19,18,22) : : # data frames : df1-data.frame(x=x, y=y1) : df2-data.frame(x=x, y=y2) : : # start list for parameters : sl-list( d=0, b=10, c1=90, c2=20) : : # y1-Analysis - Result: Error in ... singular : gradient : nls(y~d+(x-b)*c1*(x-b0)+(x-b)*c2*(x-b=0), data=df1, : start=sl) : # y2-Analysis - Result: working... : nls(y~d+(x-b)*c1*(x-b0)+(x-b)*c2*(x-b=0), data=df2, : start=sl) : : # plots to look at data : par(mfrow=c(1,2)) : plot(df1$x,df1$y) : plot(df2$x,df2$y) : : Perhaps there is another fitting routine? Can anybody : help? : : Best wishes, : Karl : : : : : : : ___ : Bestellen Sie Y! DSL und erhalten Sie die AVM FritzBox SL fr 0. : Sie sparen 119 und bekommen 2 Monate Grundgebhrbefreiung. : : __ : R-help at stat.math.ethz.ch mailing list : https://www.stat.math.ethz.ch/mailman/listinfo/r-help : PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html : : __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] nls fitting problems (singularity)
Often when nls doesn't converge there is a good reason for it. I'm on a very slow internet connection these days and will not be able to look at the data myself but I ask you to bear in mind that, when dealing with nonlinear models, there are model/data set combinations for which there are no parameter estimates. Gabor Grothendieck wrote: Have a look at optim (which supports a number of different algorithms via the method= arg) and segmented in package segmented which does segmented regression. For example, ss - function(par) { b - par[1]; c1 - par[2]; c2 - par[3]; d - par[4] x - df1$x; y - df1$y sum((y - (d+(x-b)*c1*(x-b0)+(x-b)*c2*(x-b=0)))^2) } optim(sl,ss) Karl Knoblick karlknoblich at yahoo.de writes: : : Hallo! : : I have a problem with fitting data with nls. The first : example with y1 (data frame df1) shows an error, the : second works fine. : : Is there a possibility to get a fit (e.g. JMP can fit : also data I can not manage to fit with R). Sometimes I : also got an error singularity with starting : parameters. : : # x-values : x-c(-1,5,8,11,13,15,16,17,18,19,21,22) : # y1-values (first data set) : y1=c(-55,-22,-13,-11,-9.7,-1.4,-0.22,5.3,8.5,10,14,20) : # y2-values (second data set) : y2=c(-92,-42,-15,1.3,2.7,8.7,9.7,13,11,19,18,22) : : # data frames : df1-data.frame(x=x, y=y1) : df2-data.frame(x=x, y=y2) : : # start list for parameters : sl-list( d=0, b=10, c1=90, c2=20) : : # y1-Analysis - Result: Error in ... singular : gradient : nls(y~d+(x-b)*c1*(x-b0)+(x-b)*c2*(x-b=0), data=df1, : start=sl) : # y2-Analysis - Result: working... : nls(y~d+(x-b)*c1*(x-b0)+(x-b)*c2*(x-b=0), data=df2, : start=sl) : : # plots to look at data : par(mfrow=c(1,2)) : plot(df1$x,df1$y) : plot(df2$x,df2$y) : : Perhaps there is another fitting routine? Can anybody : help? : : Best wishes, : Karl : : : : : : : ___ : Bestellen Sie Y! DSL und erhalten Sie die AVM FritzBox SL fr 0. : Sie sparen 119 und bekommen 2 Monate Grundgebhrbefreiung. : : __ : R-help at stat.math.ethz.ch mailing list : https://www.stat.math.ethz.ch/mailman/listinfo/r-help : PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html : : __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] nls fitting problems (singularity)
Douglas Bates [EMAIL PROTECTED] writes: Often when nls doesn't converge there is a good reason for it. I'm on a very slow internet connection these days and will not be able to look at the data myself but I ask you to bear in mind that, when dealing with nonlinear models, there are model/data set combinations for which there are no parameter estimates. In this particular case, the model describes a curve consisting of two line segments that meet at the point (b,d) : nls(y~d+(x-b)*c1*(x-b0)+(x-b)*c2*(x-b=0), data=df2, Now if b is between the two smallest x, you can diddle b, c1, and d in such a way that the value at x1 is constant. I.e. the model becomes unidentifiable. Putting trace=T suggests that this is what happens in this example. -- O__ Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907 __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] nls fitting problems (singularity)
Hallo! I have a problem with fitting data with nls. The first example with y1 (data frame df1) shows an error, the second works fine. Is there a possibility to get a fit (e.g. JMP can fit also data I can not manage to fit with R). Sometimes I also got an error singularity with starting parameters. # x-values x-c(-1,5,8,11,13,15,16,17,18,19,21,22) # y1-values (first data set) y1=c(-55,-22,-13,-11,-9.7,-1.4,-0.22,5.3,8.5,10,14,20) # y2-values (second data set) y2=c(-92,-42,-15,1.3,2.7,8.7,9.7,13,11,19,18,22) # data frames df1-data.frame(x=x, y=y1) df2-data.frame(x=x, y=y2) # start list for parameters sl-list( d=0, b=10, c1=90, c2=20) # y1-Analysis - Result: Error in ... singular gradient nls(y~d+(x-b)*c1*(x-b0)+(x-b)*c2*(x-b=0), data=df1, start=sl) # y2-Analysis - Result: working... nls(y~d+(x-b)*c1*(x-b0)+(x-b)*c2*(x-b=0), data=df2, start=sl) # plots to look at data par(mfrow=c(1,2)) plot(df1$x,df1$y) plot(df2$x,df2$y) Perhaps there is another fitting routine? Can anybody help? Best wishes, Karl ___ Bestellen Sie Y! DSL und erhalten Sie die AVM FritzBox SL für 0. Sie sparen 119 und bekommen 2 Monate Grundgebührbefreiung. __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] nls fitting inside a loop in S-Plus
Hi, this following problem is a S-Plus problem, I know many guys here are also experts in S-plus, so I am posting here, too. Thanks I encountered a weird problem of fitting nls inside a loop, it works well in R, but not in S-plus. The code is: data1-cbind(c(2.87,1.66,0.44,-0.78,-2.00,-3.21,-4.43,-5.65,2.87,1.66,0.44,-0.78,-2.00,-3.21,-4.43,-5.65),c(-0.69,-1.91,-3.13,-4.34,-5.56,-6.78,-7.99,-9.21,-0.69,-1.91,-3.13,-4.34,-5.56,-6.78,-7.99,-9.21)) data2-cbind(c(8.05,6.50,5.03,4.37,4.03,3.92,3.87,3.89,7.84,6.27,4.74,4.14,3.76,3.69,3.69,3.71),c(8.07,6.94,5.59,4.43,3.66,3.00,2.64,2.40,8.09,6.90,5.56,4.44,3.50,2.71,2.48,2.08)) par(mfrow=c(2,2)) for (i in 1:2) { conc-data1[,i] signal-data2[,i] fit-nls(signal~SSfpl(conc,A,B,xmid,scal)) p.conc-data.frame(conc=(1:99)*(max(conc)-min(conc))/100+min(conc)) plot(as.numeric(p.conc[,1]),as.numeric(predict(fit,newdata=p.conc)),type='l',col=4) } When the above code was run in R, it worked very well, but when it was run in S-plus, it gave me the following error: Problem in data.frameAux.list(x, na.strings = na.strings, stringsAsFactors ..: arguments imply differing number of rows: 4, 3, 2, 16, 4, 4, 4, 4, 16, 1, 1, 1, 1 Also, If I only run the loop for only 1 cycle (either No.1 or No.2 by setting for (i in 1:1) or for (i in 2:2)), the code worked ok in S-Plus, so the problem has nothing to do with my data. If I replace fit-nls(signal~SSfpl(conc,A,B,xmid,scal)) with fit-lm(signal~conc), then the code worked well in both R and S-Plus. So it seems the problem only pertain to the nls function. Can anyone pinpoint the problem for me? Thanks __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help