Home > other >  is it possible to make an inverted prediction with an nlsList object?
is it possible to make an inverted prediction with an nlsList object?

Time:11-13

I have fitted an nlsList model with my data and now I need to estimate the value of "x" corresponding to a given "y" using my model.

I have tried with "invest" from investr package, but it works only with objects from class lm, glm, nls, or lme.

CodePudding user response:

When posting to SO the question needs to provide a reproducible example for testing including input, code and expected output. See the instructions at the top of the tag page. I have done it for you this time in the Note at the end.

Create a function pred which predicts Sepal.Width given Sepal.Length and Species. Then we can use uniroot as shown:

library(nlme)
# run code in Note

pred <- function(Sepal.Length, Species) {
  predict(fm, list(Sepal.Length = Sepal.Length, Species = Species))
}
uniroot(function(x) pred(x, "virginica") - 3, c(-10, 10))

This gives the following which says that the Sepal.Length corresponding to a Sepal.Width of 3 and Species virginica is 6.7168.

$root
[1] 6.716803

$f.root
   virginica 
2.524802e-06 
attr(,"label")
[1] "Predicted values"

$iter
[1] 5

$init.it
[1] NA

$estim.prec
[1] 6.103516e-05

Note

Reproducible example using built=in iris data set follows. We assume that given Sepal.Width = 3 and Species = "virginica" that we want to find Sepal.Length.

library(nlme)
fm <- nlsList(Sepal.Width ~ a * exp(Sepal.Length*b) | Species, data = iris, 
  start = list(a = 0.5, b = 0.5))
  • Related