[R] nls and if statements

Peter Ehlers ehlers at ucalgary.ca
Thu May 17 19:34:03 CEST 2012


On 2012-05-17 08:40, DWatts wrote:
>
> David Winsemius wrote
>>
>>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>> Still untested in absence of data.
>>
>
> Thank you, and my apologies for not giving some sample numbers. Below is a
> short subset of the data set I'm working with.
>
> Cfl=c(2.61,4.21,2,2.75,7.47,1.2,3.24,12.49,2.37,3.28,4.3,2.61,2.75,2.92,3.78,2.25,2.84,3.33,2.39)
> Tsoil=c(30.01,27.72,34.91,21.96,22.83,29.65,20.56,23.72,33.96,31.57,19.15,28.49,30.02,20.14,30.23,33.99,32.58,17.87,19.78)
> Pw=c(30,29,24,22,22,41.2,-33.1,-49.21,29.6,24.5,-6.1544,42.8,63,55.5,43.3,38.5,87.2,-0.32,79.5)
>
> Rui's code seems to elegantly do what I was attempting (and will try the
> Boolian version next), but I'm having initial parameterization problems.
> I've tried doing what I've done before, which is use the excel solver to
> give me some simple early estimates of params, but it doesn't appear to be
> helping in this case.
>
> nlstest=nls(Cfl~K*ifelse(Pw>  z, 1, exp(-a*Tsoil))*exp(-b*Pw)+c,
> 	start=c(K=0.14, a=0.01, b=0.04, c=3.5, z=5), trace=T)
>
> I'll keep chugging away at this. Thank you for your help!
>

If the above data is all you have, I wouldn't chugg too long.
Looking at plots of Cfl~Pw and Cfl~Tsoil says to me that you
have far too much variability to fit your model.

You might find this plot instructive:

   library(rgl)
   plot3d(Pw, Tsoil, Cfl, col=1+(Pw<5), size=5)


Peter Ehlers



More information about the R-help mailing list