R 中的线性插值

发布于 2024-11-24 04:11:48 字数 1149 浏览 0 评论 0原文

我有一个真实数据的数据集,例如如下所示:

# 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 值是多少?”

 # 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 值,其中 ablines 与已知数据的线性插值相交

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

所以,在理想情况下,我会用我的已知数据创建一个“线性插值模型”,例如

model <- linearInterpol(known)

......然后我可以询问 Y 值,例如

model$getEstimation(0.3)

(在本例中应该给出“3”)

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

我怎样才能实现这一点?我会手动对每个值执行如下操作:

  1. 比当前 X 值 更小的最近的 X 值 Xsmall 和更大的最近的 X 值 Xlarge 是多少>X。
  2. 计算与较小 X 值的相对位置 relPos = (X - Xsmall) / (Xlarge - Xsmall)
  3. 计算预期 Y 值 Yexp = Ysmall + (relPos * (Ylarge - Ysmall) ))

至少对于Matlab这个软件我听说有一个针对此类问题的内置函数。

谢谢你的帮助,

斯文

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

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)

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

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

model <- linearInterpol(known)

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

model$getEstimation(0.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. 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))

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

Thanks for your help,

Sven

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自在安然 2024-12-01 04:11:51

为了跟进 DWin 的答案,以下是如何使用线性模型获得预测值的方法。

model.lm <- lm(y ~ x, data = known)

# Use predict to estimate the values for aim.
# Note that predict expects a data.frame and the col 
# names need to match
newY <- predict(model.lm, newdata = data.frame(x = aim))

#Add the predicted points to the original plot
points(aim, newY, col = "red")

当然,您可以直接检索这些预测值:

> cbind(aim, newY)
  aim       newY
1 0.3  2.4500000
2 0.7  6.1928571
3 2.3 21.1642857
....

To follow up on DWin's answer, here's how you'd get the predicted values using a linear model.

model.lm <- lm(y ~ x, data = known)

# Use predict to estimate the values for aim.
# Note that predict expects a data.frame and the col 
# names need to match
newY <- predict(model.lm, newdata = data.frame(x = aim))

#Add the predicted points to the original plot
points(aim, newY, col = "red")

And of course you can retrieve those predicted values directly:

> cbind(aim, newY)
  aim       newY
1 0.3  2.4500000
2 0.7  6.1928571
3 2.3 21.1642857
....
独木成林 2024-12-01 04:11:50

您可以查看 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文