Title stata.
com
ipolate — Linearly interpolate (extrapolate) values
Syntax Menu Description Options
Remarks and examples Methods and formulas Reference Also see
Syntax
ipolate yvar xvar if in , generate(newvar) epolate
by is allowed; see [D] by.
Menu
Data > Create or change data > Other variable-creation commands > Linearly interpolate/extrapolate values
Description
ipolate creates in newvar a linear interpolation of yvar on xvar for missing values of yvar.
Because interpolation requires that yvar be a function of xvar, yvar is also interpolated for tied
values of xvar. When yvar is not missing and xvar is neither missing nor repeated, the value of
newvar is just yvar.
Options
generate(newvar) is required and specifies the name of the new variable to be created.
epolate specifies that values be both interpolated and extrapolated. Interpolation only is the default.
Remarks and examples [Link]
Example 1
We have data points on y and x, although sometimes the observations on y are missing. We believe
that y is a function of x, justifying filling in the missing values by linear interpolation:
. use [Link]
. list, sep(0)
x y
1. 0 .
2. 1 3
3. 1.5 .
4. 2 6
5. 3 .
6. 3.5 .
7. 4 18
. ipolate y x, gen(y1)
(1 missing value generated)
. ipolate y x, gen(y2) epolate
1
2 ipolate — Linearly interpolate (extrapolate) values
. list, sep(0)
x y y1 y2
1. 0 . . 0
2. 1 3 3 3
3. 1.5 . 4.5 4.5
4. 2 6 6 6
5. 3 . 12 12
6. 3.5 . 15 15
7. 4 18 18 18
Example 2
We have a dataset of circulations for 10 magazines from 1980 through 2003. The identity of the
magazines is recorded in magazine, circulation is recorded in circ, and the year is recorded in year.
In a few of the years, the circulation is not known, so we want to fill it in by linear interpolation.
. use [Link] clear
. by magazine: ipolate circ year, gen(icirc)
When the by prefix is specified, interpolation is performed separately for each group.
Methods and formulas
The value y at x is found by finding the closest points (x0 , y0 ) and (x1 , y1 ), such that x0 < x
and x1 > x where y0 and y1 are observed, and calculating
y1 − y0
y= (x − x0 ) + y0
x1 − x0
If epolate is specified and if (x0 , y0 ) and (x1 , y1 ) cannot be found on both sides of x, the two
closest points on the same side of x are found, and the same formula is applied.
If there are multiple observations with the same value for x0 , then y0 is taken as the average of
the corresponding y values for those observations. (x1 , y1 ) is handled in the same way.
Reference
Meijering, E. 2002. A chronology of interpolation: From ancient astronomy to modern signal and image processing.
Proceedings of the IEEE 90: 319–342.
Also see
[MI] mi impute — Impute missing values