0

Will anyone educate me on how to interpolate and extrapolate in R. For example, take a 2D dataframe with ages and associated values. The end result is that ages that increment by one year up to a certain value. Take the below sample data:

df <- data.frame(
  age = c(20, 21, 23, 25, 27, 29, 30),
  value = c(1.1, 1.2, 1.4, 1.6, 1.8, 2.0, 2.1)
)

How would I get a data frame that has interpolated values with missing ages (i.e. age 22 == 1.3) and extrapolated values for ages 31-60 (for either linear or other technique of extrapolation)?

The end result is a data frame with ages 20 through 60 incremented by 1 and values for each age.

I've tried techniques here but didn't get good results: Linear interpolation in R

The accepted solution didn't work. I tried one of the non accepted solutions and I keep getting an error that states invalid type (list) for variable 'age'.

This is what generated the error:

 age2 <- data.frame(31,32,33)
 value2 <- data.frame(0,0,0)
 df2 <- do.call(rbind, Map(data.frame, age = age2, value = value2))

model.lm <- lm(data=df, value~age)
predict(model.lm, newdata= data.frame(x = age2))

Even this is more of an extrapolation then an interpolation.

I'm judging the output by output excel gives from its linear function. See interpolated and extrapolated values in red. Interpolated values are done with the excel formula Value+(Value-Next Value)/(Row(value)-Row(next value). As I learn R, I would like do away with excel. Is this process or output repeatable?

enter image description here

Jordan
  • 1,415
  • 3
  • 18
  • 44
  • How exactly did you try to use the solutions from the other questions? What did your code look like. What does "didn't get good results" mean exactly? How did you judge the quality of the results? – MrFlick Jul 06 '18 at 19:39
  • @李哲源. Is this a better question. Will you open it back up? – Jordan Jul 06 '18 at 20:11
  • @李哲源 Predict was giving me trouble but at second glance, that doesn't, or at least does not look like it will give me the output I desire (i.e. the example) – Jordan Jul 06 '18 at 20:16
  • thank you @李哲源. Do you want to put an answer so I can check as a solution? – Jordan Jul 06 '18 at 20:20
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/174525/discussion-between-jordan-and-). – Jordan Jul 06 '18 at 20:22

0 Answers0