I am having some errors come up in my first section of code. I have no issue in plotting the points. Is there an easier method for creating a non-linear regression using C*(x+a)^n. The .txt file is named stage_discharge with the two variables being stage and discharge. The data is a relatively small file listed below: stage discharge 6.53 2592.05 6.32 559.5782 5.96 484.2151 4.99 494.7527 3.66 456.0778 0.51 291.13> power.nls<-nls(stage_dischargee$discharge~C*(stage_discharge$stage+a)^n,data=stage_discharge, start=list(C=4, a=0, n=1))> C<-coef(power.nls)["C"] > a<-coef(power.nls)["a"] > n<-coef(power.nls)["n"] > plot(stage_discharge$stage, stage_discharge$discharge, pch=17, cex=1.25,ylab='Discharge (cfs )', xlab='Stage (ft)', font.lab=2, main='Boone Creek\n St age-Discharge Curve')> curve(C*(x+a)^n, add=TRUE, col="red")[[alternative HTML version deleted]]
Hi I am not an expert in nonlinear regression, but your data seems to be rather weird. Last five points has almost linear relationship, the first one is several times higher. If there is no error in your data, I doubt that you can model it by power equation. Cheers Petr> -----Original Message----- > From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Zachary > Shadomy > Sent: Friday, May 5, 2017 12:58 AM > To: r-help at r-project.org > Subject: [R] Non-Linear Regression Help > > I am having some errors come up in my first section of code. I have no > issue in plotting the points. Is there an easier method for creating a > non-linear regression using C*(x+a)^n. The .txt file is named > stage_discharge with the two variables being stage and discharge. > The data is a relatively small file listed below: > > stage discharge > 6.53 2592.05 > 6.32 559.5782 > 5.96 484.2151 > 4.99 494.7527 > 3.66 456.0778 > 0.51 291.13 > > > > > > > power.nls<- > nls(stage_dischargee$discharge~C*(stage_discharge$stage+a)^n, > data=stage_discharge, start=list(C=4, a=0, n=1)) > > C<-coef(power.nls)["C"] > > a<-coef(power.nls)["a"] > > n<-coef(power.nls)["n"] > > plot(stage_discharge$stage, stage_discharge$discharge, pch=17, cex=1.25, > ylab='Discharge (cfs )', xlab='Stage (ft)', font.lab=2, main='Boone Creek\n > St age-Discharge Curve') > > curve(C*(x+a)^n, add=TRUE, col="red") > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.________________________________ Tento e-mail a jak?koliv k n?mu p?ipojen? dokumenty jsou d?v?rn? a jsou ur?eny pouze jeho adres?t?m. Jestli?e jste obdr?el(a) tento e-mail omylem, informujte laskav? neprodlen? jeho odes?latele. Obsah tohoto emailu i s p??lohami a jeho kopie vyma?te ze sv?ho syst?mu. Nejste-li zam??len?m adres?tem tohoto emailu, nejste opr?vn?ni tento email jakkoliv u??vat, roz?i?ovat, kop?rovat ?i zve?ej?ovat. Odes?latel e-mailu neodpov?d? za eventu?ln? ?kodu zp?sobenou modifikacemi ?i zpo?d?n?m p?enosu e-mailu. V p??pad?, ?e je tento e-mail sou??st? obchodn?ho jedn?n?: - vyhrazuje si odes?latel pr?vo ukon?it kdykoliv jedn?n? o uzav?en? smlouvy, a to z jak?hokoliv d?vodu i bez uveden? d?vodu. - a obsahuje-li nab?dku, je adres?t opr?vn?n nab?dku bezodkladn? p?ijmout; Odes?latel tohoto e-mailu (nab?dky) vylu?uje p?ijet? nab?dky ze strany p??jemce s dodatkem ?i odchylkou. - trv? odes?latel na tom, ?e p??slu?n? smlouva je uzav?ena teprve v?slovn?m dosa?en?m shody na v?ech jej?ch n?le?itostech. - odes?latel tohoto emailu informuje, ?e nen? opr?vn?n uzav?rat za spole?nost ??dn? smlouvy s v?jimkou p??pad?, kdy k tomu byl p?semn? zmocn?n nebo p?semn? pov??en a takov? pov??en? nebo pln? moc byly adres?tovi tohoto emailu p??padn? osob?, kterou adres?t zastupuje, p?edlo?eny nebo jejich existence je adres?tovi ?i osob? j?m zastoupen? zn?m?. This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients. If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system. If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner. The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email. In case that this e-mail forms part of business dealings: - the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without stating any reasoning. - if the e-mail contains an offer, the recipient is entitled to immediately accept such offer; The sender of this e-mail (offer) excludes any acceptance of the offer on the part of the recipient containing any amendment or variation. - the sender insists on that the respective contract is concluded only upon an express mutual agreement on all its aspects. - the sender of this e-mail informs that he/she is not authorized to enter into any contracts on behalf of the company except for cases in which he/she is expressly authorized to do so in writing, and such authorization or power of attorney is submitted to the recipient or the person represented by the recipient, or the existence of such authorization is known to the recipient of the person represented by the recipient.
If you insist on using nls() for anything that you don't understand extremely well, you will end up with frustration. nls() uses the same method K F Gauss used (with good understanding of the details) over 200 years ago. The Gauss-Newton approach inside works very well and efficiently for problems where the assumptions are met, and terribly most other times. But nls() does have some nice "extras", and rather than rewrite all the code, we have a wrapnls() function for the codes in the much more modern nlsr package. It tries (and mostly succeeds) in getting analytic derivatives in cases like this. Note that nls(), when you output the diagnostic, tells you that it is having trouble with the numeric derivative. I did the following: 1) made a csv file from the data in the posting (Shadomy17.csv) 2) edited the nls() call and added trace and try() 3) ran nlxb() from nlsr. Note that it uses a lot of iterations -- the problem is quite close to singular. The singular values have NOTHING to do with the individual parameters. Their display position is just convenient. Together they show that the ratio of largest / smallest sv (a measure of the condition number) is very large -- an ill-conditioned problem. Now we know this -- there's no guessing and hand-waving. Best, JN Here's the (rather verbose) output > library(nlsr) > shadomy <- read.csv("./Shadomy17.csv") > power.nls<-try(nls(discharge~C*(stage+a)^n, data=shadomy, start=list(C=4, a=0, n=1), trace=TRUE)) 7585285 : 4 0 1 > print(power.nls) [1] "Error in numericDeriv(form[[3L]], names(ind), env) : \n Missing value or an infinity produced when evaluating the model\n" attr(,"class") [1] "try-error" attr(,"condition") <simpleError in numericDeriv(form[[3L]], names(ind), env): Missing value or an infinity produced when evaluating the model> > tmp <- readline("Try a better approach") > p.nlxb <- nlxb(discharge~C*(stage+a)^n, data=shadomy, start=list(C=4, a=0, n=1), trace=TRUE) formula: discharge ~ C * (stage + a)^n lower:[1] -Inf -Inf -Inf upper:[1] Inf Inf Inf $watch [1] FALSE $phi [1] 1 $lamda [1] 1e-04 $offset [1] 100 $laminc [1] 10 $lamdec [1] 4 $femax [1] 10000 $jemax [1] 5000 $rofftest [1] TRUE $smallsstest [1] TRUE vn:[1] "discharge" "C" "stage" "a" "n" Finished masks check datvar:[1] "discharge" "stage" Data variable discharge :[1] 2592.05 559.58 484.22 494.75 456.08 291.13 Data variable stage :[1] 6.53 6.32 5.96 4.99 3.66 0.51 trjfn: function (prm) { if (is.null(names(prm))) names(prm) <- names(pvec) localdata <- list2env(as.list(prm), parent = data) eval(residexpr, envir = localdata) } <bytecode: 0x3263280> <environment: 0x3021208> no weights lower:[1] -Inf -Inf -Inf upper:[1] Inf Inf Inf Start:lamda: 1e-04 SS= 7585285 at C = 4 a = 0 n = 1 1 / 0 lamda: 0.001 SS= Inf at C = -843.56 a = 228.63 n = 123.33 2 / 1 lamda: 0.01 SS= Inf at C = -515.08 a = 148.03 n = 84.714 3 / 1 lamda: 0.1 SS= 9.0129e+100 at C = -50.129 a = 37.016 n = 29.648 4 / 1 lamda: 1 SS= 8.5013e+47 at C = 58.103 a = 30.986 n = 13.954 5 / 1 lamda: 10 SS= 4.2141e+31 at C = 49.209 a = 45.025 n = 8.0734 6 / 1 lamda: 100 SS= 9.4088e+10 at C = 17.369 a = 15.101 n = 2.9571 7 / 1 <<lamda: 40 SS= 7139465 at C = 5.6661 a = 1.9085 n = 1.2421 8 / 1 <<lamda: 16 SS= 6321710 at C = 7.4018 a = 3.5556 n = 1.3955 9 / 2 <<lamda: 6.4 SS= 5015080 at C = 9.3512 a = 5.1077 n = 1.5166 10 / 3 <<lamda: 2.56 SS= 3724863 at C = 11.195 a = 6.3333 n = 1.6023 11 / 4 <<lamda: 1.024 SS= 3144435 at C = 12.47 a = 6.9964 n = 1.6516 12 / 5 <<lamda: 0.4096 SS= 3044065 at C = 13.076 a = 7.0845 n = 1.6763 13 / 6 <<lamda: 0.16384 SS= 3016569 at C = 13.402 a = 6.7057 n = 1.6978 14 / 7 <<lamda: 0.065536 SS= 2965611 at C = 13.887 a = 5.7652 n = 1.739 15 / 8 <<lamda: 0.026214 SS= 2874080 at C = 14.836 a = 4.0457 n = 1.8266 16 / 9 <<lamda: 0.010486 SS= 2769844 at C = 16.1 a = 1.9237 n = 1.9871 17 / 10 <<lamda: 0.0041943 SS= 2639613 at C = 15.821 a = 0.1568 n = 2.2672 18 / 11 lamda: 0.041943 SS= 1.7977e+308 at C = 12.901 a = -1.5552 n = 2.7895 19 / 12 lamda: 0.41943 SS= 1.7977e+308 at C = 16.976 a = -0.52795 n = 2.4402 20 / 12 <<lamda: 0.16777 SS= 2550653 at C = 16.551 a = 0.15159 n = 2.3095 21 / 12 <<lamda: 0.067109 SS= 2524756 at C = 16.778 a = -0.066675 n = 2.3521 22 / 13 lamda: 0.67109 SS= 1.7977e+308 at C = 17.157 a = -0.52924 n = 2.4441 23 / 14 <<lamda: 0.26844 SS= 2517716 at C = 16.855 a = -0.12164 n = 2.3641 24 / 14 <<lamda: 0.10737 SS= 2501124 at C = 16.986 a = -0.2586 n = 2.3913 25 / 15 lamda: 1.0737 SS= 1.7977e+308 at C = 17.264 a = -0.55996 n = 2.454 26 / 16 <<lamda: 0.4295 SS= 2496748 at C = 17.03 a = -0.29226 n = 2.3988 27 / 16 <<lamda: 0.1718 SS= 2486194 at C = 17.117 a = -0.37629 n = 2.4163 28 / 17 lamda: 1.718 SS= 1.7977e+308 at C = 17.307 a = -0.56916 n = 2.4578 29 / 18 <<lamda: 0.68719 SS= 2483488 at C = 17.143 a = -0.39692 n = 2.421 30 / 18 <<lamda: 0.27488 SS= 2476879 at C = 17.199 a = -0.44853 n = 2.4322 31 / 19 lamda: 2.7488 SS= 1.7977e+308 at C = 17.323 a = -0.57068 n = 2.459 32 / 20 <<lamda: 1.0995 SS= 2475207 at C = 17.214 a = -0.46124 n = 2.4351 33 / 20 <<lamda: 0.4398 SS= 2471092 at C = 17.249 a = -0.49305 n = 2.4422 34 / 21 lamda: 4.398 SS= 1.7977e+308 at C = 17.329 a = -0.56992 n = 2.4593 35 / 22 <<lamda: 1.7592 SS= 2470058 at C = 17.259 a = -0.5009 n = 2.444 36 / 22 lamda: 17.592 SS= 1.7977e+308 at C = 17.28 a = -0.52057 n = 2.4484 37 / 23 <<lamda: 7.0369 SS= 2469799 at C = 17.261 a = -0.50286 n = 2.4444 38 / 23 <<lamda: 2.8147 SS= 2469154 at C = 17.267 a = -0.50778 n = 2.4455 39 / 24 lamda: 28.147 SS= 1.7977e+308 at C = 17.28 a = -0.52008 n = 2.4483 40 / 25 <<lamda: 11.259 SS= 2468993 at C = 17.268 a = -0.50901 n = 2.4458 41 / 25 lamda: 112.59 SS= 1.7977e+308 at C = 17.272 a = -0.51208 n = 2.4465 42 / 26 <<lamda: 45.036 SS= 2468952 at C = 17.269 a = -0.50931 n = 2.4459 43 / 26 lamda: 450.36 SS= 1.7977e+308 at C = 17.27 a = -0.51008 n = 2.4461 44 / 27 <<lamda: 180.14 SS= 2468942 at C = 17.269 a = -0.50939 n = 2.4459 45 / 27 <<lamda: 72.058 SS= 2468917 at C = 17.269 a = -0.50958 n = 2.446 46 / 28 lamda: 720.58 SS= 1.7977e+308 at C = 17.27 a = -0.51006 n = 2.4461 47 / 29 <<lamda: 288.23 SS= 2468911 at C = 17.269 a = -0.50963 n = 2.446 48 / 29 <<lamda: 115.29 SS= 2468895 at C = 17.269 a = -0.50975 n = 2.446 49 / 30 lamda: 1152.9 SS= 1.7977e+308 at C = 17.27 a = -0.51005 n = 2.4461 50 / 31 <<lamda: 461.17 SS= 2468891 at C = 17.269 a = -0.50978 n = 2.446 51 / 31 > print(p.nlxb) nlsr object: x residual sumsquares = 2468891 on 6 observations after 31 Jacobian and 51 function evaluations name coeff SE tstat pval gradient JSingval C 17.2692 1404 0.0123 0.991 -733.5 4112 a -0.509779 60.67 -0.008403 0.9938 35772 188.7 n 2.44601 31.75 0.07704 0.9434 -126335 0.6456 > sink() On 2017-05-04 06:58 PM, Zachary Shadomy wrote:> I am having some errors come up in my first section of code. I have no > issue in plotting the points. Is there an easier method for creating a > non-linear regression using C*(x+a)^n. The .txt file is named > stage_discharge with the two variables being stage and discharge. > The data is a relatively small file listed below: > > stage discharge > 6.53 2592.05 > 6.32 559.5782 > 5.96 484.2151 > 4.99 494.7527 > 3.66 456.0778 > 0.51 291.13 > > > > > >> power.nls<-nls(stage_dischargee$discharge~C*(stage_discharge$stage+a)^n, > data=stage_discharge, start=list(C=4, a=0, n=1)) >> C<-coef(power.nls)["C"] >> a<-coef(power.nls)["a"] >> n<-coef(power.nls)["n"] >> plot(stage_discharge$stage, stage_discharge$discharge, pch=17, cex=1.25, > ylab='Discharge (cfs )', xlab='Stage (ft)', font.lab=2, main='Boone Creek\n > St age-Discharge Curve') >> curve(C*(x+a)^n, add=TRUE, col="red") > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >
I have a fair bit of experience with both nls and rating curves. This is not a nls() problem, this is a model problem. The power law rating curve favored by hydrologists would not apply to your data as it's based on the idea that a log-log plot of discharge vs. stage, or state+a in your case is a straight line, statistical assumptions notwithstanding. A log-log plot of your data, plot(discharge~stage,data=yourdata,pch=19,log='xy') clear is not a straight line. The very large discharge at stage=6.53 vs. stage=6.32 says one of two things: 1) there is an error in the data (perhaps the 2592.05 should be 592.05) or 2) the river channel geometry has changed dramatically, as in overtopping its banks or perhaps there's a smaller central channel set into a larger flood channel, similar to the LA river of the movies. The way forward is 1) recheck your data or 2) recheck your data and use a two-piece model with one piece for stage <= 6.32 and a second piece for stage > 6.32. For this second approach to work, you'll need more data than you have given us here. BTW, nls() should work fine if the model/data combination meet the requirements of 1) the model 'fits' the data, 2) the residuals are NIID(0,sigma^2), the parameters C, a, and n are identifiable from the data (should be if the last point is excluded). As always, you'll need good starting values for the parameters (get them from a log-log plot). You may find, based on the residuals, that linear regression (lm, glm) are most appropriate so that the errors meet the criteria of constant variance. If none of this makes sense, buy and study the book Nonlinear regression analysis: Its applications, D. M. Bates and D. G. Watts, Wiley, New York, 1988. ISBN 0471-816434. The nls() application is the easy part. Good luck David Stevens On 5/4/2017 4:58 PM, Zachary Shadomy wrote:> I am having some errors come up in my first section of code. I have no > issue in plotting the points. Is there an easier method for creating a > non-linear regression using C*(x+a)^n. The .txt file is named > stage_discharge with the two variables being stage and discharge. > The data is a relatively small file listed below: > > stage discharge > 6.53 2592.05 > 6.32 559.5782 > 5.96 484.2151 > 4.99 494.7527 > 3.66 456.0778 > 0.51 291.13 > > > > > >> power.nls<-nls(stage_dischargee$discharge~C*(stage_discharge$stage+a)^n, > data=stage_discharge, start=list(C=4, a=0, n=1)) >> C<-coef(power.nls)["C"] >> a<-coef(power.nls)["a"] >> n<-coef(power.nls)["n"] >> plot(stage_discharge$stage, stage_discharge$discharge, pch=17, cex=1.25, > ylab='Discharge (cfs )', xlab='Stage (ft)', font.lab=2, main='Boone Creek\n > St age-Discharge Curve') >> curve(C*(x+a)^n, add=TRUE, col="red") > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.-- David K Stevens, P.E., Ph.D. Professor and Head, Environmental Engineering Civil and Environmental Engineering Utah Water Research Laboratory 8200 Old Main Hill Logan, UT 84322-8200 435 797 3229 - voice 435 797 1363 - fax david.stevens at usu.edu [[alternative HTML version deleted]]