[R] nls and if statements

David Winsemius dwinsemius at comcast.net
Thu May 17 17:13:31 CEST 2012


On May 17, 2012, at 11:08 AM, David Winsemius wrote:

>
> On May 17, 2012, at 10:06 AM, DWatts wrote:
>
>> Hi All,
>>
>> I have a situation where I want an 'if' variable to be  
>> parameterized. It's
>> entirely possible that the way I'm trying to do this is wrong,  
>> especially
>> given the error message I get that indicates I can't do this using  
>> an 'if'
>> statement.
>>
>> Essentially, I have data where I think a relationship enters when a  
>> variable
>> (here Pwd) is below some value (z). I don't know that value, so I  
>> want to
>> fit it. The data is Pw, Tsoil, and Cfl.
>>
>> nlstest=nls(if(Pw>z){Cfl~K*exp(-b*Pw)+c
>> 	}else{
>> 	Cfl~K*(exp(-a*Tsoil))*exp(-b*Pw)+c
>> 	},start=c(K=5.5, a=0.1, b=0.1, c=2, z=5))
>>
>
> Perhaps recasting as Boolean equivalent:
>
> nlstest== <- nls({Cfl~ (Pw>z)*K*exp(-b*Pw)+c) +
#needs another open-paren      ^
>                       !(Pw>z)*(K*(exp(-a*Tsoil))*exp(-b*Pw)+c)},
>             start=c(K=5.5, a=0.1, b=0.1, c=2, z=5))


nlstest== <- nls({Cfl~ (Pw>z)*( K*exp(-b*Pw)+c) +
                       !(Pw>z)*(K*(exp(-a*Tsoil))*exp(-b*Pw)+c)},
             start=c(K=5.5, a=0.1, b=0.1, c=2, z=5))

>
>> and provide commented, minimal, self-contained, reproducible code.
>
Still untested in absence of data.
>
> -- 

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list