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?