R中的线性插值 [英] Linear interpolation in R

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

问题描述

我有一个真实数据集,例如:

I have a dataset of real data, for example looking like this:

# Dataset 1 with known data
known <- data.frame(
    x = c(0:6),
    y = c(0, 10, 20, 23, 41, 39, 61)
)

plot (known$x, known$y, type="o")

现在我想提一个问题 如果原始数据集的所有中间数据点位于周围的测量值之间的直线上,则0.3的Y值将是多少?"

Now I want to get an aswer to the question "What would the Y value for 0.3 be, if all intermediate datapoints of the original dataset, are on a straight line between the surrounding measured values?"

 # X values of points to interpolate from known data
 aim <- c(0.3, 0.7, 2.3, 3.3, 4.3, 5.6, 5.9)

如果您查看图表,我想获取Y值,其中的斜线与已知数据的线性插值相交

If you look at the graph: I want to get the Y-Values, where the ablines intersect with the linear interpolation of the known data

abline(v = aim, col = "#ff0000")

因此,在理想情况下,我将使用已知数据创建"linearInterpolationModel"

So, in the ideal case I would create a "linearInterpolationModel" with my known data, e.g.

model <- linearInterpol(known)

...然后我可以要求输入Y值,例如

... which I can then ask for the Y values, e.g.

model$getEstimation(0.3)

(在这种情况下,应给出"3")

(which should in this case give "3")

abline(h = 3, col = "#00ff00")

我怎么能意识到这一点?我会手动为每个值执行以下操作:

How can I realize this? Manually I would for each value do something like this:

  1. 与当前X值X相比,最接近的X值Xsmall和最接近的X值Xlarge是什么.
  2. 计算相对较小X值的相对位置relPos = (X - Xsmall) / (Xlarge - Xsmall)
  3. 计算期望的Y值Yexp = Ysmall + (relPos * (Ylarge - Ysmall))
  1. What is the closest X-value smaller Xsmall and the closest X-value larger Xlarge than the current X-value X.
  2. Calculate the relative position to the smaller X-Value relPos = (X - Xsmall) / (Xlarge - Xsmall)
  3. Calculate the expected Y-value Yexp = Ysmall + (relPos * (Ylarge - Ysmall))

至少听说过我对Matlab软件有内置的功能来解决这些问题.

At least for the software Matlab I heard that there is a built-in function for such problems.

感谢您的帮助,

斯文

推荐答案

您可能正在研究approx()approxfun() ...,或者我想您可以将lm用于线性或将lowess用于非线性参数拟合.

You could be looking at approx() and approxfun() ... or I suppose you could fit with lm for linear or lowess for non-parametric fits.

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

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