繁体   English   中英

在 r 的 S 形曲线上找到一个点

[英]finding a point on a sigmoidal curve in r

这是一个数据集:

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

到目前为止,我知道如何拟合 sigmoidal 曲线并将其显示在屏幕上:

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

我现在想在 y = 50 时在 S 型曲线上找到一个点。我该怎么做?

SSlogis 适合的 function 在 function 的帮助中给出:

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

为简单起见,让我们将input更改为x并将此 function 设置为等于yfit您的代码):

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

我们需要反转这个 function 以在 LHS 上单独获得x ,以便我们可以从y计算x 这样做的代数在这个答案的末尾。

首先,让我们 plot 你原来的配合:

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

在此处输入图像描述

现在,我们将创建一个 function 来根据 y 值计算 x 值。 再一次,请参阅下面的代数来生成这个 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)

在此处输入图像描述

通过代数工作,让x单独出现在左侧。 请注意,在下面的代码中 p[1]=Asym、p[2]=xmid 和 p[3]=scal(由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])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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