R-定位两条曲线的交点 [英] R - locate intersection of two curves

查看:559
本文介绍了R-定位两条曲线的交点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此论坛上,关于定位拟合模型和一些原始数据之间的交点有很多问题.但是,就我而言,我处于早期项目中,我仍在评估数据.

There are a number of questions in this forum on locating intersections between a fitted model and some raw data. However, in my case, I am in an early stage project where I am still evaluating data.

首先,我创建了一个数据框,其中包含一个理想值应为1.0的比率值.我已经绘制了数据框,还使用了abline()函数在y=1.0处绘制了一条水平线.该水平线和比率图在某个点相交.

To begin with, I have created a data frame that contains a ratio value whose ideal value should be 1.0. I have plotted the data frame and also used abline() function to plot a horizontal line at y=1.0. This horizontal line and the plot of ratios intersect at some point.

plot(a$TIME.STAMP, a$PROCESS.RATIO,
     xlab='Time (5s)',
     ylab='Process ratio',
     col='darkolivegreen',
     type='l')
abline(h=1.0,col='red')

我的目标是找到相交点,例如x,并在x±k处绘制两条垂直线,分别为abline(v=x-k)abline(v=x+k),其中k是一定的公差带.

My aim is to locate the intersection point, say x and draw two vertical lines at x±k, as abline(v=x-k) and abline(v=x+k) where, k is certain band of tolerance.

在图上应用网格并不是真正的选择,因为该图将成为多面板图的一部分.并且,由于比率数据的布局非常紧密,因此该图不会太易读.最后,x±k在我与领域专家的讨论中将非常有价值.

Applying a grid on the plot is not really an option because this plot will be a part of a multi-panel plot. And, because ratio data is very tightly laid out, the plot will not be too readable. Finally, the x±k will be quite valuable in my discussions with the domain experts.

您能指导我如何实现这一目标吗?

Can you please guide me how to achieve this?

推荐答案

以下是两种解决方案.第一个使用locator(),如果您没有太多图表可以生成,则将非常有用:

Here are two solutions. The first one uses locator() and will be useful if you do not have too many charts to produce:

x <- 1:5
y <- log(1:5)
df1 <-data.frame(x= 1:5,y=log(1:5))
k <-0.5

plot(df1,type="o",lwd=2)
abline(h=1, col="red")
locator()

通过单击交点(并在图表的左上方停止定位器),您将获得交点:

By clicking on the intersection (and stopping the locator top left of the chart), you will get the intersection:

> locator()
$x
[1] 2.765327
$y
[1] 1.002495

然后您将添加abline(v=2.765327).

如果您需要一种更可编程的方式来找到交点,则我们将不得不估算数据的功能.不幸的是,您尚未向我们提供PROCESS.RATIO,因此我们只能猜测您的数据是什么样子.希望数据是顺利的.这是适用于非线性数据的解决方案.如上图所示,R所做的只是在点之间画一条线.因此,我们必须在其中拟合一条曲线.在这里,我用2阶的多项式拟合数据.如果数据的线性度较低,则可以尝试增加阶数(此处为2).如果数据是线性的,请使用简单的lm.

If you need a more programmable way of finding the intersection, we will have to estimate the function of your data. Unfortunately, you haven’t provided us with PROCESS.RATIO, so we can only guess what your data looks like. Hopefully, the data is smooth. Here’s a solution that should work with nonlinear data. As you can see in the previous chart, all R does is draw a line between the dots. So, we have to fit a curve in there. Here I’m fitting the data with a polynomial of order 2. If your data is less linear, you can try increasing the order (2 here). If your data is linear, use a simple lm.

fit <-lm(y~poly(x,2))
newx <-data.frame(x=seq(0,5,0.01))
fitline = predict(fit, newdata=newx)

est <-data.frame(newx,fitline)

plot(df1,type="o",lwd=2)
abline(h=1, col="red")
lines(est, col="blue",lwd=2)

使用该拟合曲线,我们可以找到最接近y = 1的点.一旦有了该点,就可以在相交处和+/- k处绘制垂直线.

Using this fitted curve, we can then find the closest point to y=1. Once we have that point, we can draw vertical lines at the intersection and at +/-k.

cross <-est[which.min(abs(1-est$fitline)),] #find closest to 1

plot(df1,type="o",lwd=2)
abline(h=1)
abline(v=cross[1], col="green")
abline(v=cross[1]-k, col="purple")
abline(v=cross[1]+k, col="purple")

这篇关于R-定位两条曲线的交点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆