r - 如何确定一个点是高于还是低于 R 中连接点的线?

标签 r regression curve-fitting

目标:

Given a set of data, how can I determine if a new data point is above or below the line connecting the points.

例如,我如何确定显示的红点是在线上方还是下方(无需目视检查)?

![enter image description here

我想用精确的线拟合这些点。本质上是连接点,需要一个合适的点才能将线上的任何点用作比较器。

当前尝试:

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

enter image description here

我认为,一旦我得到正确的拟合,就可以直接使用它来确定点是在线上方还是下方(例如,使用预测或函数),但我需要拟合方面的帮助。

也完全欢迎另一种方法。

数据:

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/

相关文章:

python - 为什么 GridSearchCV 不给出最好的分数? - Scikit 学习

python - 如何将不同维度的多个分类输入变量用于随机森林回归模型?

r - 为什么在包含 R 中的因素的方差分析中删除变量不会降低自由度?

python - python中使用leastsq同时拟合数据

r - 将类 "perm.htest"对象从(多重排列 t 检验)转换为数据框

r - 我可以将 cranvas 交互式图形嵌入到 R 中的 gWidgets (gWidgetsQt) 中吗

r - 在R中使用lm和nls进行正弦曲线拟合

gnuplot - 为什么我对对数函数的拟合看起来如此错误

将数字重新编码为字符串不会替换所有值

r - 如何在R中生成0和1之间没有0和1的随机数?