I have a panel with missing values that I need to interpolate.
a <- data.frame(id= c(1,1,1,1,1,1,1,2,2,2,2,2,2,2), year=1:7, index=c(1,NA,NA,NA,3,NA,NA, 2,NA,NA,NA,5,NA,NA))
The issue is that I don't have the final value, so I would like to use the same interpolation line to predict values above the last observation reported. Additionally, the interpolation should be "by id", rather than considering the first observation of the next id as part of the line.
I've tried the basic interpolation but it does stop at the latest values, or predicts using the next id:
a <- na.approx(a)
output I get:
id year index
[1,] 1 1 1.000000
[2,] 1 2 1.500000
[3,] 1 3 2.000000
[4,] 1 4 2.500000
[5,] 1 5 3.000000
[6,] 1 6 2.666667
[7,] 1 7 2.333333
[8,] 2 1 2.000000
[9,] 2 2 2.750000
[10,] 2 3 3.500000
[11,] 2 4 4.250000
[12,] 2 5 5.000000
[13,] 2 6 NA
[14,] 2 7 NA
desired output:
id year index
[1,] 1 1 1.000000
[2,] 1 2 1.500000
[3,] 1 3 2.000000
[4,] 1 4 2.500000
[5,] 1 5 3.000000
[6,] 1 6 3.500000
[7,] 1 7 4.000000
[8,] 2 1 2.000000
[9,] 2 2 2.750000
[10,] 2 3 3.500000
[11,] 2 4 4.250000
[12,] 2 5 5.000000
[13,] 2 6 5.750000
[14,] 2 7 6.500000