0

我在 Matlab 中有一个不平衡的面板数据集,我需要滞后。不平衡的面板数据集很容易在 R 中与plm包一起使用。Matlab中是否有类似的功能?这是一个玩具示例:

A = table(sort([repmat([1;2],4,1);repmat(3,3,1)]),[repmat((1994:1997).',2,1);(1995:1997).'],normrnd(100,1,11,1))

A = 

    Var1    Var2     Var3 
    ____    ____    ______

    1       1994    98.423
    1       1995    100.51
    1       1996    100.28
    1       1997    100.03
    2       1994    98.666
    2       1995    101.13
    2       1996    100.35
    2       1997    99.701
    3       1995    100.02
    3       1996    99.738
    3       1997     98.25

对于 Var1 的每个实例,我想将 Var3 的值滞后一年。请注意,对于 Var1=3,1994 年没有值。因此,我想要以下内容:

    Var1    Var2     Var3      Var4 
    ____    ____    ______    ______

    1       1994    98.423       NaN
    1       1995    100.51    98.423
    1       1996    100.28    100.51
    1       1997    100.03    100.28
    2       1994    98.666       NaN
    2       1995    101.13    98.666
    2       1996    100.35    101.13
    2       1997    99.701    100.35
    3       1995    100.02       NaN
    3       1996    99.738    100.02
    3       1997     98.25    99.738

在 Matlab 中是否有一种简单的方法可以做到这一点?

4

1 回答 1

0

我自己找到了一个解决方案varfun

A = table(sort([repmat([1;2],4,1);repmat(3,3,1)]),[repmat((1994:1997).',2,1);(1995:1997).'],normrnd(100,1,11,1))

B = varfun(@(x)lagmatrix(x,1),A,'GroupingVariables','Var1')
于 2014-12-19T12:57:30.380 回答