查找给定 y 值的 x 值

Finding x-value for a given y-value

对于给定的 x、y 系列,我需要拟合样条。然后在给定的 y 值(比如 0.65)下,我想绘制一条切线并提取其对应的 x 值。我使用 smooth.spline 函数来拟合曲线。

spl <- smooth.spline(y ~ x)

我的数据如下。任何帮助我应该怎么做。

structure(list(x = c(0.55, 0.575, 0.6, 0.625, 0.65, 0.675, 0.7, 
0.725, 0.75, 0.775, 0.8, 0.825, 0.85, 0.875, 0.9, 0.925, 0.95, 
0.975, 1, 1.025, 1.05, 1.075, 1.1, 1.125, 1.15, 1.175, 1.2, 1.225, 
1.25, 1.275, 1.3, 1.325, 1.35), y = c(0, 0.004065041, 0.02173913, 
0.067164179, 0.166666667, 0.357142857, 0.642857143, 0.907407407, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1)), .Names = c("x", "y"), class = "data.frame", row.names = c(NA, 
-33L))

也有类似的解决方法!但是这个例子中的新数据是 x 轴上的一个点,而在我的例子中,新数据是 y 轴上的一个点。

您可以使用 unitroot 找到 y 匹配的点:

x <- seq(0,40)
y <- pnorm(seq(0,40), mean=25, sd=5)
spl <- smooth.spline(y ~ x)

newy <- 0.85
newx <- uniroot(function(x) predict(spl, x, deriv = 0)$y - newy,
                interval = c(0, 40))$root

plot(x, y)
lines(spl, col=2)
points(newx, newy, col=3, pch=19)

关于算法,我们从?uniroot得到:

uniroot() uses Fortran subroutine ‘"zeroin"’ (from Netlib) based on algorithms given in the reference below. They assume a continuous function (which then is known to have at least one root in the interval).

[...]

Based on ‘zeroin.c’ in http://www.netlib.org/c/brent.shar.

[...]

Brent, R. (1973) Algorithms for Minimization without Derivatives. Englewood Cliffs, NJ: Prentice-Hall.

另见 https://en.wikipedia.org/wiki/Brent%27s_method