Stata连享会 主页 || 视频 || 推文 || 知乎
温馨提示: 定期 清理浏览器缓存,可以获得最佳浏览体验。
New!
lianxh
命令发布了:
随时搜索连享会推文、Stata 资源,安装命令如下:
. ssc install lianxh
使用详情参见帮助文件 (有惊喜):
. help lianxh
Source: http://www.statalist.org/forums/forum/general-stata-discussion/general/17996-substitute-rows-with-average-of-row-above-and-below
我有一份面板数据,有些年份上的数据有两行或多行记录 (例如,本例中 2007 年的数据)。棘手的是,这两行数据存在差异,且无法判断哪一个记录是正确的。此时,比较稳妥的选择是:将这两汉数据都舍弃,使用相邻年度 (2006 和 2008 年) 的均值作为 2007 年的观察值 (插值)。
clear
input ID year var1 var2 var3
1 2006 34 45 65
1 2007 45 43 41
1 2007 3 56 59
1 2008 39 54 76
1 2009 41 57 68
end
save "data00.dta", replace
原始数据详情:
. list
+--------------------------------+
| ID year var1 var2 var3 |
|--------------------------------|
1. | 1 2006 34 45 65 |
2. | 1 2007 45 43 41 |
3. | 1 2007 3 56 59 |
4. | 1 2008 39 54 76 |
5. | 1 2009 41 57 68 |
+--------------------------------+
思路:
tsfill
填充年份,让数据变成平行面板;tssmooth ma
命令插值 (用前后两年的平均值代替 2007 年的缺失值)。命令如下:
. use "data00.dta", clear
*-删除重复值 (数据有误)
. drop if year == 2007
. tsset ID year
. tsfill
. list
+--------------------------------+
| ID year var1 var2 var3 |
|--------------------------------|
1. | 1 2006 34 45 65 |
2. | 1 2007 . . . |
3. | 1 2008 39 54 76 |
4. | 1 2009 41 57 68 |
+--------------------------------+
*-填充缺失值:
forv i = 1/3 {
tssmooth ma v`i' = var`i' , w(1, 0, 1)
replace var`i' = v`i' if var`i' == .
}
drop v?
list
运行结果如下:
. forv i = 1/3 {
2. tssmooth ma v`i' = var`i' , w(1, 0, 1)
3. replace var`i' = v`i' if var`i' == .
4. }
The smoother applied was
(1/2)*[x(t-1) + 0*x(t) + x(t+1)]; x(t)= var1
(1 real change made)
The smoother applied was
(1/2)*[x(t-1) + 0*x(t) + x(t+1)]; x(t)= var2
(1 real change made)
The smoother applied was
(1/2)*[x(t-1) + 0*x(t) + x(t+1)]; x(t)= var3
(1 real change made)
. drop v?
. list
+--------------------------------+
| ID year var1 var2 var3 |
|--------------------------------|
1. | 1 2006 34 45 65 |
2. | 1 2007 36.5 49.5 70.5 |
3. | 1 2008 39 54 76 |
4. | 1 2009 41 57 68 |
+--------------------------------+
从结果的提示信息可以看出 w(1, 0, 1) 选项的含义:
The smoother applied was
(1/2)*[x(t-1) + 0*x(t) + x(t+1)]; x(t)= var1
思路:
duplicates tag
命令标记重复的观察值,并使用 gen()
选项产生新变量用以记录标记情况;_n
标记观察先后顺序 (x[_n] 表示 x t ; x_{t}; xt;x[_n-1] 表示 x t − 1 x_{t-1} xt−1;x[_n+1] 表示 x t + 1 x_{t+1} xt+1),进行插值;clear
input id year var1 var2 var3
1 2006 34 45 65
1 2007 45 43 41
1 2007 3 56 59
1 2008 39 54 76
1 2009 41 57 68
end
使用 duplicates tag
标记重复的观察值:
. duplicates tag id year, gen(mistake)
. list
. bysort id year: keep if (_n == 1)
foreach v of varlist var1 var2 var3 {
replace `v' = (`v'[_n-1] + `v'[_n+1])/2 if mistake
}
结果如下:
. duplicates tag id year, gen(mistake)
Duplicates in terms of id year
. list // A-1
+------------------------------------------+
| id year var1 var2 var3 mistake |
|------------------------------------------|
1. | 1 2006 34 45 65 0 |
2. | 1 2007 45 43 41 1 |
3. | 1 2007 3 56 59 1 |
4. | 1 2008 39 54 76 0 |
5. | 1 2009 41 57 68 0 |
+------------------------------------------+
. bysort id year: keep if (_n == 1)
(1 observation deleted)
. list // A-2
+------------------------------------------+
| id year var1 var2 var3 mistake |
|------------------------------------------|
1. | 1 2006 34 45 65 0 |
2. | 1 2007 45 43 41 1 |
3. | 1 2008 39 54 76 0 |
4. | 1 2009 41 57 68 0 |
+------------------------------------------+
. foreach v of varlist var1 var2 var3 {
2. replace `v' = (`v'[_n-1] + `v'[_n+1])/2 if mistake
3. }
. list // A-3
+------------------------------------------+
| id year var1 var2 var3 mistake |
|------------------------------------------|
1. | 1 2006 34 45 65 0 |
2. | 1 2007 36.5 49.5 70.5 1 |
3. | 1 2008 39 54 76 0 |
4. | 1 2009 41 57 68 0 |
+------------------------------------------+
*------------
*-Solution 1:
* You could use tssmooth ma.
* For example:
clear
input ID year var1 var2 var3
1 2006 34 45 65
1 2007 45 43 41
1 2007 3 56 59
1 2008 39 54 76
1 2009 41 57 68
end
list
drop if year == 2007
tsset ID year
tsfill
list
forv i = 1/3 {
tssmooth ma v`i' = var`i' , w(1, 0, 1)
replace var`i' = v`i' if var`i' == .
}
drop v?
list
*------------
*-Solution 2:
clear
input id year var1 var2 var3
1 2006 34 45 65
1 2007 45 43 41
1 2007 3 56 59
1 2008 39 54 76
1 2009 41 57 68
end
duplicates tag id year, gen(mistake)
list
bysort id year: keep if (_n == 1)
list
foreach v of varlist var1 var2 var3 {
replace `v' = (`v'[_n-1] + `v'[_n+1])/2 if mistake
}
list
完整阅读:
Stata数据处理:面板数据的填充和补漏