替换具有初始值的变量

时间:2019-03-18 08:33:32

标签: loops replace stata panel-data

我有一个面板数据集(标识符是ccyear的变量)。

我拥有Y变量的所有值,但只有X_{1996}变量的初始(X)值。

我想用以下模式替换X的缺失值:

X_{1997} = Y_{1996} + 0.95*X_{1996} 
X_{1998} = Y_{1997} + 0.95*X_{1997}
.
.
.
X_{2015} = Y_{2014} + 0.95*X_{2014}

我想从X开始迭代地替换1997,即使用最后生成的值。

是否可以为此目的编写循环?

示例数据:

clear 
input str3 cc   year     X         Y
"ABW" 1996 467.5953  28.46004
"ABW" 1997        .  30.23142
"ABW" 1998        .  31.70577
"ABW" 1999        . 30.870903
"ABW" 2000        . 25.495903
"ABW" 2001        .  23.71254
"ABW" 2002        . 26.906475
"ABW" 2003        . 29.215036
"ABW" 2004        .  28.77914
"ABW" 2005        . 33.581017
"ABW" 2006        .  35.16129
"ABW" 2007        .  32.51442
"ABW" 2008        . 33.251934
"ABW" 2009        .  30.29287
"ABW" 2010        . 28.931993
"ABW" 2011        .   28.5057
"ABW" 2012        . 26.647564
"ABW" 2013        . 23.999134
"ABW" 2014        . 22.917984
"ABW" 2015        . 21.481943
"AFG" 1996  362.353 11.51442
"AFG" 1997        . 11.557285
"AFG" 1998        . 12.19376
"AFG" 1999        . 14.557285
"AFG" 2000        . 12.906254
"AFG" 2001        . 13.156855
"AFG" 2002        . 11.557285
"AFG" 2003        . 15.758896
"AFG" 2004        . 19.005346
"AFG" 2005        . 23.193766
"AFG" 2006        . 26.407976
"AFG" 2007        . 20.068926
"AFG" 2008        . 19.021326
"AFG" 2009        . 17.991436
"AFG" 2010        . 17.959072
"AFG" 2011        . 16.674591
"AFG" 2012        . 16.906254
"AFG" 2013        . 17.343513
"AFG" 2014        . 18.156855
"AFG" 2015        . 19.454687

1 个答案:

答案 0 :(得分:3)

如果要使用最后生成的观察值来计算X的缺失值,则可以按照以下步骤进行:

bysort cc (year): replace X = Y[_n-1] + 0.95 * X[_n-1] if _n > 1

list, sepby(cc) 

     +----------------------------------+
     |  cc   year          X          Y |
     |----------------------------------|
  1. | ABW   1996   467.5953   28.46004 |
  2. | ABW   1997   472.6756   30.23142 |
  3. | ABW   1998   479.2732   31.70577 |
  4. | ABW   1999   487.0153    30.8709 |
  5. | ABW   2000   493.5355    25.4959 |
  6. | ABW   2001   494.3546   23.71254 |
  7. | ABW   2002   493.3494   26.90648 |
  8. | ABW   2003   495.5884   29.21504 |
  9. | ABW   2004    500.024   28.77914 |
 10. | ABW   2005    503.802   33.58102 |
 11. | ABW   2006   512.1929   35.16129 |
 12. | ABW   2007   521.7445   32.51442 |
 13. | ABW   2008   528.1717   33.25193 |
 14. | ABW   2009    535.015   30.29287 |
 15. | ABW   2010   538.5571   28.93199 |
 16. | ABW   2011   540.5613    28.5057 |
 17. | ABW   2012   542.0389   26.64756 |
 18. | ABW   2013   541.5845   23.99913 |
 19. | ABW   2014   538.5045   22.91798 |
 20. | ABW   2015   534.4972   21.48194 |
     |----------------------------------|
 21. | AFG   1996    362.353   11.51442 |
 22. | AFG   1997   355.7498   11.55729 |
 23. | AFG   1998   349.5196   12.19376 |
 24. | AFG   1999   344.2373   14.55729 |
 25. | AFG   2000   341.5828   12.90625 |
 26. | AFG   2001   337.4099   13.15685 |
 27. | AFG   2002   333.6962   11.55729 |
 28. | AFG   2003   328.5687    15.7589 |
 29. | AFG   2004   327.8992   19.00535 |
 30. | AFG   2005   330.5096   23.19377 |
 31. | AFG   2006   337.1778   26.40798 |
 32. | AFG   2007   346.7269   20.06893 |
 33. | AFG   2008   349.4595   19.02133 |
 34. | AFG   2009   351.0078   17.99144 |
 35. | AFG   2010   351.4489   17.95907 |
 36. | AFG   2011   351.8355   16.67459 |
 37. | AFG   2012   350.9183   16.90625 |
 38. | AFG   2013   350.2787   17.34351 |
 39. | AFG   2014   350.1082   18.15685 |
 40. | AFG   2015   350.7597   19.45469 |
     +----------------------------------+