0

I am using the nls SSlogis function in R to make a model expressing water absorption in a plant, where y=water absorption (g per m^2), and x=minutes in water. For a given y value (in this case max water absorption), I would like to solve for x (the minute we reach peak absorption) using each model (I have a few species each with an individual model).

I have tried the uniroot() function, but I seem to be doing it wrong:

mod1_roots <- uniroot(mod1, lower=0, upper=600, y=max_y_mod1)

where lower and upper are the range of minutes, and mod1 is the SSlogis model in question

The error message is: Error in f(lower, ...) : could not find function "f"

So I assume I am not using the "function" part correctly and it should be something other than the model. Ugh.

Can anyone help a clueless botanist out? Thanks friends!

Edit for reproducibility (I hope):

Here's some real data

absorption <- c(297.0470936,262.809483,323.8243166,296.6731868,313.5985664,283.1004567,259.8724386,228.8903642,197.1585476,230.1674857,182.5799195,148.0262402,134.087096,77.98206413)

minutes <- c(195,181,167,157,147,135,105,83,63,45,33,24,10)

cembra <- as.data.frame(cbind(absorption,minutes))

cembra_mod <- nls(absorption ~ SSlogis(minutes, Asym, xmid, scal), cembra)

My goal is to predict the minutes at which absorption = 100 (or any specified number). Thanks!!

treee
  • 13
  • 3
  • Not sure what you're trying, but if you want to revert SSlogis(x, a,b,c..) then the function should be `function(x) SSlogis(x, a,b,c...)` instead mod (despite logistic being analytically invertible). – Ric Dec 15 '22 at 13:11
  • Thanks @RicVillalba ! I may have misunderstood, but when I try with real values: 'mod1_roots <- uniroot(function(x) SSlogis(x, 205.569308 , -6.008204 , 84.069670 ), lower=0, upper=600, y=0.610389630515968)' Then I get the error: 'Error in f(lower, ...) : unused argument (y = 0.610389630515968)' – treee Dec 15 '22 at 14:26
  • `y` is not a parameter of neither uniroot nor sslogis. – Ric Dec 15 '22 at 15:05

1 Answers1

0

When posting to SO please provide a complete self contained reproducible example that others can run to reproduce the problem. We will provide a reproducible example for you this time to show how it is done. ChickWeight below comes with R. This shows how to find what Time corresponds to a weight of 200 assuming that the value is within the range of the input Time values. The fact that the objective in the output is zero shows that it worked.

Chick.1 <- ChickWeight[ChickWeight$Chick == 1, ]
fm <- nls(weight ~ SSlogis(Time, Asym, xmid, scal), Chick.1)
pred <- function(x) predict(fm, list(Time = x))
optimize(function(x) (pred(x) - 200)^2, range(Chick.1$Time))

giving:

$minimum
[1] 20.34701

$objective
[1] 2.498361e-08
attr(,"gradient")
          Asym      xmid     scal
[1,] 0.2134402 -13.79298 17.99035
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
  • Thanks for your help and patience @G.Grothendieck ! I edited my question to be reproducible. My goal is to predict minutes when a given level of absorption is attained. Sorry for the confusion, I'm a new user of this forum – treee Dec 15 '22 at 20:07