Page 1 of 1
Interpolate if gap is not larger than 3 obs
Posted: Thu Nov 19, 2015 9:53 am
by strypste
Dear All
I want to interpolate a series but it can only interpolate if the number of missing observation is 3 or smaller.
So for example if I have a series like this:
14.6301
NA
NA
16.969
NA
NA
NA
NA
NA
18.4762
20.5717
then the command should interpolate the first gap but not the second gap.
Any ideas?
Best
s
Re: Interpolate if gap is not larger than 3 obs
Posted: Thu Nov 19, 2015 10:29 am
by EViews Gareth
There's nothing built in that will do it. You'll have to write a program.
Luckily I found an ancient program that I wrote that performs simple linear interpolation on up to 2 missing values in a row. Should be fairly straight forward to add an additional 3rd.
Code: Select all
create u 100
rndseed 1
series y=nrnd
smpl if nrnd>0.8
y = na
smpl @all
series temp=y
'interpolation of two missings
smpl if y = na and y(1) = na
temp = (y(-1) + y(2))/3
smpl if y=na and y(-1) = na
temp = (y(-2)+y(1))*2/3
'interpolation of a single missing
smpl if y = na and temp=na
temp = (y(-1)+y(+1))/2
smpl @all
show temp y
Re: Interpolate if gap is not larger than 3 obs
Posted: Thu Nov 19, 2015 10:38 am
by strypste
Thanks for providing the program.
Cheers
Re: Interpolate if gap is not larger than 3 obs
Posted: Thu Nov 19, 2015 11:34 am
by strypste
I think there was a small error in the code related to the interpolation of two gaps... The following code produces exactly the same as the
ipolate command.
Code: Select all
create u 100
rndseed 1
series y=nrnd
smpl if nrnd>0.8
y = na
smpl @all
series temp=y
' Interpolate y with the built in command (so this will also fill gaps larger than 2)
y.ipolate y_i
'interpolation of two missings
' first missing
smpl if y = na and y(1) = na
temp = (2*y(-1) + y(2))/3 ' This is slightly different compared to previous code
' second missing
smpl if y=na and y(-1) = na
temp = (y(-2)+2*y(1))/3 ' This is slightly different compared to previous code
'interpolation of a single missing
smpl if y = na and temp=na
temp = (y(-1)+y(+1))/2
smpl @all
show temp y y_i