问题描述
我有一个真实数据集,例如:
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:
- 与当前X值
X
相比,最接近的X值Xsmall
和最接近的X值Xlarge
是什么. - 计算相对较小X值的相对位置
relPos = (X - Xsmall) / (Xlarge - Xsmall)
- 计算期望的Y值
Yexp = Ysmall + (relPos * (Ylarge - Ysmall))
- What is the closest X-value smaller
Xsmall
and the closest X-value largerXlarge
than the current X-valueX
. - Calculate the relative position to the smaller X-Value
relPos = (X - Xsmall) / (Xlarge - Xsmall)
- 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中的线性插值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!