计算 R 中线图的积分 [英] Computing integral of a line plot in R

查看:56
本文介绍了计算 R 中线图的积分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 R 中有两个相同长度的正值向量 x,y.使用 plot(x, y, "l",...),从我的有限向量中给我一个二维连续线图x 和 y.有没有办法计算 R 中这条线图的某个范围内的定积分?

edit1:我研究了 R 中的积分函数.但是我不确定如何从两个向量中创建一个函数来传递给它,因为我的向量都是有限的.

edit2:对于一些更多的背景,x 和 y 的长度 ~ 10,000.我编写了一个函数来查找我观察到的数据中异常的周期 [xi, xj].对于这些异常中的每一个,我都使用 plot 来查看这些数据片段中发生了什么.现在我需要计算关于这些异常时期积分值的统计数据,所以我试图获得尽可能准确的数字以与我的图表相匹配.X 是一个时间变量,我采用了非常精细的时间间隔.

解决方案

approxfun 函数将采用 2 个向量并返回一个函数,该函数给出点之间的线性插值.然后可以将其传递给像 integrate 这样的函数.splinefun 函数也将进行插值,但基于样条而不是分段线性.

在分段线性情况下,积分将只是梯形的总和,将梯形的面积(宽度,x 的差值)求和可能更快/更简单

I have two positive-valued vectors x,y of the same length in R. Using plot(x, y, "l",...), gives me a continuous line plot in 2 dimensions out of my finite vectors x and y. Is there a way to compute a definite integral over some range of this line plot in R?

edit1: I've looked into the integrate function in R. I'm not sure however how to make a function out of two vectors to pass to it, as my vectors are both finite.

edit2: For some more background, The length of x and y ~ 10,000. I've written a function to find periods, [xi, xj], of abnormalities in the data I'm observing. For each of these abnormalities, I've used plot to see what's going on in these snippets of my data. Now i need to compute statistics concerning the values of the integrals in these abnormal periods, so I'm trying to get as accurate as a number as possible to match with my graphs. X is a time variable, and I've taken very fine intervals of time.

解决方案

The approxfun function will take 2 vectors and return a function that gives the linear interpolation between the points. This can then be passed to functions like integrate. The splinefun function will also do interpolation, but based on a spline rather than piecewise linear.

In the piecewise linear case the integral will just be the sum of the trapezoids, it may be faster/simpler to just sum the areas of the trapezoids (the width, difference in x's

这篇关于计算 R 中线图的积分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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