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))
Which returns the error:
Error in inherits(object, "formula") : object 'z' not found
Is there a better way to try and allow a conditional variable to, well,
vary?
--
View this message in context: http://r.789695.n4.nabble.com/nls-and-if-statements-tp4630391.html
Sent from the R help mailing list archive at Nabble.com.
nls and if statements
7 messages · Rui Barradas, David Winsemius, DWatts +1 more
Hello,
I don't know if this is what you want, but your formula is equivalent to
nlstest=nls(Cfl~K*ifelse(Pw > z, 1, exp(-a*Tsoil))*exp(-b*Pw)+c
,start=c(K=5.5, a=0.1, b=0.1, c=2, z=5))
Hope this helps,
Rui Barradas
--
View this message in context: http://r.789695.n4.nabble.com/nls-and-if-statements-tp4630391p4630398.html
Sent from the R help mailing list archive at Nabble.com.
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) +
!(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.
Untested in absence of data.
David Winsemius, MD West Hartford, CT
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
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! -- View this message in context: http://r.789695.n4.nabble.com/nls-and-if-statements-tp4630391p4630410.html Sent from the R help mailing list archive at Nabble.com.
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
Peter Ehlers wrote
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
I hadn't realized there was such a package, and wow, do I ever wish I'd learned of that plot3d sooner! I did have a substantially larger data set to work with, but after a much more careful look I realized it would never fit the extra param to the low Pw numbers-- the predictability just isn't there. The 3d plot confirms this, of course. I did, however, get the help I was looking for, so thank you all! Danielle -- View this message in context: http://r.789695.n4.nabble.com/nls-and-if-statements-tp4630391p4630451.html Sent from the R help mailing list archive at Nabble.com.