cd D:\stata11\ado\personal\PX_Panel\xA6_Panel_Data
cd `c(sysdir_personal)'PX_Panel\xA6_Panel_Data
xA6_Panel_Data - Printed on 2011-11-25 10:43:02
1
2
3 * ========================
4 *
5 * 面板数目模型
6 *
7 * ========================
8
9
10 *
11 *------------------------------------------------
12 *
13 * 主讲人:连玉君 副教授
14 *
15 *
16 * 单 位:中山大学岭南学院金融系
17 * 电 邮: arlionn@163.com
18 * 博 客: http://blog.cnfol.com/arlion
19 * 主 页:http://toran.cn/arlion
20 *
21 *------------------------------------------------
22
23
24
25
26 *-注意:执行后续命令之前,请先执行如下命令,进入课程讲义所在目录
27
28
29
30
31 *-或(如果你的stata11放置于其他盘符下,如C盘或F盘)
32
33
34
35
36 * ------------------
37 * ---- 本讲目录 ----
38 * ------------------
39 *
40 * 6.1 静态面板模型:固定效应模型 v.s. 随机效应模型
41 * 6.2 时间效应、模型的筛选和常见问题
42 * 6.3 异方差、序列相关和截面相关
43 * 6.4 内生性问题与 IV-GMM 估计
44 * 6.5 动态面板模型
45 * 6.6 面板数据资料的处理
46
47
48
49 *-----------------
50 *-6.1 静态面板模型
51 *-----------------
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 *--------------------
74 *-6.1.1 简介
* ==本节目录==
* 6.1.1 简介
* 6.1.2 参考资料
* 6.1.3 静态面板简介
* 6.1.4 固定效应模型
* 6.1.4.1 FE模型的基本原理
* 6.1.4.2 如何估计固定效应模型
* 6.1.4.3 stata的估计方法解析
* 6.1.4.4 解读 xtreg,fe 的估计结果
* R^2
* 个体效应是否显著?
* 如何得到调整后的 adj-R2 ?
* 如何得到每家公司的截距项?
* 拟合值和残差
* 6.1.5 随机效应模型
* 6.1.5.1 RE 与 FE 的异同
* 6.1.5.2 解读 xtreg,re 的估计结果
Page 1
use xtlabor.dta, clear
browse
tsset id year
xtdes
use invest2.dta, clear
browse
tsset id t
xtdes
75
76
77 *--------------------
78 *-6.1.2 参考资料
79
80 * 连玉君计量笔记第8章,重点介绍stata操作,可在我的博客中下载
81 * Greene(2000), chp14 较为清晰的介绍了静态面板模型
82 * Baltagi(2001), 一本专门介绍Panel的书,人大论坛可下载
83 * Arrllano(2003), 重点关注动态面板和GMM估计,人大论坛可下载
84 * Stata Longitudinal/Panel data Reference Manual(9)
85 * 详细介绍了stata中有关Panel的各种命令
86 * 以上书籍我这里都有原版教材,可为大家提供复印版本
87
88 shellout 连玉君_Chp8_Panel_Data.pdf
89
90
91 *--------------------
92 *-6.1.3 静态面板简介
93
94 * 面板数据的结构(兼具截面资料和时间序列资料的特征)
95
96
97
98
99
100
101
102
103
104
105 *--------------------
106 *-6.1.4 固定效应模型
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144 *-6.1.4.2 如何估计固定效应模型
145
146
147
148
* 实质上就是在传统的线性回归模型中加入 N-1 个虚拟变量,
* 使得每个截面都有自己的截距项,
* 截距项的不同反映了个体的某些不随时间改变的特征
*
* 例如: lny = a_i + b1*lnK + b2*lnL + e_it
* 考虑中国28个省份的C-D生产函数
* OLS 估计的偏误
* 一份模拟数据
do B7_introFe.do
save B7_introFe, replace
tsset id t
xtdes
twoway (scatter y x) (lfit y x)
reg y x
twoway (scatter y x) (lfit y x)
(lfit y x if id==1)
(lfit y x if id==2)
(lfit y x if id==3),
legend(off)
tab id, gen(dum)
list
reg y x dum1 dum2 dum3, nocons
est store m_ols
* 回归分析
reg y x
tsset id t
xtreg y x, fe
est table m_ols m_fe, b(%6.3f) star(0.1 0.05 0.01)
est store m_fe
* 真实的数据生成过程
doedit B7_introFe.do
///
///
///
///
xA6_Panel_Data - Printed on 2011-11-25 10:43:02
*-M1:放入三个虚拟变量,即每家公司都有一个自己的截距项
use B7_introFe, clear
tab id , gen(dum)
Page 2
*-M2:放入两个虚拟变量,三家公司有一个公共的截距项
*-M3:面板固定效应模型(stata的估计方法)
tsset id t
xtreg y x, fe
est store m_fe
est table m_*, b(%6.3f) star(0.1 0.05 0.01)
* 目的:如果截面的个数非常多,那么采用虚拟变量的方式运算量过大
* 因此,要寻求合理的方式去除掉个体效应
* 因为,我们关注的是 x 的系数,而非每个截面的截距项
* 处理方法:
*
* y_it = u_i + x_it*b + e_it (1)
* ym_i = u_i + xm_i*b + em_i (2) 组内平均
* ym = um + xm*b + em (3) 样本平均
* (1) - (2), 可得:
* (y_it - ym_i) = (x_it - xm_i)*b + (e_it - em_i) (4)//within估计
* (4)+(3), 可得:
* (y_it-ym_i+ym) = um + (x_it-xm_i+xm)*b + (e_it-em_i+em)
* 可重新表示为:
* Y_it = a_0 + X_it*b + E_it
* 对该模型执行 OLS 估计,即可得到 b 的无偏估计量
/*样本平均*/
egen y_meanw = mean(y), by(id) /*公司内部平均*/
egen y_mean = mean(y)
egen x_meanw = mean(x), by(id)
egen x_mean = mean(x)
gen dy = y - y_meanw + y_mean
gen dx = x - x_meanw + x_mean
reg dy dx
est store m_stata
est table m_*, b(%6.3f) star(0.1 0.05 0.01)
xA6_Panel_Data - Printed on 2011-11-25 10:43:02
reg y x dum2 dum3
est store m_pooldum2
reg y x dum1 dum2 dum3, nocons
est store m_pooldum3
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163 *-6.1.4.3 stata的估计方法解析
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193 *-6.1.4.4 解读 xtreg,fe 的估计结果
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
Page 3
use invest2.dta, clear
tsset id t
edit
xtreg market invest stock, fe
*-- R^2
* y_it = a_0 + x_it*b_o + e_it (1) pooled OLS
* y_it = u_i + x_it*b_w + e_it (2) within estimator
* ym_i = a_0 + xm_i*b_b + em_i (3) between estimator
*
* -> R-sq: within 模型(2)对应的R2,是一个真正意义上的R2
* -> R-sq: between corr{xm_i*b_w,ym_i}^2
* -> R-sq: overall corr{x_it*b_w,y_it}^2
*-- F(2,93) = 33.23 检验除常数项外其他解释变量的联合显著性
* 93 = 100-2-5
*-- corr(u_i, Xb) = 0.5256
*-- sigma_u, sigma_e, rho
* rho = sigma_u^2 / (sigma_u^2 + sigma_e^2)
dis e(sigma_u)^2 / (e(sigma_u)^2 + e(sigma_e)^2)
dis 1023.5914^2 / (1023.5914^2 + 370.9569^2)
*-- 个体效应是否显著?(假设检验)
* F(4, 93) = 97.68 H0: a1 = a2 = a3 = a4 = 0
* Prob > F = 0.0000 表明,固定效应高度显著
*-- 如何得到每家公司的截距项?
* 方法二:xi 前缀
xi: reg market invest stock i.id
xi: reg market invest stock i.id, robust
* 方法三:采用predict命令
* 方法四:areg 命令
areg market invest stock, absorb(id)
predict ai, d
gen a_i = ai + _b[_cons]
order id t a_i ai
browse
*-- 如何得到调整后的 R2,即 adj-R2 ?
use invest2.dta, clear
qui tab id, gen(dum)
cap drop dum1
reg market invest stock dum*
areg market invest stock, a(id)
* 方法一:加入 (N-1) 个虚拟变量,采用 OLS 估计
use invest2.dta, clear
tab id, gen(dum)
reg market invest stock dum*, nocons
xtreg market invest stock , fe
* 优点:可以同时获得每个截距项的标准误、t值和p值
* 适用于 大 T 小 N 型数据
qui xtreg market invest stock, fe
predict a , u
replace a = _b[_cons] + a
duplicates example id a
order id t a
browse
reg market invest stock dum*, nocons nohead
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274 xb xb, fitted values; the default
275 stdp calculate standard error of the fitted values
276 ue u_i + e_it, the combined residual
277 xbu xb + u_i, prediction including effect
278 u u_i, the fixed- or random-error component
279 e e_it, the overall error component */
280
281
282
283
284
285
286
287
288
289
290 *--------------------
291 *-6.1.5 随机效应模型
292
293 *-6.1.5.1 RE 与 FE 的异同
294
295
296
xtreg market invest stock, fe
predict y_hat
predict a
, u
predict res , e
predict cres, ue
gen ares = a + res
list ares cres in 1/10
* 采用areg估计模型,将截距项的标准误存于一个新的变量中
* ssc install fese, replace
use invest2.dta, clear
fese market invest stock, s(o) oonly
* y_it = u_i + x_it*b + e_it
* predict newvar, [option]
/*
* 方法五:fese 命令
*-- 拟合值和残差
xA6_Panel_Data - Printed on 2011-11-25 10:43:02
/*更为简洁*/
*-RE的模型设定:
* y_it = x_it*b + (a_i + u_it)
Page 4
* = x_it*b + v_it
* 基本思想:将随机干扰项分成两种
* 一种是不随时间改变的,即个体效应 a_i
* 另一种是随时间改变的,即通常意义上的干扰项 u_it
* 估计方法:FGLS
* Var(v_it) = sigma_a^2 + sigma_u^2
* Cov(v_it,v_is) = sigma_a^2
* Cov(v_it,v_js) = 0
* 利用Pooled OLS,Within Estimator, Between Estimator
* 可以估计出sigma_a^2和sigma_u^2,进而采用GLS或FGLS
* Re估计量是Fe估计量和Be估计量的加权平均
* yr_it = y_it - theta*ym_i
* xr_it = x_it - theta*xm_i
* theta = 1 - sigma_u / sqrt[(T*sigma_a^2 + sigma_u^2)]
use invest2.dta, clear
xtreg market invest stock, re
*-- R2
* -> R-sq: within corr{(x_it-xm_i)*b_r, y_it-ym_i}^2
* -> R-sq: between corr{xm_i*b_r,ym_i}^2
* -> R-sq: overall corr{x_it*b_r,y_it}^2
* 上述R2都不是真正意义上的R2,因为Re模型采用的是GLS估计。
*-- rho = sigma_u^2 / (sigma_u^2 + sigma_e^2)
dis e(sigma_u)^2 / (e(sigma_u)^2 + e(sigma_e)^2)
*-- corr(u_i, X) = 0 (assumed)
* 这是随机效应模型的一个最重要,也限制该模型应用的一个重要假设
* 然而,采用固定效应模型,我们可以粗略估计出corr(u_i, X)
*-- Wald chi2(2) = 95.98 Prob> chi2 = 0.0000
xA6_Panel_Data - Printed on 2011-11-25 10:43:02
xtreg market invest stock, fe
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315 *-6.1.5.2 解读 xtreg,re 的估计结果
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342 *------------------------------------
343 *-6.2 时间效应、模型的筛选和常见问题
344 *------------------------------------
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370 *-----------------
Page 5
* ==本节目录==
* 6.2.1 时间效应
* 6.2.1.1 时间虚拟变量的设定
* 6.2.1.2 检验时间效应是否显著
* 6.2.2 模型的筛选
* 6.2.2.1 固定效应模型还是Pooled OLS?
* 6.2.2.2 随机效应模型还是Pooled OLS?
* 6.2.2.3 固定效应模型还是随机效应模型?Hausman检验
* 6.2.3 一些常见问题
* 6.2.3.1 为何tsset命令总是报告错误信息?
* 6.2.3.2 数据的结构
* 6.2.3.3 为何有些变量会被drop掉?
* 6.2.3.4 unbalance —> balance
* 6.2.3.5 得到时间连续的样本
* 6.2.3.6 得到连续的公司编号
* 6.2.3.7 长条形数据与扁平型数据的转换
* 6.2.3.8 绘图
* 6.2.3.9 统计类别变量
* 6.2.4 面板数据的转换
* 6.2.4.1 FE 转换
* 6.2.4.2 RE 转换
use xtcs, clear
qui tab year, gen(yr)
drop yr1
xtreg tl size ndts tang tobin npr yr*, fe
* 随机效应模型中的时间效应
xtreg tl size ndts tang tobin npr yr*, re
* 单向固定效应模型
* y_it = u_i + x_it*b + e_it
* 双向固定效应模型
* y_it = u_i + f_t + x_it*b + e_it
* 固定效应模型中的时间效应
* Wald 检验
xtreg tl size ndts tang tobin npr yr*, fe
test yr2 = yr3 = yr4 = yr5 = yr6 = yr7
test yr2 = yr3 = yr4 = yr5 = yr6 = yr7 = 0
* LR 检验
xtreg tl size ndts tang tobin npr
, fe
est store fe
xtreg tl size ndts tang tobin npr yr*, fe
est store fe_dumt
lrtest fe fe_dumt
371 *-6.2.1 时间效应
372
373 *-6.2.1.1 时间虚拟变量的设定
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390 *-6.2.1.2 检验时间效应是否显著
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405 *-----------------
406 *-6.2.2 模型的筛选
407
408 *-6.2.2.1 固定效应模型还是Pooled OLS?
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423 *-6.2.2.2 随机效应模型还是Pooled OLS?
424
425
426
427
428
429
430
431
432
433
434
435 *-6.2.2.3 固定效应模型还是随机效应模型?Hausman检验
436
437
438
439
440
441
442
443
444
* Wald 检验
use invest2.dta, clear
xtreg market invest stock, fe
* LR检验
qui tab id, gen(dum)
reg market invest stock
est store m_ols
reg market invest stock dum*, nocons
est store m_fe
lrtest m_ols m_fe
* 方法二:LR test(似然比检验)
xtreg market invest stock, re mle
* H0: Var(u) = 0
* 方法一:B-P 检验
xtreg market invest stock, re
xttest0
xA6_Panel_Data - Printed on 2011-11-25 10:43:03
* y_it = u_i + x_it*b + e_it
*--- Hausman 检验 ---
* 基本思想:如果 Corr(u_i,x_it) = 0, Fe 和 Re 都是一致的,但Re更有效
* 如果 Corr(u_i,x_it)!= 0, Fe 仍然有效,但Re是有偏的
* 基本步骤
Page 6
* 通常是因为RE模型的基本假设 Corr(x,u_i)=0 无法得到满足
* 检验过程中两个模型的方差-协方差矩阵都采用Fe模型的
hausman m_fe m_re, sigmaless
* 两个模型的方差-协方差矩阵都采用Re模型的
hausman m_fe m_re, sigmamore
* (1) Fe
* (2) IV 估计
*--- xtoverid 命令 ---
* 基本思想:如果二者都是一致的,那么系数估计值应当不存在显著差异
xA6_Panel_Data - Printed on 2011-11-25 10:43:03
* Hausman 检验值为负怎么办?
* 如果Hausman检验拒绝Re模型,怎么办?
xtreg market invest stock, re
xtoverid
use invest2.dta, clear
xtreg market invest stock, fe
est store m_fe
xtreg market invest stock, re
est store m_re
hausman m_fe m_re
use xtcs.dta, clear
xtreg tl size ndts tang tobin npr, fe
est store fe
xtreg tl size ndts tang tobin npr, re
est store re
hausman fe re
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481 *---------------------
482 *-6.2.3 一些常见问题
483
484 *-6.2.3.1 为何tsset命令总是报告错误信息?
485
486
487
488
489
490
491
492
493
494
495
496 *-6.2.3.2 数据的结构 -xtdes- -xtpattern-
497
498
499
500
501
502
503
504
505 *-6.2.3.3 为何有些变量会被drop掉?
506
507
508
509
510
511
512
513
514
515
516
517
518
use invest3.dta, clear
tsset id t
xtdes
duplicates report id t
duplicates example id t
list if id == 1
duplicates drop id t, force
tsset id t
// 错误
Page 7
xtdes
xtpattern, gen(pat) /*findit xtpattern*/
tab pat
keep if pat == "11111111111111111111" /*20个1*/
xtdes
use nlswork.dta, clear
tsset idcode year
xtreg ln_wage hours tenure ttl_exp, fe // 正常执行
* 产生种族虚拟变量
tab race, gen(dum_race)
xtreg ln_wage hours tenure ttl_exp dum_race2 dum_race3, fe
* 为何 dum_race2 和 dum_race3 会被 dropped ?
* 固定效应模型的设定:y_it = u_i + x_it*b + e_it (1)
* 由于个体效应 u_i 不随时间改变,
* 因此若 x_it 包含了任何不随时间改变的变量,
// written by arlion
// written by arlion
use grunfeld.dta, clear
drop in 28
drop in 55
drop in 87
drop in 94
xtdes
*- 转换
*- 主要目的:去除 varlist 中的缺漏值,把时间不连续的部分删除
cap erase grun1.dta
onespell invest mvalue kstock, saving(grun1) replace
use grun1.dta, clear
xtdes
*- 问题:在使用循环命令(如forvalues)时,需要公司编号是连续的,
* 此时可采用 egen 命令提供的 group() 函数。
use xtcs.dta, clear
tsset code year
list code year tl if code<20
egen code_new = group(code)
list code code_new year tl if code<20
tsset code_new year
xtdes
* 启示:采用同样的方法,可以针对任何类别变量生成连续编号
xA6_Panel_Data - Printed on 2011-11-25 10:43:03
* 都会与 u_i 构成多重共线性,Stata会自动删除之。
*- 样本
// unbalanced
// many missing values
use invest3.dta, clear
duplicates drop id t,force
xtdes
xtbalance, range(3 19)
use abond91.dta, clear
tsset id year
xtdes
sum
xtbalance , rang(1978 1982) miss(_all)
xtdes
sum
519
520
521
522 *-6.2.3.4 unbalance —> balance
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539 *-6.2.3.5 得到时间连续的样本
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556 *-6.2.3.6 得到连续的公司编号
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572 *-6.2.3.7 长条形数据与扁平型数据的转换
573
574
575
576
577
578
579
580
581
582
583
584
585 *-6.2.3.8 绘图
586
587
588
589
590
591
592
use reshape.dta, clear
browse
* 扁平 —> 长条
reshape long r c, i(id) j(year)
tsset id year
list in 1/20, sep(4)
* 长条 —> 扁平
reshape wide r c, i(id) j(year)
browse
*-xtline
use invest2.dta, clear
xtline invest
xtline invest, overlay
xtline market invest stock
*-xtgraph
Page 8