[R] non linear regression with nls

lauramorgana at bluewin.ch lauramorgana at bluewin.ch
Tue Feb 3 11:33:07 CET 2009


Hello,
I'm a beginner with R and it's the first time I'm using the R-help list... I hope I'm in the right place, if not: 
Sorry!!

I need to do non linear regressions on a data set which columns are:
"river.name"    "Portata"  "PTG.P"   "PO4.P"   "NT.N"    "NH4.N"   "NO3.N"   "BOD5"    "SiO2"   
I need to predict every variable (PTG, PO4, NT, ..., which are concentration of substances in water) starting from 
the "Portata" variable (which is the water flow)
The functions that I'm using are:
> fz1<-function(Portata, a, b){a+b/Portata}
> fz2<-function(Portata, a, b){a*exp(b*Portata)}
> fz3<-function(Portata, a, b, d, e){a+b/Portata+d*(Portata^e)}
> fz4<-function(Portata, b, d){b*Portata^d}
> fz5<-function(Portata, a, b, d){a+b*(Portata^d)}
I've made a list of the functions with list(fz1, fz2, fz3, fz4, fz5)

and the starting , lower and upper parameters for each function are:
fz1: start=list(a=10, b=10), lower=list(a=0, b=0), upper=list(a=1000, b=1000) 
fz2: start=list(a=10, b=1), lower=list(a=0, b=0), upper=list(a=1000, b=1000)
fz3: start=list(a=10, b=10, d=10, e=10), lower=list(a=0, b=0, d=0, e=-50), upper=list(a=1000, b=1000, d=1000, e=50) 
fz4: start=list (a=10, b=1), lower=list(a=0, b=-50), upper=list(a=1000, b=50)
fz5: start=list(a=10, b=10, d=1), lower=list(a=0, b=0, d=-50), upper=list(a=1000, b=1000, d=50)

so far i manage to do non linear regression one at a time that is, using one function for one river) using nls(), for 
example:
r.NT.lav<-nls(NT.N~ fz1(Portata, a,b), data=subset(dati,river.name=="Laveggio"), start=list(a=10, b=10), nls.control
(maxiter=200), algorithm='port', trace=TRUE, na.action=na.omit, lower=list(a=0, b=0), upper=list(a=1000, b=1000))
and then I get the results with summary() and the graph using plotfit()

but this will get extremly long since I have 12 rivers to analize for every variable and then compare the results, so 
I'd like to use a loop (cycle for??) but I can't figure out how it works. I've tried to look on the help page on ?
Control (control flow) but I didn't understand it... 
Can somebody help me (give me a hint or an example of a loop) to automize the regression and save the results
Please consider that my knowledge of computer programming is practically non-existent!!
Thanks a lot!
Laura  F.




More information about the R-help mailing list