Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 691
B C Avatar asked Oct 17 '19 01:10

B C


People also ask

What is sigmoid midpoint?

midpoint: The time point at which the sigmoidal curve reaches half of its maximum intensity.

What is sigmoid function in R?

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.

What does a sigmoidal curve show?

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.

How do I fit a curve to data in R?

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.


Video Answer


1 Answers

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

enter image description here

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)

enter image description here

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])
like image 60
eipi10 Avatar answered Sep 28 '22 02:09

eipi10