李子奈《计量经济学》第三版例题及习题的stata解答

更新时间:2024-05-16 21:25:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

第二章

例2.1.1(p24)

(1)表2.1.2中E(Y|X=800)即条件均值的求法,将数据直接复制到stata中。 程序:

sum y if x==800 Variable Obs Mean Std. Dev. Min Max y

程序:

sum y if x==1100 4 605 34.78505 561 638 其他条件均值求法程序相同,sum是summarize 的缩写(横线表示最简省形式),显示变量的描述统计信息,包括:观测量数,均值,标准差,最小值,最大值,if是条件表达式。 Variable Obs Mean Std. Dev. Min Max y 6 825 121.698 638 968 程序:

sum y if x==1400 Variable Obs Mean Std. Dev. Min Max y 11 1045 116.3091 869 1210 (2)图2.1.1的做法: 程序:

twoway(scatter y x )(lfit y x ),title(\不同可支配收入水平组家庭消费支出的条件分布图\每月可支配收入(元)\每月消费支出(元)\

Scatter表示散点图选项,lfit表示回归线,title表示题目,xtick表示刻度,(500(500)4000)分别表示起始刻度,中间数表示以单位刻度,4000表示最后的刻度。要注意的是命令中的符号都要用英文字符,否则命令无效。这个图可以直接复制的,但是由于我的软件出问题,只能直接剪切,所以影响清晰度。 例2.3.1(p37)

将数据直接复制到stata中 程序: (1) total xiyi

Total Std. Err. [95% Conf. Interval] xiyi 4974750 1507821 1563822 8385678 return list

scalars:

r(skip) = 0 r(first) = 1 r(k_term) = 0 r(k_operator) = 0 r(k) = 0 r(k_level) = 0 r(output) = 1

r(b) = 4974750

r(se) = 1507820.761894463 g a=r(b) in 1 total xi2

Total表示求和,return list命令可以引用其中的数据,接下来在第一列生成一个新的变量代表xiyi的和,同样生成一个b代表xi平方的,a除以b即可得到bata

return list g b=r(b) in 1 di a/b .67 (2) mean Yi

gen m=r(b) in 1 mean Xi

g n=r(b) in 1 di m-n*0.67

142.4

由此得到回归方程:Y=142.4+0.67Xi

例2.6.2(p53)

程序:(1)回归

reg y x

(2) 求X的样本均值和样本方差:

mean x

Mean estimation Number of obs = 31 Mean Std. Err. [95% Conf. Interval] x 11363.69 591.7041 10155.27 12572.11

sum x ,d(d表示detail的省略,这个命令会产生更多的信息)

x Percentiles Smallest 1% 8871.27 8871.27 5% 8920.59 8920.5910% 9000.35 8941.08 Obs 3125% 9267.7 9000.35 Sum of Wgt. 3150% 9898.75 Mean 11363.69 Largest Std. Dev. 3294.46975% 12192.24 16015.5890% 16015.58 18265.1 Variance 1.09e+0795% 19977.52 19977.52 Skewness 1.69197399% 20667.91 20667.91 Kurtosis 4.739267

di r(Var)(特别注意Var的大小写)

10853528

例2.6.2(P56) (1)reg Y X

Source SS df MS Number of obs = 29 F( 1, 27) = 2214.60 Model 2.4819e+09 1 2.4819e+09 Prob > F = 0.0000 Residual 30259023.9 27 1120704.59 R-squared = 0.9880 Adj R-squared = 0.9875 Total 2.5122e+09 28 89720219.8 Root MSE = 1058.6 Y Coef. Std. Err. t P>|t| [95% Conf. Interval] X .4375268 .0092973 47.06 0.000 .4184503 .4566033 _cons 2091.295 334.987 6.24 0.000 1403.959 2778.632

(2)图2.6.1的绘制:

twoway (line Y X year),title(\中国居民可支配总收入X与消费总支出Y 的变动图\

Variance-weighted least-squares regression Number of obs = 31Goodness-of-fit chi2(28) = 73.28 Model chi2(2) = 263.97Prob > chi2 = 0.0000 Prob > chi2 = 0.0000 lnY Coef. Std. Err. z P>|z| [95% Conf. Interval] lnX1 .3177322 .0514579 6.17 0.000 .2168765 .4185879 lnX2 .428669 .0275805 15.54 0.000 .3746122 .4827257 _cons 2.338164 .4472981 5.23 0.000 1.461476 3.214852

例4.21(老师有标准答案)

reg Y X

Source SS df MS Number of obs = 29 F( 1, 27) = 2214.60 Model 2.4819e+09 1 2.4819e+09 Prob > F = 0.0000 Residual 30259023.9 27 1120704.59 R-squared = 0.9880 Adj R-squared = 0.9875 Total 2.5122e+09 28 89720219.8 Root MSE = 1058.6 Y Coef. Std. Err. t P>|t| [95% Conf. Interval] X .4375268 .0092973 47.06 0.000 .4184503 .4566033 _cons 2091.295 334.987 6.24 0.000 1403.959 2778.632

predict e,resid tsset year

time variable: year, 1978 to 2006 delta: 1 unit

line e year,title(\残差相关图\

scatter e e1,title(\残差相关图\

g T=_n g T2=T^2 reg Y X T2

Source SS df MS Number of obs = 29 F( 2, 26) = 5380.77 Model 2.5061e+09 2 1.2531e+09 Prob > F = 0.0000 Residual 6054792.7 26 232876.642 R-squared = 0.9976 Adj R-squared = 0.9974 Total 2.5122e+09 28 89720219.8 Root MSE = 482.57 Y Coef. Std. Err. t P>|t| [95% Conf. Interval] X .1761519 .0259858 6.78 0.000 .1227374 .2295664 T2 21.65582 2.124183 10.19 0.000 17.2895 26.02215 _cons 3328.191 195.0326 17.06 0.000 2927.296 3729.086 reg e X T2 e1

Source SS df MS Number of obs = 28 F( 3, 24) = 64.94 Model 25597419.6 3 8532473.19 Prob > F = 0.0000 Residual 3153351.72 24 131389.655 R-squared = 0.8903 Adj R-squared = 0.8766 Total 28750771.3 27 1064843.38 Root MSE = 362.48 e Coef. Std. Err. t P>|t| [95% Conf. Interval] X -.1435191 .0335797 -4.27 0.000 -.2128242 -.074214 T2 11.04582 2.915754 3.79 0.001 5.028004 17.06365 e1 .6186482 .1467037 4.22 0.000 .3158666 .9214297 _cons 910.3409 172.739 5.27 0.000 553.8251 1266.857

g e2=e[_n-1] reg e X T2 e1 e2

Source SS df MS Number of obs = 28 F( 4, 23) = 46.69 Model 25598535.3 4 6399633.84 Prob > F = 0.0000 Residual 3152235.94 23 137053.737 R-squared = 0.8904 Adj R-squared = 0.8713 Total 28750771.3 27 1064843.38 Root MSE = 370.21 e Coef. Std. Err. t P>|t| [95% Conf. Interval] X -.1421776 .0373799 -3.80 0.001 -.2195039 -.0648513 T2 10.80845 3.973581 2.72 0.012 2.58847 19.02843 e1 .6192203 .1499666 4.13 0.000 .3089908 .9294498 e2 4.183503 46.36562 0.09 0.929 -91.73108 100.0981 _cons 886.1107 321.3096 2.76 0.011 221.4311 1550.79

prais Y X T2,rhotype(orrc)

Prais-Winsten AR(1) regression -- iterated estimates Source SS df MS Number of obs = 29 F( 2, 26) = 1153.30 Model 215943215 2 107971607 Prob > F = 0.0000 Residual 2434113.93 26 93619.7664 R-squared = 0.9889 Adj R-squared = 0.9880 Total 218377329 28 7799190.31 Root MSE = 305.97 Y Coef. Std. Err. t P>|t| [95% Conf. Interval] X .1896298 .0292979 6.47 0.000 .1294071 .2498524 T2 20.79527 2.693162 7.72 0.000 15.25939 26.33114 _cons 3118.169 329.4324 9.47 0.000 2441.011 3795.327 rho .764553 Durbin-Watson statistic (original) 0.442033Durbin-Watson statistic (transformed) 1.361658

newey lnY lnX, lag(2)

例4.3.1(P140)

g lnX1=ln(X1) g lnX2=ln(X2) g lnX3=ln(X3) g lnX4=ln(X4) g lnX5=ln(X5) g lnY=ln(Y)

reg lnY lnX1 lnX2 lnX3 lnX4 lnX5

Source SS df MS Number of obs = 25 F( 5, 19) = 202.68 Model .205495866 5 .041099173 Prob > F = 0.0000 Residual .003852744 19 .000202776 R-squared = 0.9816 Adj R-squared = 0.9768 Total .209348611 24 .008722859 Root MSE = .01424 lnY Coef. Std. Err. t P>|t| [95% Conf. Interval] lnX1 .3811446 .050242 7.59 0.000 .275987 .4863022 lnX2 1.222289 .1351786 9.04 0.000 .9393566 1.505221 lnX3 -.0811099 .0153037 -5.30 0.000 -.1131409 -.0490789 lnX4 -.0472287 .0447674 -1.05 0.305 -.1409279 .0464705 lnX5 -.1011737 .0576866 -1.75 0.096 -.2219131 .0195656 _cons -4.173174 1.923624 -2.17 0.043 -8.199365 -.1469838

corr lnX1 lnX2 lnX3 lnX4 lnX5

lnX1 lnX2 lnX3 lnX4 lnX5 lnX1 1.0000 lnX2 -0.5687 1.0000 lnX3 0.4517 -0.2141 1.0000 lnX4 0.9644 -0.6976 0.3988 1.0000 lnX5 0.4402 -0.0733 0.4113 0.2795 1.0000

stepwise, pr(0.05) : reg Y X1 X2 X3 X4 X5

或者stepwise, pe(0.05) : reg Y X1 X2 X3 X4 X5(逐步向前回归和逐步向后回归)

reg lnY lnX1 lnX2 lnX3

Source SS df MS Number of obs = 25 F( 3, 21) = 320.34 Model .204871849 3 .068290616 Prob > F = 0.0000 Residual .004476761 21 .000213179 R-squared = 0.9786 Adj R-squared = 0.9756 Total .209348611 24 .008722859 Root MSE = .0146 lnY Coef. Std. Err. t P>|t| [95% Conf. Interval] lnX1 .3233849 .0108608 29.78 0.000 .3007987 .3459711 lnX2 1.290729 .0961534 13.42 0.000 1.090767 1.490691 lnX3 -.0867539 .0151549 -5.72 0.000 -.1182702 -.0552376 _cons -5.999638 1.162078 -5.16 0.000 -8.416312 -3.582964

例4.4.1(P151)

reg X1 X2 Z

Source SS df MS Number of obs = 31 F( 2, 28) = 1947.55 Model 323280649 2 161640324 Prob > F = 0.0000 Residual 2323912.12 28 82996.8616 R-squared = 0.9929 Adj R-squared = 0.9924 Total 325604561 30 10853485.4 Root MSE = 288.09 X1 Coef. Std. Err. t P>|t| [95% Conf. Interval] X2 -.470904 .1154633 -4.08 0.000 -.7074199 -.2343881 Z 1.460539 .0860022 16.98 0.000 1.284372 1.636707 _cons 132.7416 194.2843 0.68 0.500 -265.2317 530.7149

predict v,resid reg Y X1 X2 v

Source SS df MS Number of obs = 31 F( 3, 27) = 1313.48 Model 169977392 3 56659130.6 Prob > F = 0.0000 Residual 1164688.99 27 43136.6292 R-squared = 0.9932 Adj R-squared = 0.9924 Total 171142081 30 5704736.02 Root MSE = 207.69 Y Coef. Std. Err. t P>|t| [95% Conf. Interval] X1 .4502363 .042451 10.61 0.000 .3631339 .5373386 X2 .4025897 .0638268 6.31 0.000 .2716278 .5335515 v 1.191137 .1427031 8.35 0.000 .8983341 1.483939 _cons 155.6975 140.1522 1.11 0.276 -131.871 443.266

ivreg Y X2 (X1=Z)

Instrumental variables (2SLS) regression Source SS df MS Number of obs = 31 F( 2, 28) = 513.69 Model 166680210 2 83340105 Prob > F = 0.0000 Residual 4461870.66 28 159352.524 R-squared = 0.9739 Adj R-squared = 0.9721 Total 171142081 30 5704736.02 Root MSE = 399.19 Y Coef. Std. Err. t P>|t| [95% Conf. Interval] X1 .4502363 .0815915 5.52 0.000 .2831037 .6173688 X2 .4025897 .122676 3.28 0.003 .1512992 .6538801 _cons 155.6975 269.3743 0.58 0.568 -396.0907 707.4858

reg Y X1 X2

g X1=X-L.X g Y1=Y-L.Y reg Y1 X1

Source SS df MS Number of obs = 27 F( 1, 25) = 397.46 Model 485227703 1 485227703 Prob > F = 0.0000 Residual 30520493.1 25 1220819.73 R-squared = 0.9408 Adj R-squared = 0.9385 Total 515748196 26 19836469.1 Root MSE = 1104.9 Y1 Coef. Std. Err. t P>|t| [95% Conf. Interval] X1 .5964135 .0299158 19.94 0.000 .5348008 .6580262 _cons 889.3387 260.8835 3.41 0.002 352.0391 1426.638

estat dwatson

Durbin-Watson d-statistic( 2, 27) = .9608428

4-练习题10(P155)

reg Y X1 X2

Source SS df MS Number of obs = 10 F( 2, 7) = 88.85 Model 855305.867 2 427652.934 Prob > F = 0.0000 Residual 33694.1329 7 4813.44755 R-squared = 0.9621 Adj R-squared = 0.9513 Total 889000 9 98777.7778 Root MSE = 69.379 Y Coef. Std. Err. t P>|t| [95% Conf. Interval] X1 .5684245 .7160975 0.79 0.453 -1.124877 2.261726 X2 -.0058326 .0702937 -0.08 0.936 -.1720507 .1603855 _cons 245.5158 69.52348 3.53 0.010 81.11887 409.9127

第五章

例5.1.1(P160)

reg Y1 X1

Source SS df MS Number of obs = 31 F( 1, 29) = 583.13 Model 182436639 1 182436639 Prob > F = 0.0000 Residual 9072820.63 29 312855.884 R-squared = 0.9526 Adj R-squared = 0.9510 Total 191509460 30 6383648.66 Root MSE = 559.34 Y1 Coef. Std. Err. t P>|t| [95% Conf. Interval] X1 .6919714 .0286553 24.15 0.000 .6333648 .750578 _cons 450.3413 388.9091 1.16 0.256 -345.0672 1245.75

reg Y2 X2

Source SS df MS Number of obs = 31 F( 1, 29) = 247.88 Model 60399153.9 1 60399153.9 Prob > F = 0.0000 Residual 7066328.2 29 243666.49 R-squared = 0.8953 Adj R-squared = 0.8916 Total 67465482.1 30 2248849.4 Root MSE = 493.63 Y2 Coef. Std. Err. t P>|t| [95% Conf. Interval] X2 .7195035 .0456999 15.74 0.000 .6260367 .8129703 _cons 179.1848 221.5788 0.81 0.425 -273.9948 632.3644

tab(region),g(D)

region Freq. Percent Cum. 3??ò 31 50.00 50.00 ??′? 31 50.00 100.00 Total 62 100.00drop D1 g DX=D2*X

reg Y X D2 DX

Source SS df MS Number of obs = 62 F( 3, 58) = 992.43 Model 828461102 3 276153701 Prob > F = 0.0000 Residual 16139148.8 58 278261.187 R-squared = 0.9809 Adj R-squared = 0.9799 Total 844600251 61 13845905.8 Root MSE = 527.5 Y Coef. Std. Err. t P>|t| [95% Conf. Interval] X .6919714 .0270245 25.61 0.000 .6378759 .7460669 D2 -271.1565 436.5699 -0.62 0.537 -1145.046 602.7331 DX .0275321 .0558151 0.49 0.624 -.0841939 .1392581 _cons 450.3413 366.7772 1.23 0.224 -283.843 1184.526

例5.2.2(P168)(和书上的答案略有不同)

g X1=X[_n-1] g X2=X[_n-2] g X3=X[_n-3] g X4=X[_n-4] g X5=X[_n-5] g X6=X[_n-6] g X7=X[_n-7] g lnX=ln(X) g lnX1=ln(X1) g lnX2=ln(X2) g lnX3=ln(X3) g lnX4=ln(X4) g lnX5=ln(X5) g lnX6=ln(X6) g lnX7=ln(X7) g lnY=ln(Y)

g W0=lnX+lnX1+lnX2+lnX3+lnX4+lnX5+lnX6+lnX7

g W1=lnX1+2*lnX2+3*lnX3+lnX*4+lnX5*5+lnX6*6+lnX7*7 g W2=4*lnX2+9*lnX3+lnX4*16+lnX5*25+lnX6*36+lnX7*49 reg lnY W0 W1 W2

Source SS df MS Number of obs = 21 F( 3, 17) = 1187.99 Model 5.01994038 3 1.67331346 Prob > F = 0.0000 Residual .023944895 17 .001408523 R-squared = 0.9953 Adj R-squared = 0.9944 Total 5.04388527 20 .252194264 Root MSE = .03753 lnY Coef. Std. Err. t P>|t| [95% Conf. Interval] W0 .142537 .023324 6.11 0.000 .0933277 .1917464 W1 -.0580477 .0206204 -2.82 0.012 -.101553 -.0145424 W2 .0062676 .002926 2.14 0.047 .0000942 .012441 _cons 6.708366 .0426449 157.31 0.000 6.618393 6.798339

例5.2.2(P173)

tsset year

time variable: year, 1978 to 2007

delta: 1 unit

g Yt1=Y[_n-1] reg Y X P Yt1

Source SS df MS Number of obs = 29 F( 3, 25) = 5869.00 Model 2.2684e+09 3 756146042 Prob > F = 0.0000 Residual 3220932.79 25 128837.312 R-squared = 0.9986 Adj R-squared = 0.9984 Total 2.2717e+09 28 81130680.7 Root MSE = 358.94 Y Coef. Std. Err. t P>|t| [95% Conf. Interval] X .0357098 .012565 2.84 0.009 .0098317 .061588 P 7.455727 3.065732 2.43 0.023 1.141733 13.76972 Yt1 .7236337 .1327963 5.45 0.000 .4501346 .9971328 _cons -202.5274 221.9648 -0.91 0.370 -659.6724 254.6176

练习题5(P186)

(和例5.2.3相似,具体步骤略) (1)估计Y*

tsset year

time variable: year, 1970 to 1991

delta: 1 unit g Yt1=Y[_n-1] reg Y X Yt1

Source SS df MS Number of obs = 21 F( 2, 18) = 621.38 Model 51963.6177 2 25981.8089 Prob > F = 0.0000 Residual 752.640861 18 41.8133812 R-squared = 0.9857 Adj R-squared = 0.9841 Total 52716.2586 20 2635.81293 Root MSE = 6.4663 Y Coef. Std. Err. t P>|t| [95% Conf. Interval] X .6480192 .1034473 6.26 0.000 .4306844 .865354 Yt1 .2415177 .1223811 1.97 0.064 -.0155954 .4986308 _cons -14.5344 4.87717 -2.98 0.008 -24.78095 -4.287846 通过自回归模型的参数估计,可以得到Y即理想的或长期的新建厂房企业开支。

(2)存量调整模型(对数转换): g lnY=ln(Y)

g lnYt1=ln(Yt1) g lnX=ln(X)

*

reg lnY lnX lnYt1

Source SS df MS Number of obs = 21 F( 2, 18) = 1023.79 Model 6.15325454 2 3.07662727 Prob > F = 0.0000 Residual .054092643 18 .003005147 R-squared = 0.9913 Adj R-squared = 0.9903 Total 6.20734719 20 .310367359 Root MSE = .05482 lnY Coef. Std. Err. t P>|t| [95% Conf. Interval] lnX .9837083 .1342437 7.33 0.000 .7016728 1.265744 lnYt1 .1866692 .1068091 1.75 0.098 -.0377283 .4110668 _cons -1.134494 .2164561 -5.24 0.000 -1.589251 -.6797361

(3)以X*代表理想的销售量,用X和Xt-1表示,带入Y方程中,重新估计模型Y。通过整理得到β0,β1。(结果略)

g Xt1=X[_n-1] reg Y X Xt1

Source SS df MS Number of obs = 21 F( 2, 18) = 614.92 Model 51955.8336 2 25977.9168 Prob > F = 0.0000 Residual 760.425036 18 42.2458353 R-squared = 0.9856 Adj R-squared = 0.9840 Total 52716.2586 20 2635.81293 Root MSE = 6.4997 Y Coef. Std. Err. t P>|t| [95% Conf. Interval] X .4240542 .22186 1.91 0.072 -.0420564 .8901649 Xt1 .4196949 .2190631 1.92 0.071 -.0405395 .8799293 _cons -16.1012 4.505287 -3.57 0.002 -25.56645 -6.635942

第六章

例6.4.1(P215);(通过2SLS估计,其他方法略)

tsset year g Ct=C[_n-1] reg Y Ct G

Source SS df MS Number of obs = 29 F( 2, 26) = 3629.86 Model 1.4148e+11 2 7.0741e+10 Prob > F = 0.0000 Residual 506704733 26 19488643.6 R-squared = 0.9964 Adj R-squared = 0.9962 Total 1.4199e+11 28 5.0710e+09 Root MSE = 4414.6 Y Coef. Std. Err. t P>|t| [95% Conf. Interval] Ct 1.258635 .1354676 9.29 0.000 .9801776 1.537093 G 2.794437 .2190344 12.76 0.000 2.344205 3.244668 _cons 1291.153 1317.612 0.98 0.336 -1417.237 3999.543

predict m,xb

. reg C m Ct Source SS df MS Number of obs = 29 F( 2, 26) = 5194.25 Model 1.9921e+10 2 9.9604e+09 Prob > F = 0.0000 Residual 49857078.8 26 1917579.95 R-squared = 0.9975 Adj R-squared = 0.9973 Total 1.9971e+10 28 713236666 Root MSE = 1384.8 C Coef. Std. Err. t P>|t| [95% Conf. Interval] m .0851009 .0245869 3.46 0.002 .0345618 .1356401 Ct .8617011 .0728371 11.83 0.000 .7119823 1.01142 _cons 886.5725 400.8777 2.21 0.036 62.55666 1710.588

练习题8(P228)(2SLS)

reg M Y P

Source SS df MS Number of obs = 18 F( 2, 15) = 963.31 Model 2.3413e+11 2 1.1707e+11 Prob > F = 0.0000 Residual 1.8229e+09 15 121525509 R-squared = 0.9923 Adj R-squared = 0.9912 Total 2.3596e+11 17 1.3880e+10 Root MSE = 11024 M Coef. Std. Err. t P>|t| [95% Conf. Interval] Y 1.810219 .0631125 28.68 0.000 1.675698 1.94474 P -147.4108 62.84855 -2.35 0.033 -281.3693 -13.45225 _cons 2067.01 14473.77 0.14 0.888 -28783.1 32917.12

predict m,xb reg Y M C I

Source SS df MS Number of obs = 18 F( 3, 14) =31456.30 Model 8.1426e+10 3 2.7142e+10 Prob > F = 0.0000 Residual 12079846.3 14 862846.166 R-squared = 0.9999 Adj R-squared = 0.9998 Total 8.1438e+10 17 4.7905e+09 Root MSE = 928.9 Y Coef. Std. Err. t P>|t| [95% Conf. Interval] M -.0217166 .0195131 -1.11 0.284 -.063568 .0201348 C 1.601022 .0577308 27.73 0.000 1.477202 1.724843 I .8974178 .0368883 24.33 0.000 .8183003 .9765352 _cons 541.9417 803.0233 0.67 0.511 -1180.372 2264.255

1. 安装estout。最简单的方式是在stata的指令输入:

ssc install estout, replace

EST安装的指导网址是:http://repec.org/bocode/e/estout/installation.html 2.跑你的regression

3.写下这行指令esttab using test.rtf,然后就会出现个漂亮的表格给你(WORD文档)。只要再小幅修改,就可以直接用了。这个档案会存在my documentstata下。如果你用打开的是一个stata do file,结果会保存到do文件所在文件夹中。如果要得到excel文件,就把后缀改为.xls或者.csv就可以了

4.跑多个其实也不难,只要每跑完一个regression,你把它取个名字存起来:est store m1。m1是你要改的,第一个model所以我叫m1,第二个的话指令就变成est store m2,依次类推。

5.运行指令:esttab m1 m2 ... using test.rtf就行了。

异方差的检验:

Breusch-Pagan test in STATA: 其基本命令是:estat hettest var1 var2 var3

其中,var1 var2 var3 分别为你认为导致异方差性的几个自变量。是你自己设定的一个 滞后项数量。

同样,如果输出的P-Value 显著小于0.05,则拒绝原假设,即不存在异方差性。 White检验:

其基本命令是在完成基本的OLS 回归之后,输入 imtest, white

如果输出的P-Value 显著小于0.05,则拒绝原假设,即不存在异方差性

处理异方差性问题的方法: 方法一:WLS

WLS是GLS(一般最小二乘法)的一种,也可以说在异方差情形下的GLS就是WLS。在WLS下,我们设定扰动项的条件方差是某个解释变量子集的函数。之所以被称为加权最小二乘法,是因为这个估计最小化的是残差的加权平方和,而上述函数的倒数恰为其权重。 在stata中实现WLS的方法如下:

reg (被解释变量) (解释变量1) (解释变量2)…… [aweight=变量名] 其中,aweight后面的变量就是权重,是我们设定的函数。

一种经常的设定是假设扰动项的条件方差是所有解释变量的某个线性组合的指数函数。在stata中也可以方便地实现:

首先做标准的OLS回归,并得到残差项;

reg (被解释变量) (解释变量1) (解释变量2)…… predict r, resid

生成新变量logusq,并用它对所有解释变量做回归,得到这个回归的拟合值,再对这个拟合值求指数函数; gen logusq=ln(r^2)

reg logusq (解释变量1) (解释变量2)…… predict g, xb gen h=exp(g)

最后以h作为权重做WLS回归;

reg (被解释变量) (解释变量1) (解释变量2)…… [aweight=h]

如果我们确切地知道扰动项的协方差矩阵的形式,那么GLS估计是最小方差线性无偏估计,是所有线性估计中最好的。显然它比OLS更有效率。虽然GLS有很多好处,但有一个致命弱点:就是一般而言我们不知道扰动项的协方差矩阵,因而无法保证结果的有效性。 方法二:HC SE

There are 3 kinds of HC SE

(1)Huber-White Robust Standard Errors HC1, 其基本命令是: reg var1 var2 var3, robust

White(1980)证明了这种方法得到的标准误是渐进可用(asymptotically valid)的。这种方法的优点是简单,而且需要的信息少,在各种情况下都通用。缺点是损失了一些效率。这种方法在我们日常的实证研究中是最经常使用。 (2)MacKinnon-White SE HC2,其基本命令是: reg var1 var2 var3, hc2

(3)Long-Ervin SE HC3,其基本命令是: reg var1 var2 var3, hc3

约束条件检验:

如果需要检验两个变量,比如x 与y,之间系

数之间的关系,以检验两者系数相等为例,我们可以直接输入命令: test x=y

再如检验两者系数之和等于1,我们可以直接输入命令: test x+y=1

如果输出结果对应的P-Value 小于0.05,则说明原假设显著不成立,即拒绝原假设。

序列相关性问题的检验与处理

序列相关性问题的检验:

首先,要保证所用的数据必须为时间序列数据。如果原数据不是时间序列数据,

则需要进行必要的处理,最常用的方法就是: gen n=_n tsset n

这两个命令的意思是,首先要生成一个时间序列的标志变量n(或者t 也可以);

然后通过tsset 命令将这个数据集定义为依据时间序列标志变量n定义的时间序

列数据。

最直观的检验方式是通过观察残差分布,其基本步骤是在跑完回归之后,直接输 入

Predict error, stdp

这样就得到了残差值;然后输入命令: plot error n

会得到一个error 随n 变化的一个散点图。

D-W检验——对一阶自相关问题的检验:

D-W检验是对一阶自相关问题的常用检验方法,但是如果实际问题中存在高阶 序列相关性问题,则不能用这个检验方法。 D-W 检验的命令如下: 首先,输入回归命令,

reg Variable1 Variable2 Variable3…VariableM 输出一个简单的OLS估计结果。然后,再输入命令: dwstat

这时会输出一个DW 统计量。通过与临界值之间的比较,可以得出结论。也可 以执行如下命令 estat durbinalt

直接进行Durbin检验。

Breusch-GodfreyTest in STATA——检验高阶序列相关性: 在得到一个基本回归结果和error 之后,我们假设这样一个关系:

et = α0 + α1 et-1 + α2 et-2 …+ αk et-p + β1 x1t + β2 x2t … +βk xkt +εt BG 检验的原假设是:H0 : α1 = α2 = … αp =0。 其基本命令是: bgodfrey , lags(p)

其中p 是你自己设定的一个滞后项数量。如果输出的p-value 显著小于0.05,则 可以拒绝原假设,这就意味着模型存在p 阶序列相关性;如果输出的p-value 显 著大于0.05 甚至很大,则可以接受原假设,即不存在p 阶序列相关性。

处理序列相关性问题的方法——GLS: 常用的几种GLS 方法:

(1) Cochrane-Orcutt estimator 和Prais-Winsten estimator 其基本命令是

prais var1 var2 var3, corc

(2) Newey-West standard errors 其基本命令是

newey var1 var2 var3, lag(3)

其中,lag(3)意思是对三阶序列相关性问题进行处理;如果需要对p 阶序列相

关性问题进行处理,则为lag(p)

t因变量,g,f,c是自变量,_26存放了弟26个观测值,为需要预测的值 reg t g f c if _n!=26 点预测

predict taxpredict if _n==26

均值的区间预测

predictnl py=predict(xb),ci(lb ub) l(95)

因变量的区间预测

adjust g=117251.9 f=24649.95 c=99.9,stdf ci level(95)

Hausman检验是检验内生性的最常用的方法。它是通过比较一致估计量与有效估计量的Wald统计量。 命令格式为:

.hausman name-constistent [name-efficent] [,options]

其中,name-cosistent指一致估计的结果, name-efficent 指有效估计的结果。注意,一致、有效估计量的先后顺序不能改变。 Option选项:

constant 计算检验统计量将常数也包括在内,默认值为排除常数 allegs 利用所有方程进行检验,默认只对第一个方程进行检验 skipeqs(eqlist) eqlist只能以方程名称而不能以方程序号表示 equation(matchlist) 比较设定的方程。 force 即使假设条件不满足仍进行检验

df(#) 默认值为一致估计与有效估计的协方差矩阵的差的估计 sigmamore 协方差矩阵采用有效估计量的协方差矩阵 sigmaless 协方差矩阵采用一致估计量的协方差矩阵 tconsistent(string) 一致估计量的标题 tefficient(string) 有效估计量的标题

工具变量估计 命令格式:

.ivregress esitimator depvar [varlist1] [varlist2=varlist_iv] [if] [in] [weight][,options]

其中,estimator包括2sls,gmm,liml三种。varlist1为模型中的外生变量,varlist2为模型中的内生变量,varlist_iv为模型中的工具变量。 Nonconstant 不包括常数项 Hascons 用户自己设定常数项 CMM 选项:

wmatrix(wmtype) robust,cluster clustvar,hac kernel, unadjusted center 权数矩阵采用中心矩 igmm 采用迭代GMM估计

eps(#) 参数收敛标准。默认值为eps(le-6) weps(#) 权数矩阵的收敛标准。默认值为w eps(le-6)

Vce(vcetype) unajusted,robust,cluster clustvar,bootstrap,jackknife,hac kernel level(#)置信区间

First 输出第一阶段的估计结果 Small 小样本下的自由度调整 .estat firststage [,all forcenonrobust]

该命令给出第一阶段的估计结果以及各种统计量,包括排除外生变量的相关性检验。All选项给出所有的拟合优度统计量。如果模型存在多个内生变量,则stata给出R2、偏R2、调整的R2 、F统计量;如果模型存在多个内生变量,则stata给出Shea偏R2和调整的偏R2。

forcenonrobust给出最小特征值统计量及其临界值,即使采用稳健估计(这一检验的假设条件是误差项为独立正态分布)。

estat overid[,lag(#) forceweights forcenonrobust]

该命令给出了过度识别约束检验。如果使用2sls估计估计,则Stata给Sargan’s(1958)和Basman’s(1960)卡方统计量,这也是Wooldridge’(1995)稳健得分检验。 如果采用liml估计方法,则stata给出Anderson and Rubin’s(1950) 卡方统计量以及Basmann F统计量;如果采用GMM估计,则stata给出hansen’s(1982)J统计量。Lags(#)用于计算得分检验的HAC(异方差自相关一致)统计量的过程中进行去噪时设定滞后阶数。如果设定lag(0),则表示不进行去噪处理。默认选择为lag(1)。这一选择仅使用于2sls估计方法和设定vce(hac)选项情况。 Forceweight 表示即使采用aweights,pweights或iweights也进行检验。Stata仅对于fweights的情况进行检验,其他权数所得到临界值可能不准确。

Forcenonrobust 指在2sls或LIML估计中即使采用稳健标准差也进行Sargan and Basmann检验(这一检验的假设的假设条件是误差项为独立正态分布)。 例子:

log(wage)=a+b*educ+c*exper+d*expersq+u

怀疑模型教育(educ)具有内生性问题,利用父母接受教育的年数(fatheduc,motheduc)作educ的工具变量估计上述模型。 (1)利用2SLS估计模型

.ivregress 2sls lwage exper expersq (educ=fatheduc motheduc),first 第一阶段回归结果为:

educhat=9.1+0.19fatheduc+0.16motheduc+0.05exper (21.34) (5.62) (4.39) (1.12) - 0.001expersq (-0.84)

第二阶段的估计结果为:

lwagehat=0.05+0.06educ+0.04exper-0.001expersq (0.12) (1.95) (5.29) (-2.24) (2)检验educ的内生性

.quietly ivreg iwage exper expersq {educ=fatheduc motheduc} .est store IV_reg

.quietly regress lwage exper expersq educ .est store LS_reg

.hausman IV_reg LS_reg

可以得到hausman估计量=2.7,P值=0.44。接受原假设,即educ是外生的。 (3)进行过度识别的约束检验 .estat overid

可得Sargan统计量=0.38,P值=0.54接受原假设。 面板数据估计

首先对面板数据进行声明: 前面是截面单元,后面是时间标识: tsset company year tsset industry year

产生新的变量:gen newvar=human*lnrd 产生滞后变量Gen fiscal(2)=L2.fiscal 产生差分变量Gen fiscal(D)=D.fiscal

描述性统计:

xtdes :对Panel Data截面个数、时间跨度的整体描述 Xtsum:分组内、组间和样本整体计算各个变量的基本统计量 xttab 采用列表的方式显示某个变量的分布

Stata中用于估计面板模型的主要命令:xtreg xtreg depvar [varlist] [if exp] , model_type [level(#) ] Model type 模型

be Between-effects estimator fe Fixed-effects estimator re GLS Random-effects estimator pa GEE population-averaged estimator

mle Maximum-likelihood Random-effects estimator 主要估计方法:

xtreg: Fixed-, between- and random-effects, and population-averaged linear models xtregar:Fixed- and random-effects linear models with an AR(1) disturbance xtpcse :OLS or Prais-Winsten models with panel-corrected standard errors

xtrchh :Hildreth-Houck random coefficients models

xtivreg :Instrumental variables and two-stage least squares for panel-data models xtabond:Arellano-Bond linear, dynamic panel data estimator xttobit :Random-effects tobit models

xtlogit : Fixed-effects, random-effects, population-averaged logit models xtprobit :Random-effects and population-averaged probit models xtfrontier :Stochastic frontier models for panel-data xtrc gdp invest culture edu sci health social admin,beta

xtreg命令的应用:

声明面板数据类型:tsset sheng t 描述性统计:xtsum gdp invest sci admin 1.固定效应模型估计:

xtreg gdp invest culture sci health admin techno,fe

固定效应模型中个体效应和随机干扰项的方差估计值(分别为sigma u 和sigma e),二者之间的相关关系(rho)

最后一行给出了检验固定效应是否显著的F 统计量和相应的P 值

2.随机效应模型估计:

xtreg gdp invest culture sci health admin techno,re 检验随机效应模型是否优于混合OLS 模型: 在进行随机效应回归之后,使用xttest0

检验得到的P 值为0.0000,表明随机效应模型优于混合OLS 模型 3. 最大似然估计Ml:

xtreg gdp invest culture sci health admin techno,mle

Hausman检验

Hausman检验究竟选择固定效应模型还是随机效应模型: 第一步:估计固定效应模型,存储结果 xtreg gdp invest culture sci health admin techno,fe est store fe

第二步:估计随机效应模型,存储结果 xtreg gdp invest culture sci health admin techno,re est store re

第三步:进行hausman检验 hausman fe

Hausman检验量为:

H=(b-B)′[Var(b)-Var(B)]-1(b-B)~x2(k)

Hausman统计量服从自由度为k的χ2分布。当H大于一定显著水平的临界值时,我们就认为模型中存在固定效应,从而选用固定效应模型,否则选用随机效应模型

如果hausman检验值为负,说明的模型设定有问题,导致Hausman 检验的基本假设得不到满足,遗漏变量的问题,或者某些变量是非平稳等等 可以改用hausman检验的其他形式: hausman fe, sigmaless

对于固定效应模型的异方差检验和序列相关检验: Xtserial gdp invest culture sci health admin techno 异方差检验:

xtreg gdp invest culture sci health admin techno,fe

xttest3 (Modified Wald statistic for groupwise heteroskedasticity in fixed effect model) 随机效应模型的序列相关检验:

xtreg gdp invest culture sci health admin techno,re Xttest1

Xttest1用于检验随机效应(单尾和双尾) 、一阶序列相关以及两者的联合显著

检验结果表明存在随机效应和序列相关,而且对随机效应和序列相关的联合检验也非常显著 可以使用广义线性模型xtgls对异方差和序列相关进行修正:

xtgls gdp invest culture sci health admin techno, panels(hetero),修正异方差

xtgls gdp invest culture sci health admin techno, panels(correlated),修正依横截面而变化的异方差

xtgls gdp invest culture sci health admin techno, panels(hetero) corr(ar1),修正异方差和一阶序列相关ar(1)

本文来源:https://www.bwwdw.com/article/h0l7.html

Top