Linear interpolation in R

前端 未结 2 728
半阙折子戏
半阙折子戏 2021-02-05 10:23

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         


        
相关标签:
2条回答
  • 2021-02-05 10:45

    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
    ....
    
    0 讨论(0)
  • 2021-02-05 10:48

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

    0 讨论(0)
提交回复
热议问题