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?
midpoint: The time point at which the sigmoidal curve reaches half of its maximum intensity.
sigmod-function-r. The Sigmoid function is also known as the S function (it has shape of S). The function can be used to map values to (0, 1) so the input can be from negative infinity to infinity. The Sigmoid function is used in the Logistic Regression.
In red blood cells, the oxygen-binding curve for hemoglobin displays an “S” shaped called a sigmoidal curve. A sigmoidal curve shows that oxygen binding is cooperative; that is, when one site binds oxygen, the probability that the remaining unoccupied sites that will bind to oxygen will increase.
To fit a curve to some data frame in the R Language we first visualize the data with the help of a basic scatter plot. In the R language, we can create a basic scatter plot by using the plot() function. where, df: determines the data frame to be used.
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])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With