如何使用循环插值面板数据中的值

时间:2019-11-10 10:05:40

标签: for-loop stata

我有一个面板数据集。我的变量标识符是cc代表国家/地区代码,Year代表年份:

clear
input long cc float(sch Year)
2         0 1960
2         0 1961
2         0 1962
2         0 1963
2         0 1964
2         0 1965
2         0 1966
2         0 1967
2         0 1968
2         0 1969
2         0 1970
2         0 1971
2         0 1972
2         0 1973
2         0 1974
2         0 1975
2         0 1976
2         0 1977
2   .733902 1978
2     .7566 1979
2       .78 1980
2      .875 1981
2     .9225 1982
2 1.0174999 1983
2 1.0649999 1984
2      1.16 1985
2    1.2425 1986
2   1.28375 1987
2   1.36625 1988
2    1.4075 1989
2      1.49 1990
2    1.5825 1991
2   1.62875 1992
2   1.72125 1993
2    1.7675 1994
2      1.86 1995
2     1.935 1996
2    1.9725 1997
2 2.0475001 1998
2     2.085 1999
2      2.16 2000
2      2.27 2001
2     2.325 2002
2     2.435 2003
2      2.49 2004
2       2.6 2005
2    2.7575 2006
2   2.83625 2007
2   2.99375 2008
2    3.0725 2009
2      3.23 2010
2   3.15125 2011
2  3.190625 2012
2 3.1709375 2013
2 3.1807814 2014
2 3.1758595 2015
2 3.1783204 2016
2   3.17709 2017
2  3.177705 2018
4         0 1960
4         0 1961
4         0 1962
4         0 1963
4         0 1964
4         0 1965
4         0 1966
4         0 1967
4         0 1968
4         0 1969
4         0 1970
4         0 1971
4         0 1972
4         0 1973
4         0 1974
4         0 1975
4         0 1976
4         0 1977
4  4.657455 1978
4    4.8015 1979
4      4.95 1980
4       5.4 1981
4     5.625 1982
4     6.075 1983
4       6.3 1984
4      6.75 1985
4      7.02 1986
4     7.155 1987
4     7.425 1988
4      7.56 1989
4      7.83 1990
4    7.8275 1991
4   7.82625 1992
4   7.82375 1993
4    7.8225 1994
4      7.82 1995
4     8.195 1996
4    8.3825 1997
4    8.7575 1998
4     8.945 1999
4      9.32 2000
4  9.412499 2001
4   9.45875 2002
4   9.55125 2003
4    9.5975 2004
4      9.69 2005
4      9.73 2006
4      9.75 2007
4      9.79 2008
4      9.81 2009
4      9.85 2010
4      9.83 2011
4      9.84 2012
4     9.835 2013
4    9.8375 2014
4   9.83625 2015
4  9.836875 2016
4  9.836563 2017
4   9.83672 2018
end

我想插值sch变量以减少年份。变量sch1979-2018年的观测。通过使用对1978的观察,我想对1977的值进行插值:

sch_1977 = 0.97 * sch_1978

我尝试过的代码如下:

forvalues y = 1977 1976 1975{
local i = `y' - 1958
bysort cc (Year): generate sch`y' = 0.97*sch[`i'] 
replace sch`y' = 0 if Year != `y'
replace sch = sch + sch`y'
}

此处i对应于变量1978放置cc的年份的行。通过使用forvalues循环,在每个迭代中,我想创建一个新变量(sch1977sch1978sch1979),并在相应的年份中进行插值观测,并为零所有其他观察。接下来,我想用sch总结这个新变量。但是,Stata抱怨该代码无效。

1 个答案:

答案 0 :(得分:2)

以下对我有用:

foreach x in 1977 1976 1975 {
    local i = (2018 - 1960) - (2018 - `x') + 2
    bysort cc (Year): generate sch_`x' = 0.97 * sch[`i'] 
    replace sch_`x' = 0 if Year != `x'
    replace sch = sch + sch_`x'
}

结果:

bysort cc (Year): list if inrange(Year, 1970, 1980), sepby(cc)

-> cc = 2

     +-------------------------------------------------------+
     | cc        sch   Year   sch_1977   sch_1976   sch_1975 |
     |-------------------------------------------------------|
 11. |  2          0   1970          0          0          0 |
 12. |  2          0   1971          0          0          0 |
 13. |  2          0   1972          0          0          0 |
 14. |  2          0   1973          0          0          0 |
 15. |  2          0   1974          0          0          0 |
 16. |  2   .6698126   1975          0          0   .6698126 |
 17. |  2   .6905284   1976          0   .6905284          0 |
 18. |  2   .7118849   1977   .7118849          0          0 |
 19. |  2    .733902   1978          0          0          0 |
 20. |  2      .7566   1979          0          0          0 |
 21. |  2        .78   1980          0          0          0 |
     +-------------------------------------------------------+

-> cc = 4

     +-------------------------------------------------------+
     | cc        sch   Year   sch_1977   sch_1976   sch_1975 |
     |-------------------------------------------------------|
 11. |  4          0   1970          0          0          0 |
 12. |  4          0   1971          0          0          0 |
 13. |  4          0   1972          0          0          0 |
 14. |  4          0   1973          0          0          0 |
 15. |  4          0   1974          0          0          0 |
 16. |  4   4.250733   1975          0          0   4.250733 |
 17. |  4   4.382199   1976          0   4.382199          0 |
 18. |  4   4.517731   1977   4.517731          0          0 |
 19. |  4   4.657455   1978          0          0          0 |
 20. |  4     4.8015   1979          0          0          0 |
 21. |  4       4.95   1980          0          0          0 |
     +-------------------------------------------------------+