Interpolating data above the maximum value in a panel with R

With lm you can get the slope that this simple interpolation is using, and then use that slope to generate new values with predict. But maybe there’s a simpler solution

mod <- lm(index ~ year, a)

a[,2] <- predict(mod, newdata=data.frame(year=a$year))

EDIT 1

No, for each id we will run a different lm. To do that we select the part of a with a unique id inside a loop, and run the lm only with that part:

for(i in unique(a$id)){
  ai = a[a$id==i,]
  mod = lm(index ~ year, ai)
  a[a$id==i,3] = predict(mod, newdata=data.frame(year=ai$year))}

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top