計量經濟學

使用 Stata 或 Excel 平均超過 5 年的數據

  • October 25, 2016

我需要 Stata 命令或 Excel 函式來計算面板數據集中 5 年的平均值組。特別是,此過程考慮到可能存在的缺失值(Excel 中的空單元格),從而根據該期間的實際非缺失數調整計算。

這是你想要的(在Stata中)嗎?

clear all
* Generate Data (n=5, T=20)
set obs 100
gen id = floor((_n-1)/20)+1
by id, sort: gen year = 1990+_n
gen x = rnormal()
xtset id year
* Convert
gen t5 = floor((year-1991)/5)+1
gen idt5 = id*10+t5 /* 10 can be 100, 1000, etc., depending on T */
by idt5, sort: egen xbar5 = mean(x)
*drop t5 idt5  /* drop if you want; you can also use tempvar */

如果不是,請使用範例澄清您的問題。

使用tssmooth ma命令。使用該window()選項控制要包含在平均值中的時間段。

. use http://www.stata-press.com/data/r14/abdata, clear
. keep id year wage
. quietly keep if id == 1 | id == 2
. quietly xtset id year
. 
. ** moving average of 2 years before, 2 years after, and the current year
. tssmooth ma wage_moving_avg_5yr = wage, window(2 1 2)
The smoother applied was
    by id : (1/5)*[x(t-2) + x(t-1) + 1*x(t) + x(t+1) + x(t+2)]; x(t)= wage

. list

    +--------------------------------+
    | year      wage   id   wage_m~r |
    |--------------------------------|
 1. | 1977   13.1516    1    12.7643 |
 2. | 1978   12.3018    1    13.0242 |
 3. | 1979   12.8395    1    13.2773 |
 4. | 1980   13.8039    1    13.6206 |
 5. | 1981   14.2897    1   13.91592 |
    |--------------------------------|
 6. | 1982   14.8681    1   14.18503 |
 7. | 1983   13.7784    1   14.31207 |
 8. | 1977   14.7909    2   14.61597 |
 9. | 1978   14.1036    2   14.83472 |
10. | 1979   14.9534    2   15.10716 |
    |--------------------------------|
11. | 1980    15.491    2   15.37526 |
12. | 1981   16.1969    2   15.81556 |
13. | 1982   16.1314    2    16.0311 |
14. | 1983   16.3051    2   16.21113 |
    +--------------------------------+

請注意,此命令採用指定時間段內可用的內容。例如,對於 1977 年,由於前兩年不可用,它只是計算當年和未來兩年的平均值。

引用自:https://economics.stackexchange.com/questions/13828