R – Linear interpolation in R

interpolationrstatistics

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

Best Solution

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

Related Question