期末缺失值的线性插值

Linear Interpolation on missing values at the end of the period

这是一个数据集示例:

data data;
   input group $ date value;
   datalines;
A 2001 1.5
A 2002 2.6
A 2003 2.8
A 2004 2.9
A 2005 .
B 2001 0.1
B 2002 0.6
B 2003 0.7
B 2004 1.4
B 2005 .
C 2001 4.7
C 2002 4.6
C 2003 4.8
C 2004 5.0
C 2005 .
;
run;

我想使用线性插值替换每个组的变量 "value" 的缺失值。

我试过使用 proc expand :

proc expand data=data method = join out=want;
    by group;
    id date;
    convert value;
run;

但它不会替换输出数据库中的任何值。

知道我做错了什么吗?

我对expand程序了解不多,不过你可以在proc expand语句中加上extrapolate

proc expand data=data method = join out=want extrapolate;
    by group;
    id date;
    convert value;
run;

结果:

  Obs    group    date    value

    1      A      2001     1.5
    2      A      2002     2.6
    3      A      2003     2.8
    4      A      2004     2.9
    5      A      2005     3.0
    6      B      2001     0.1
    7      B      2002     0.6
    8      B      2003     0.7
    9      B      2004     1.4
   10      B      2005     2.1
   11      C      2001     4.7
   12      C      2002     4.6
   13      C      2003     4.8
   14      C      2004     5.0
   15      C      2005     5.2

请注意声明here

By default, PROC EXPAND avoids extrapolating values beyond the first or last input value for a series and only interpolates values within the range of the nonmissing input values. Note that the extrapolated values are often not very accurate and for the SPLINE method the EXTRAPOLATE option results may be very unreasonable. The EXTRAPOLATE option is rarely used."

这里有三种方法。您丢失的数据在系列的末尾。您实际上是在用几个点进行预测。 proc expand 不适合这样做,但为了填充缺失值,这些是一些可用的选项。

1.过程扩展

你很接近!您丢失的数据位于系列的末尾,这意味着它之间没有可连接的值。在这种情况下,您需要使用 extrapolate 选项。如果两个数据点之间存在缺失值,则无需使用 extrapolate.

proc expand data=data method = join 
            out=want
            extrapolate;
    by group;
    id date;
    convert value;
run;

2。 PROC ESM

您可以使用指数平滑模型进行插值。我喜欢这种方法,因为它可以解释季节性、趋势等因素。

/* Convert Date to SAS date */
data to_sas_date;
    set data;
    year = mdy(1,1,date);

    format year year4.;
run;

proc esm data=to_sas_date
         out=want
         lead=0;
    by group;
    id year interval=year;
    forecast value / replacemissing;
run;

3。 PROC 时间序列

这将使用 mean/median/first/last/etc 填充值。在一个时间范围内。首先将年份转换为 SAS 日期,如上所示。

proc timeseries data=to_sas_date
                out=want;
    by group;
    id year interval=year;
    var value / setmissing=average;
run;