目标:
Given a set of data, how can I determine if a new data point is above or below the line connecting the points.
例如,我如何确定显示的红点是在线上方还是下方(无需目视检查)?
我想用精确的线拟合这些点。本质上是连接点,需要一个合适的点才能将线上的任何点用作比较器。
当前尝试:
So far I've tried fitting various splines to the data, but it is still a bit too smooth. I'm really looking for an exact fit to the data, sharp corners and all.
我尝试了自然样条曲线(以及平滑样条曲线),但不能完全准确/锐利地拟合:
plot(df$lowerx, df$lowery, type='b', xlab='x', ylab='y', pch=21, bg='steel blue')
myspline <- splinefun(df$lowerx, df$lowery, method='natural')
curve(myspline, add=T, from = 0, to=140, n = 100, col='green')
我认为,一旦我得到正确的拟合,就可以直接使用它来确定点是在线上方还是下方(例如,使用预测或函数),但我需要拟合方面的帮助。
也完全欢迎另一种方法。
数据:
df <- structure(list(lowerx = c(11.791, 18.073, 23.833, 35.875, 39.638, 44.153, 59.206, 71.498, 83.289, 95.091, 119.676, 131.467, 143.76), lowery = c(5.205, 5.89, 6.233, 9.041, 10, 10.342, 12.603, 13.493, 14.658, 15.274, 15.89, 15.616, 15.342)), .Names = c("lowerx", "lowery"), class = "data.frame", row.names = c(NA, -13L))
最佳答案
R 函数 approxfun 将创建一个进行线性插值的函数。
> F <- approxfun(x=df$lowerx, y=df$lowery)
> F(80) > 13
[1] TRUE
我使用您提供的数据并测试了我对“红点”坐标的最佳猜测为 (80, 13),所以它说 13 小于插值,而 (80,15) 大于:
> F(80) > 15
[1] FALSE
关于r - 如何确定一个点是高于还是低于 R 中连接点的线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34363467/