[R] Non-Linear Regression Help
David Stevens
david.stevens at usu.edu
Wed May 10 17:43:15 CEST 2017
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]]
More information about the R-help
mailing list