简体   繁体   中英

finding a point on a sigmoidal curve in r

Here is a data set:

df <- data.frame('y' = c(81,67,54,49,41,25), 'x' =c(-50,-30,-10,10,30,50))

So far, I know how to fit a sigmoidal curve and display it on screen:

plot(df$y ~ df$x)
fit <- nls(y ~ SSlogis(x, Asym, xmid, scal), data = df)
summary(fit)
lines(seq(-100, 100, length.out = 100),predict(fit, newdata = data.frame(x = seq(-100,100, length.out = 100))))

I now want to find a point on the sigmoidal curve when y = 50. How do I do this?

The function that SSlogis is fitting is given in the help for the function as:

Asym/(1+exp((xmid-input)/scal))

For simplicity of notation, let's change input to x and we'll set this function equal to y (which is fit in your code):

y = Asym/(1+exp((xmid - x)/scal))

We need to invert this function to get x alone on the LHS so that we can calculate x from y . The algebra to do that is at the end of this answer.

First, let's plot your original fit:

plot(df$y ~ df$x, xlim=c(-100,100), ylim=c(0,120))
fit <- nls(y ~ SSlogis(x, Asym, xmid, scal), data = df)
lines(seq(-100, 100, length.out = 100),predict(fit, newdata = data.frame(x = seq(-100,100, length.out = 100))))

在此处输入图像描述

Now, we'll create a function to calculate the x value from the y value. Once again, see below for the algebra to generate this function.

# y is vector of y-values for which we want the x-values
# p is the vector of 3 parameters (coefficients) from the model fit
x.from.y = function(y, p) {
  -(log(p[1]/y - 1) * p[3] - p[2])
}

# Run the function
y.vec = c(25,50,75)
setNames(x.from.y(y.vec, coef(fit)), y.vec)
 25 50 75 61.115060 2.903734 -41.628799
# Add points to the plot to show we've calculated them correctly
points(x.from.y(y.vec, coef(fit)), y.vec, col="red", pch=16, cex=2)

在此处输入图像描述

Work through algebra to get x alone on the left side. Note that in the code below p[1]=Asym, p[2]=xmid, and p[3]=scal (the three parameters calculated by SSlogis ).

# Function fit by SSlogis
y = p[1] / (1 + exp((p[2] - x)/p[3]))

1 + exp((p[2] - x)/p[3]) = p[1]/y

exp((p[2] - x)/p[3]) = p[1]/y - 1

log(exp((p[2] - x)/p[3])) = log(p[1]/y - 1)

(p[2] - x)/p[3] = log(p[1]/y - 1)

x = -(log(p[1]/y - 1) * p[3] - p[2])

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