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

上传人:仙*** 文档编号:34997504 上传时间:2021-10-25 格式:DOC 页数:35 大小:454.50KB
返回 下载 相关 举报
六步学会用MATLAB做空间计量回归详细步骤_第1页
第1页 / 共35页
六步学会用MATLAB做空间计量回归详细步骤_第2页
第2页 / 共35页
六步学会用MATLAB做空间计量回归详细步骤_第3页
第3页 / 共35页
点击查看更多>>
资源描述
与MATLAB链接:Excel :选项一一加载项一一COM加载项一一转到一一没有勾选项COM加载项.它一f li Illi I * il一 II ai Ha I II ii III2. MATLAB安装目录中寻找 toolbox exlink 点击,启用宏E:MATLABtoolboxexli nk否hitrsctnw夷gkt* Contents2013/12/28 2:14MATLAB Cede1 KBQ eKdlink2013/10/9 3:48Microsoft Excel 108 KB &Kcllink2(X)32013/10/ 23:43Microsoft Excel305 KB园 ExliSamp2009/11/5 19:58Microsoft Excel,“72 KB cetfu nctfo nh el p2010/12/4 2:17MATLAB 匚ode2 KB单 getfunctionlkt2010/12/1 2tl7MATLAB Cede3 KBcetifijrtcti3n5igfia:ure2OL2/5/4 2:59MATLAB Code4 KB空info2007/7/24 0:22XML文档1 KB然后,Excel中就出现MATLABT具洎样式1 nLi*|SA 删徐福式WT单訣禅.一丿蝙曙MATLA6x(注意Excel中的数据:)羽4U4 UUfJJJU lbU4U.1U业旳4 awsbJ1C14 1114.b29414 3402420 1801810.1535194 775762579.2452 129424.6839B10.118175-0.03264.6&48996354.14965.129434.39668313505260.1764084.8564635162.23960.229444 5849670.120077-0 03264 碍85861062 11465.4294&4.&6542S0.2295330.240S064.7582615Q17.3853.3294&4 7077270 1002B70.1313914 711692433 3360.5变量数权重数据cleitEiQ wl H Sheet33. 启动 matlab(1) 点击 start MATLAB(2) senddata to matlab ,并对变量矩阵变量进行命名(注意:选取变量为数值,不包括各变量)主誉支肓馥牛艾占汁QJ4S42-4 0T5!-tSfcQI4UUU4冲1】-QJiAtljiQJ4 GINS-Q t1fc卫1Wd44曲逊f-11M1Q5SKuir1 1301出咋谢a55 1121 13214: 1321QrJSUI1 JWai4113624J 1142-0 LZH37Qi4 SUMSflUMTQ HKIau4 ST4T11-i Dai-4 2K1IQ114 WIFImKJ4jsnUWM*milHTI 厂306 7FMJ恻r4-1MI 4N-a 1K1V詔何 ?*MM I 3i71 n?M-恥t4 (Wfil4临曲4 :PW甲79&IKTWI Cffl丈出J JQZZd&Z5W3K14 呃耐洱4】4 mil KM WK iT程序(E;) MATLAB toolbox 卜*| 鬻琴 g&boxP鈿新建文件夹归 O17酣*鴻4愣改H甲IS轴jpl*?2 0L 7/1 16 1219品迅雪下叢.local2017/lylJ 9 J4丈9 he?P17/1/1 甘盃丈件来-常2D17/1/1J1 9 234宝底链rnaftlab2017/1/14 947Mtlab prngram?MI3-?0142017/1/15 29* VWIinatlabx)2017/1/14 923文料央rnbcJ0L7/1H4 9:25二C),m pc2017/1/14 9241. *.nnft2 017,114 9J5丈輯夷l - m(e:)B. opc2O17?l/14 9J5文14夫.孙旧L octi rm2OL7/U14 925-rn玉彌口八文ft末:jplvlI站文倏I飯當6.输入程序,得出结果 1=30;N=40;W=narjiLV Wl):尸A(:R :口4, 6):xcons-t ant=arLe e 1):心nobs E=siz&(x);T=30;N=46;W=n ormw(W1);y=A(:,3);x=A(:,4,6);xcon sta nt=on es(N*T,1);n obs K=size(x);results=ols(y,xc on sta nt x);vnames=strvcat( logcit , intercept , logp , logy);prt_reg(results,v names,1);sige=*( (no bs-K)/nobs);loglikols二-nobs/2*log(2*pi*sige)-1/(2*sige)*% The (robust)LM tests developed by ElhorstLMsarsem_panel(results,W,y,xconstant x); %(Robust) LMtests解释每一行分别表示:该面板数据的时期数为30 (T=30),该面板数据有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=n ormw(W1);y=A(:,3);x=A(:,4,6);xcon sta nt=on es(N*T,1);n obs K=size(x);results=ols(y,xc on sta nt x); vnames=strvcat( logcit , intercept , logp , logy ); prt_reg(results,vnames,1);sige=*(nobs-K)/nobs); loglikols=-nobs/2*log(2*pi*sige)-1/(2*sige)* % The (robust)LM tests developed by Elhorst LMsarsem_panel(results,W,y,xconstant x); % (Robust) LM 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,meanny,meannx,meanty,meantx=demean(y,x,N,T,mo del);results=ols(ywith,xwith);vnames=strvcat(logcit,logp,logy); % should be changedif x is changedprt_reg(results,vnames);sfe=meanny-meannx*; % including the constant termyme = y - mean(y);et=ones(T,1);error=y-kron(et,sfe)-x*;rsqr1 = error*error;rsqr2 = yme*yme;FE_rsqr2 = - rsqr1/rsqr2 %r-squared including fixed effectssige=*(nobs-K)/nobs);logliksfe=-nobs/2*log(2*pi*sige)-1/(2*sige)*LMsarsem_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,meanny,meannx,meanty,meantx=demean(y,x,N,T,mo del);results=ols(ywith,xwith);vnames=strvcat(logcit,logp,logy); % should be changedif 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*;rsqr1 = error*error;rsqr2 = yme*yme;effectsFE_rsqr2 = - rsqr1/rsqr2 %r-squared including fixed sige=*(nobs-K)/nobs);logliktfe=-nobs/2*log(2*pi*sige)-1/(2*sige)*LMsarsem_panel(results,W,ywith,xwith); % (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,mea nn x,mea nty,mea ntx二demea n( y,x,N,T,mo del);results=ols(ywith,xwith);vn ames二strvcat(logcit,logp,logy); % should be cha ngedif x is cha ngedprt_reg(results,v names)en=on es(N,1);et=on es(T,1);in tercept=mea n(y)-mea n( x)*;sfe=mea nn y-mea nn x*(e n,i ntercept);tfe=mea nty-mea ntx*(et,i ntercept);yme = y - mean( y);en t=o nes(N*T,1);error二y-kro n( tfe,e n) -kro n(et,sfe)-x*(e nt,i ntercept);rsqrl = error*error;rsqr2 = yme*yme;FE_rsqr2 = - rsqr1/rsqr2 %r-squared including fixed effects sige=*(nobs-K)/nobs);loglikstfe=-nobs/2*log(2*pi*sige)-1/(2*sige)*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);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_panel_FE(y,xconstant x,W,T,info);vnames=strvcat( logcit , intercept , logp , logy ); prt_spnew(results,vnames,1)% Print out effects estimatesspat_model=0; direct_indirect_effects_estimates(results,W,spat_model); panel_effects_sar(results,vnames,W);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_panel_FE(y,x,W,T,info);vnames=strvcat( logcit , logp , logy ); prt_spnew(results,vnames,1)% Print out effects estimatesspat_model=0; direct_indirect_effects_estimates(results,W,spat_model); panel_effects_sar(results,vnames,W);3、时点固定效应( Time period 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; % required for exact results=2;=0; % Do not print intercept and fixed effects; use =1 to turn on results=sar_panel_FE(y,x,W,T,info);vnames=strvcat( logcit , logp , logy );prt_spnew(results,vnames,1)% Print out effects estimatesspat_model=0;direct_indirect_effects_estimates(results,W,spat_model);panel_effects_sar(results,vnames,W);4、双固定效应( Spatial and time period 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; % required for exact results=3;=0; % Do not print intercept and fixed effects; use =1 to turn on results=sar_panel_FE(y,x,W,T,info);vnames=strvcat( logcit , logp , logy ); prt_spnew(results,vnames,1) % Print out effects estimates spat_model=0;direct_indirect_effects_estimates(results,W,spat_model);panel_effects_sar(results,vnames,W);三、静态面板SDM模型1、无固定效应( 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;results=sar_panel_FE(y,xconstant x wx,W,T,info);, W*logy );vnames=strvcat( logcit , intercept , logp , logy , W*logp prt_spnew(results,vnames,1)% Print out effects estimatesspat_model=1; direct_indirect_effects_estimates(results,W,spat_model); panel_effects_sdm(results,vnames,W);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,:);end xconstant=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 results=sar_panel_FE(y,x wx,W,T,info);vnames=strvcat( logcit , logp , logy , W*logp , W*logy ); prt_spnew(results,vnames,1)% Print out effects estimatesspat_model=1; direct_indirect_effects_estimates(results,W,spat_model); panel_effects_sdm(results,vnames,W);3、时点固定效应( Time period 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,:);end xconstant=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 estimates results=sar_panel_FE(y,x wx,W,T,info);vnames=strvcat( logcit , logp , logy , W*logp , W*logy );% Print out coefficient estimates prt_spnew(results,vnames,1) % Print out effects estimates spat_model=1;direct_indirect_effects_estimates(results,W,spat_model);panel_effects_sdm(results,vnames,W)4、双固定效应( Spatial and time period 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; % required for exact results=3;=0; % Do not print intercept and fixed effects; use =1 to turn on results=sar_panel_FE(y,x wx,W,T,info);vnames=strvcat( logcit , logp , logy , W*logp , W*logy ); prt_spnew(results,vnames,1)% Print out effects estimatesspat_model=1;direct_indirect_effects_estimates(results,W,spat_model);panel_effects_sdm(results,vnames,W)wald test spatial lag% Wald test for spatial Durbin model against spatial lag model btemp=;varcov=;Rafg=zeros(K,2*K+2);for k=1:KRafg(k,K+k)=1;% R(1,3)=0 and R(2,4)=0;endWald_spatial_lag=(Rafg*btemp)*inv(Rafg*varcov*Rafg)*Rafg*btemp prob_spatial_lag=1-chis_cdf (Wald_spatial_lag, K)wald test spatial error% Wald test spatial Durbin model against spatial error modelR=zeros(K,1);for k=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,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_panel_FE(y,x,W,T,info);LR_spatial_lag=-2* (LR_spatial_lag,K)LR test spatial errorresultssem=sem_panel_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);% 2=time dummiesywith,xwith,meanny,meannx,meanty,meantx=demean(y,x wx,N,T,2);=1;results=sar_panel_RE(ywith,xwith,W,T,info);prt_spnew(results,vnames,1) spat_model=1;direct_indirect_effects_estimates(results,W,spat_model);panel_effects_sdm(results,vnames,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(1,3)=0 and R(2,4)=0;endWald_spatial_lag=(Rafg*btemp)*inv(Rafg*varcov*Rafg)*Rafg*btemp prob_spatial_lag= 1-chis_cdf (Wald_spatial_lag, K)wald test spatial errorR=zeros(K,1);for k=1:K% k changed in 1, 7/12/2010R(k)=btemp(2*K+1)*btemp(k)+btemp(K+k);% 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,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_panel_RE(ywith,xwith(:,1:K),W,T,info);LR_spatial_lag=-2* (LR_spatial_lag,K)LR test spatial error resultssem=sem_panel_RE(ywith,xwith(:,1:K),W,T,info);LR_spatial_error=-2* (LR_spatial_error,K)四、静态面板SEM模型1、无固定效应( 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;results=sem_panel_FE(y,xconstant x,W,T,info);vnames=strvcat( logcit , intercept , logp , logy ); prt_spnew(results,vnames,1)% Print out effects estimatesspat_model=0; direct_indirect_effects_estimates(results,W,spat_model); panel_effects_sar(results,vnames,W);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=sem_panel_FE(y,x,W,T,info);vnames=strvcat( logcit , logp , logy ); prt_spnew(results,vnames,1)% Print out effects estimatesspat_model=0; direct_indirect_effects_estimates(results,W,spat_model); panel_effects_sar(results,vnames,W);3、时点固定效应( Time period 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,:);end xconstant=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 results=sem_panel_FE(y,x,W,T,info);vnames=strvcat( logcit , logp , logy ); prt_spnew(results,vnames,1)% Print out effects estimatesspat_model=0; direct_indirect_effects_estimates(results,W,spat_model); panel_effects_sar(results,vnames,W);4、双固定效应( Spatial and time period 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; % required for exact results=3;=0; % Do not print intercept and fixed effects; use =1 to turn on results=sem_panel_FE(y,x,W,T,info);vnames=strvcat( logcit , logp , logy );prt_spnew(results,vnames,1)% Print out effects estimatesspat_model=0;direct_indirect_effects_estimates(results,W,spat_model); panel_effects_sar(results,vnames,W);五、静态面板SDEM模型1、无固定效应( 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 wx,W,T,info);, W*logy );vnames=strvcat( logcit , intercept , logp , logy , W*logp prt_spnew(results,vnames,1)% Print out effects estimates spat_model=1;direct_indirect_effects_estimates(results,W,spat_model);panel_effects_sdm(results,vnames,W);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; % required for exact results=1;=0; % Do not print intercept and fixed effects; use =1 to turn onresults=sem_panel_FE(y,x wx,W,T,info);vnames=strvcat( logcit , logp , logy , W*logp , W*logy );prt_spnew(results,vnames,1) % Print out effects estimatesspat_model=1; direct_indirect_effects_estimates(results,W,spat_model); panel_effects_sdm(results,vnames,W);3、时点固定效应( Time period 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; % required for exact results=0; % Do not print intercept and fixed effects; use =1 to turn on % New routines to calculate effects estimatesresults=sem_panel_FE(y,x wx,W,T,info);vnames=strvcat( logcit , logp , logy , W*logp , W*logy );% Print out coefficient estimatesprt_spnew(results,vnames,1)% Print out effects estimatesspat_model=1;direct_indirect_effects_estimates(results,W,spat_model); panel_effects_sdm(results,vnames,W)4、双固定效应( Spatial and time period 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,:);end xconstant=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 on results=sem_panel_FE(y,x wx,W,T,info);vnames=strvcat( logcit , logp , logy , W*logp , W*logy );prt_spnew(results,vnames,1) % Print out effects estimates spat_model=1;direct_indirect_effects_estimates(results,W,spat_model);panel_effects_sdm(results,vnames,W)
展开阅读全文
相关资源
相关搜索

最新文档


当前位置:首页 > 商业管理 > 销售管理


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!