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:
- 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 - X开发者_开发知识库small)
- 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
You could be looking at approx()
and approxfun()
... or I suppose you could fit with lm
for linear or lowess
for non-parametric fits.
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
....
精彩评论