步学会用MATLAB做空间计量回归详细步骤.doc

上传人:scccc 文档编号:12753384 上传时间:2021-12-05 格式:DOC 页数:43 大小:806KB
返回 下载 相关 举报
步学会用MATLAB做空间计量回归详细步骤.doc_第1页
第1页 / 共43页
步学会用MATLAB做空间计量回归详细步骤.doc_第2页
第2页 / 共43页
步学会用MATLAB做空间计量回归详细步骤.doc_第3页
第3页 / 共43页
步学会用MATLAB做空间计量回归详细步骤.doc_第4页
第4页 / 共43页
步学会用MATLAB做空间计量回归详细步骤.doc_第5页
第5页 / 共43页
点击查看更多>>
资源描述

《步学会用MATLAB做空间计量回归详细步骤.doc》由会员分享,可在线阅读,更多相关《步学会用MATLAB做空间计量回归详细步骤.doc(43页珍藏版)》请在三一文库上搜索。

1、与MATLAB链接:Excel :选项一一加载项一一 COM加载项一一转到一一没有勾选项2. MATLAB安装目录中寻找toolboxexlink点击,启用宏E:MATLABtoolboxexlink務次口朗Contents2013/12/28 2d4MATLAB Code1 KBQL excllink2013/10/9 23:48WtcfOMil Excel 108 KBC| exdlink2OO32013/10/9 29:朝Microsoft Excel 屮305 KBB' ExliSamp2009/11/5 19;58MJcrosoft Excel .72 KBgetfuncti

2、onheip2010/12/4 2:17MAT LA冬 Code2 K8' getfunctionlist2010/12/4 2tl7MATLAB Code3 KB松getfLinctionsiriaiure2012/5/4 2:59N?ATLAB Code4 Kfi邕 info2007/7/24 0:22XML1 KB然后,Excel屮就出现 MATLAB工具FCQ循入CXL壬兰动環印塞静筆元格zTATLABIB(注意Excel中的数据:)zy4U4 uurjjjU 诧bU4U1UUZ0/MT4 11Z14 b29414 8402420 1801810 1585194 7757825

3、792452 129424.6839810.118179-0.03264.8548996354.14966.129434.3956830.3505260.1754084 86645351S223950.229444 5649670 120077-0 03264 5685861862 11465429454 56642902295380 2408064 7582615017 33853.829464 7077270.10028701313914 711692483.3L 360 5I权:重数据1 5heet3 j3启动 matlab(1 ) 点击 start MATLAB(2 ) senddat

4、a to matlab ,并对变量矩阵变量进行命名(注意:选取变量为数值,不包括各变量)(HMk1woe>t0£nuonMP畑巧141 MH41叫紳SB?2W fQ2< ttfcuW跡MMl034 «7»)1fl 17M?)sw:W'T?H30d42i»U1WTWI叫懒席Q!曲如H.S617:F*iMHaf1 g就4 15214 15211 Jtt2lV血surV?«翊4 Zt4.4%如Q1皿轴剛5:出如: /jmth0t如T创町询1* 40it4 6UM14的越血竹XMfeJS*对让;Qu11小蝴4 ai)M<KM巧

5、0124T3JJ"-ozjm:-azwn丄2?跖广4T5sw巧U01)4 WMK*4砂IfMimr/Q14i e*Bw4J17W?4JY7W:A?51M:'叫QuIJWM记 JJin阳F1W r,i?a114 WM-qffiM*AW3IZ1MW0it1 WMI可l»U砒则4. H Z*Cfl* f 'giiimvaz4KNB1M4 V5W»WM砂E0»4 95 mi4 WJI砒3W!yzzrM:EH !y*0JI1如82FXM:磅理啓d »MUMM!M2:2?Ji4 JZlMrHFcrur4J u-nuA71ZW1如Z3Ufr0

6、22QWF4 UAH1WW皆323丄 fl9 «M2-flFT处5EiS«!M?wrAj4 r?MflTrtMiV.41 tv.udmiun e亠NUMJUK* i <(data表屮数据进行命名)(空间权重进行命名)(3 ) 导入MATLAB中的两个矩阵变量就可以看见八 MATLAB R2*.亠. 主页*应馬稈序由资逹轄运mott的二囚打开麗C 'Simulink齐信我逹新逹导入4将elhorst和jplv7两个程序文件夹复制到MATLAB安装目录的toolbox文件夹5- elhorst2017/1/W 1908文样與心 jplv72017/1/15 &am

7、p;46女忖5 设置路径:唱K甌址1_密密缜再文件裘渤国澤径, v'卞 程序(£:) > MATLAB toolbox 勺;攬箕P1 el4A詠*修8田期F"云.:上讣/2017/1/16 12:19-jmsyil local2017/1/14 9 24/ Ire2017/1/14 9 22文舛灭胪ap2G17/1/14 Q 21matlab2017/1/1 9 24文枠rMthb prr>gram200-?G147017/1/15 259丈<4JB洌 matlabxl2017/1/1423如喪J J®b t 事再 If、mbc2017/

8、V14 9 25丈<4仏 iBS#【G)e怕pc2017/1/14 24a专业(氏nnet2017/1/14 925穩序(&)A op2O17/V1 925d資科Vh)注 oo:irr?017/14 925文除J i-«1 CD敗祈谿M< 1現 文件舟jpl"6 输入程序,得出结果4 C5一 . T 丁 c徐宀 r . D 1 d.tac - 门 - Q C .f "cwsT=30;N=46;W=normw(W1);y=A(:,3);x=A(:,4,6);xconstant=ones(N*T,1);nobs K=size(x);results=

9、ols(y,xconstant x);vnames=strvcat(logcitVi nterceptTlogp','logy);prt_reg(results,vnames,1);sige=*(nobs-K )/no bs);loglikols=-nobs/2*log(2*pi*sige)-1/(2*sige)*1*% The (robust)LM tests developed by ElhorstLMsarsem_panel(results,W5y,xconstant x); % (Robust) LM tests解释每一行分别表示:该面板数据的时期数为30 ( T=30

10、 ),该面板数据有30个地区(N=30 ),将空间权重矩阵标准化(W=normw(w1),将名为A (以矩阵形式出现在 MATLABA中)的变量的第 3列数据定义为被解释变量y,将名为A的变量的第4、5、6列数据定义为解释变量矩阵 X,定义一个有N*T行,1列的全1矩阵,该矩阵名为:xconstant , ( ones即为全1矩阵)说明解释变量矩阵x的大小:有nobs行,K列。(size为描述矩阵的大小)。附录:静态面板空间计量经济学一、OLS静态面板编程1、普通面板编程T=30;N=46;W=normw(W1);y=A(:,3);x=A(:,4,6);xconstant=ones(N*T,1

11、);nobs K=size(x);results=ols(y,xconstant x);vnames=strvcat( logcit* /intercept' ,'logp* ,'logy1);prt_reg(results,vnames,1);sige=*(nobs-K )/no bs);Ioglikols=-nobs/2*log(2*pi*sige)-1/(2*sige)*% The (robust)LM tests developed by ElhorstLMsarsem_panel(results,W,y,xconstant x); % (Robust) LM

12、tests2、空间固定 OLS (spatial-fixed effects )T=30;N=46;W=normw(W1);y=A(:,3);x=A(:,4,6);xconstant=ones(N*T,1);nobs K=size(x);model=1;ywith,xwith,mea nn y,mea nn x,meanty,meantx=demean(y,x,N,TJmodel);results=ols(ywith,xwith);vnames=strvcat(,logcit,/logp,logy,); % should be changed if x ischangedprt_reg(res

13、ults,vnames);sfe=meanny-meannx*; % including the constant term yme = y - mean(y);et=ones(T,1);error=y-kron(et,sfe)-x*;rsqrl = error" error;rsqr2 = yme'*yme;FE_rsqr2 = - rsqrl/rsqr2 % r-squared including fixed effectssige=*(nobs-K)/nobs);logliksfe=-nobs/2*log(2*pi*sige)-1/(2*sige)*1*LMsarsem

14、_panel(results,W,ywith,xwith); % (Robust) LM tests3、时期固定 OLS (time-period fixed effects )T=30;N=46;W=normw(W1);y=A(:,3); x=A(:,4,6);xconstant=ones(N*T,1);nobs K=size(x);model=2;ywith,xwith,mea nn y,mea nn x,meanty,meantx=demean(y,x,N,T,model);results=ols(ywith,xwith);vnames=strvcat(logcit7logp,/logy

15、,); % should be changed if x is changedprt_reg(results,vnames);tfe=meanty-meantx*; % including the constant termyme = y - mean(y);en=ones(N,1);error=y-kron(tfe,en)-x*;rsqrl = errorf*error;rsqr2 = yme'*yme;FE_rsqr2 = - rsqr1/rsqr2 % r-squared including fixed effects sige=*(nobs-K )/no bs);loglikt

16、fe=-nobs/2*log(2*pi*sige)-1/(2*sige)*1*LMsarsem_panel(results,W,ywithJxwith); % (Robust) LM tests4、空间与时间双固定模型T=30;N=46;W=normw(W1);y=A(:,3);x=A(:,4,6);xconstant=ones(N*T,1);nobs K=size(x);model=3;ywith,xwith,mea nn y,meannx,meanty,meantx=demean(y,x,N5T,model);results=ols(ywith,xwith);vnames=strvcat(

17、,logcit7logp,logy,); % should be changed if x is changedprt_reg(results,vnames)en=ones(N,1);et=ones(T,1);intercept=mean(y)-mean(x)*;sfe=meanny-mea nn x*(en,intercept);tfe=meanty-meantx*(et,intercept);yme = y - mean(y);ent=ones(N*T,1);error=y-kron(tfe,en)-kron(et,sfe)-x*(ent,intercept);rsqrl = errorf

18、*error;rsqr2 = yme'*yme;FE_rsqr2 = - rsqrl/rsqr2 % r-squared including fixed effectssige=*(nobs-K )/no bs);loglikstfe=-nobs/2*log(2*pi*sige)-1/(2*sige)*1*LMsarsem_panel(results,W,ywith,xwith); % (Robust) LM tests二、静态面板SAR模型1 > 无固定效应(No fixed effects)T=30;N=46;W=normw(W1);y=A(:,3);x=A(:,4,6);f

19、or t=1:Tt1=(t-1)*N+1;t2=t*N;wx(t1:t2,:)=W*x(t1:t2,:);endxconstant=ones(N*T,1);nobs K=size(x);=0;=0;=0;results=sar_panel_FE(y,xc onstant x,W,T,i nfo);vnames=strvcat( 'logcit', 'intercept', 'logp' , 'logy' );prt_sp new(results,v names, 1)% Print out effectsestimates spa

20、t_model=0;direct_indirect_effects_estimates(results,W,spat_model);pan el_effects_sar(results,vnames5W);2、空间固定效应(Spatial fixed effects)T=30;N=46;W=normw(W1);y=A(:,3);x=A(:,4,6);for t=1:Tt1=(t-1)*N+1;t2=t*N;wx(t1:t2,:)=W*x(t1:t2,:);endxconstant=ones(N*T,1);nobs K=size(x);=0;=1;=0;results=sar_pa nel_FE

21、(y,x,W,T,info);vnames=strvcat('logcit1 , logp1 , 'logy1 );prt_sp new(results,v names J)% Print out effectsestimates spat_model=0;pan el_effects_sar(results,vnames,W);direct_indirect_effects_estimates(results,W,spat_model);3、时点固定效应(Time period fixed effectsT=30;N=46;W=normw(W1);y=A(:,3);x=A(:

22、,4,6);for t=1:Tt1=(t-1)*N+1;t2=t*N;wx(t1:t2,:)=W*x(t1:t2,:);endxconstant=ones(N*T,1);nobs K=size(x);=0;% required for exact results=2;=0;% Do not print intercept and fixed effects; use =1 to turn onresults=sar_pa nel_FE(y,x,W,T,info);vnames=strvcat('logcif , logp' , logy* );prt_sp new(result

23、s,v names J)% Print out effectsestimates spat_model=0;direct_indirect_effects_estimates(results,W,spat_model);4、双固定效应 (Spatial and time period fixed effectspan el_effects_sar(results,vnames3W);T=30;N=46;W=normw(W1);y=A(:,3);x=A(:,4,6);for t=1:Tt1=(t-1)*N+1;t2=t*N;wx(t1:t2,:)=W*x(t1:t2,:);endxconstan

24、t=ones(N*T,1);nobs K=size(x);=0;% required for exact results=3;=0;% Do not print intercept and fixed effects; use =1 to turn onresults=sar_pa nel_FE(y,x,W,T,info);vnames=strvcat('logcit1 , logp1 , 'logy1 );prt_sp new(results,v names J)% Print out effectsestimates spat_model=0;direct_indirect

25、_effects_estimates(results,W5spat_model);pan el_effects_sar(results,vnames3W);三、静态面板SDM模型1、无固定效应(No fixed effectsT=30;N=46;W=normw(W1);y=A(:,3);x=A(:,4,6);for t=1:Tt1=(t-1)*N+1;t2=t*N;wx(t1:t2,:)=W*x(t1:t2,:);endxconstant=ones(N*T,1);nobs K=size(x);=0;=0;=0;results=sar_pa nel_FE(y,xc on stant x wx,W

26、,T,info);vnames=strvcat( 'logcit1 , Intercept1, 'logp1 , logy1 , 'WJogp' , 'WJogy' );prt_sp new(results,v names J)% Print out effectsestimates spat_model=1;direct_indirect_effects_estimates(results,W5spat_model);pan el_effects_sdm(results,vnames,W);2> 空间固定效应(Spatial fixed

27、effects)T=30;N=46;W=normw(W1);y=A(:,3);x=A(:,4,6);for t=1:Tt1=(t-1)*N+1;t2=t*N;wx(t1:t2,:)=W*x(t1:t2,:);endxconstant=ones(N*T,1);nobs K=size(x);=0;% required for exact results=1;=0;% Do not print intercept and fixed effects; use =1 to turn on,WJogy');results=sar_pa nel_FE(y,x wx,W,T,i nfo);vname

28、s=strvcat( 'logcit1 , logp1 , 'logy1 JWJogp' prt_sp new(results,v names)% Print out effectsestimates spat_model=1;direct_indirect_effects_estimates(results,W5spat_model);pan e l_ef f ects_sd m (res u I ts, v names,W);3、时点固定效应(Time period fixed effectsT=30;N=46;x=A(:,4,6);for t=1:Tt1=(t-1

29、)*N+1;t2=t*N;wx(t1:t2,:)=W*x(t1:t2,:);endxconstant=ones(N*T,1);nobs K=size(x);=0;% required for exact results=2;=0;% Do not print intercept and fixed effects; use =1 to turn on% New routines to calculate effects estimatesresults=sar_panel_FE(y,x wx,W,T,info);,W*logy );vnames=strvcat( 'logcit1 ,

30、logp' , 'logy* JWJogp'% Print out coefficient estimatesprt_spnew(results,vnames, 1)% Print out effectsestimates spat_model=1;direct_indirect_effects_estimates(results,W5spat_model);pan e l_ef f ects_sd m (res u I ts, v names,W)4、双固定效应 (Spatial and time period fixed effectsT=30;N=46;W=nor

31、mw(W1);y=A(:,3);x=A(:,4,6);for t=1:Tt1=(t-1)*N+1;t2=t*N;wx(t1:t2,:)=W*x(t1:t2,:);endxconstant=ones(N*T,1);nobs K=size(x);=0;=0;% required for exact results=3;=0;% Do not print intercept and fixed effects; use =1 to turn onresults=sar_panel_FE(y,x wx,W,T,info);vnames=strvcat( 'logcit' , logp&

32、#39; , 'logy* , 'W*logp* , 'W*logy' );prt_sp new(results,v names, 1)% Print out effectsestimates spat_model=1;direct_indirect_effects_estimates(results,W5spat_model);pan el_effects_sdm(results,v names,W)wald test spatial lag% Wald test for spatial Durbin model against spatial lagmode

33、l btemp=;varcov=;Rafg=zeros(K,2*K+2);for k=1:KRafg(k,K+k)=1;end% R(1,3)=0 and R4)=0;Wald_spatial_lag=(Rafg*btemp),*inv(Rafg*varcov*Rafgl)*Rafg*btempprob_spatial_lag=1 -chis_cdf (Wald_spatial_lag, K)wald test spatial error% Wald test spatial Durbin model against spatial errormodel R=zeros(K,1);for k=

34、1:KR(k)=btemp(2*K+1 )*btemp(k)+btemp(K+k);% k changed in 1,7/12/2010% R(1 )=btemp(5)*btemp(1 )+btemp(3);% R(2)=btemp(5)*btemp(2)+btemp(4);endRafg=zeros(K,2*K+2);for k=1:KRafg(k,k) =btemp(2*K+1);% k changed in 1,7/12/2010Rafg(k,K+k)=1;Rafg(k,2*K+1 )=btemp(k);% Rafg(1,1 )=btemp(5);Rafg(1,3)=1 ;Rafg(1,

35、5)=btemp(1);% Rafg(2,2)=btemp(5);Rafg(2,4)=1 ;Rafg(2,5)=btemp(2);endWald_spatial_error=R'*inv(Rafg*varcov*Rafg')*Rprob_spatial_error=1 -chis_cdf (Wald_spatial_error,K)LR test spatial lagresultssar=sar_pa nel_FE(y,x,W,T,info);LR_spatial_lag=-2* (LR_spatial_lag,K)LR test spatial errorresultsse

36、m=sem_pa nel_FE(y,x,W,T,info);LR_spatial_error=-2* (LR_spatial_error,K)5、空间随机效应与时点固定效应模型T=30;N=46;W=normw(W1);y=A(:,3);x=A(:,4,6);for t=1:Tt1=(t-1)*N+1;t2=t*N;wx(t1:t2,:)=W*x(t1:t2,:);endxconstant=ones(N*T,1);nobs K=size(x);ywith,xwith,meanny,meannx,meanty5meantx=demean(y,x wx,N,T,2);2=time dummies=

37、1;results=sar_pa nel_RE(ywith,xwith,W,T,i nfo);prt_sp new(results,v names J) spat_model=1;direct_indirect_effects_estimates(results,W5spat_model);pan e l_ef f ects_sd m (res u I ts, v names,W)wald test spatial lagbtemp=(1:2*K+2);varcov=(1:2*K+2,1:2*K+2);Rafg=zeros(K,2*K+2);for k=1:KRafg(k,K+k)=1;% R

38、(1,3)=0 and R(2,4)=0;endWald_spatial_lag=(Rafg*btemp)'*inv(Rafg*varcov*Rafg')*Rafg*btempprob_spatial_lag= chis_cdf (Wald_spatial_lag, K)wald test spatial errorR=zeros(K,1);for k=1:K% k changed in 1,R(k)=btemp(2*K+1)*btemp(k)+btemp(K+k);7/12/2010% R(1 )=btemp(5)*btemp(1 )+btemp(3);% R(2)=btem

39、p(5)*btemp(2)+btemp(4);endRafg=zeros(K,2*K+2);for k=1:KRafg(k,k) =btemp(2*K+1);% k changed in 1,7/12/2010Rafg(k,K+k)=1;Rafg(k,2*K+1 )=btemp(k);% Rafg(1,1)=btemp(5);Rafg(1,3)=1 ;Rafg(1,5)=btemp(1);% Rafg(2,2)=btemp(5);Rafg(2,4)=1 ;Rafg(2,5)=btemp(2);endWald_spatial_error=R'*inv(Rafg*varcov*Rafg&#

40、39;)*Rprob_spatial_error= 1-chis_cdf (Wald_spatial_error,K)LR test spatial lagresultssar=sar_panel_RE(ywith,xwith(:,1:K),W,T,info);LR_spatial_lag=-2* (LR_spatial_lag,K)LR test spatial errorresultssem=sem_pa nel_RE(ywith,xwith(:,1:K),W5T,info);LR_spatial_error=-2* (LR_spatial_error,K)四、静态面板SEM模型1 >

41、; 无固定效应(No fixed effects)T=30;N=46;W=normw(W1);y=A(:,3);x=A(:,4,6);for t=1:Tt1=(t-1)*N+1;t2=t*N;wx(t1:t2,:)=W*x(t1:t2,:);endxconstant=ones(N*T,1);nobs K=size(x);=0;=0;=0;results=sem_panel_FE(y,xconstant x,W,T,info);vnames=strvcat('logcif, Sntercept1, logp* , logy1 );prt_sp new(results,v names, 1

42、)% Print out effectsestimates spat_model=0;direct_indirect_effects_estimates(results,W5spat_model);pan el_effects_sar(results,vnames,W);2、空间固定效应(Spatial fixed effectsT=30;N=46;W=normw(W1);y=A(:,3);x=A(:,4,6);for t=1:Tt1=(t-1)*N+1;t2=t*N;wx(t1:t2,:)=W*x(t1:t2,:);endxconstant=ones(N*T,1);nobs K=size(x

43、);=0;=1;=0;results=sem_pa nel_FE(y,x,W,T,i nfo);vnames=strvcat('logcif , logp' , logy* );prt_sp new(results,v names J)% Print out effectsestimates spat_model=0;direct_in direct_effects_estimates(results,W,spat_model);pan el_effects_sar(results,v names,W);3、时点固定效应(Time period fixed effects)T=30;N=46;W=normw(W1);y=A(:,3)

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1