简体   繁体   中英

how to predict x given y for nls model?

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!!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM