搜索
人大经济论坛 附件下载

附件下载

所在主题:
文件名:  Spat-Sym-US.xls
资料下载链接地址: https://bbs.pinggu.org/a-2407458.html
附件大小:
目前用matlab做空间计量还处于讳莫如深的境况,很多会操作的人还利用它来赚钱(诸如开培训班等),而不是把相关代码的收集和运算公开,让更多人受益。我觉得这都没有必要,我们做科学研究不是窝里争,再者很多代码是国外学者编的,所以没必要藏着掖着,而是让科学流行起来。在这之前,我公开了R和stata的相关命令和操作,都很受欢迎。基于我的理解和掌握,我决定贡献并适当讲解一下matlab代码,本次分享的代码是:空间面板数据,可能还不是很周全,抛砖引玉吧!如有不懂的地方,请到Regional Study群交流(234081931)。

  1. A=xlsread('D:\matlab\toolbox\SpatEcon_ren\Files_SLX_paper\cigarette.xls');
  2. W1=xlsread('D:\matlab\toolbox\SpatEcon_ren\Files_SLX_paper\Spat-Sym-US.xls');
  3. %
  4. % SAR, SEM, SAC, SDM, SDEM and GNS models. Every time this file is run
  5. %
  6. T=30; % number of time periods
  7. N=46; % number of regions
  8. % row-normalize W
  9. W=normw(W1); % function of LeSage
  10. y=A(:,[3]); % column number in the data matrix that corresponds to the dependent variable
  11. x=A(:,[4,6]); % column numbers in the data matrix that correspond to the independent variables
  12. for t=1:T
  13. t1=(t-1)*N+1;t2=t*N;
  14. wx(t1:t2,:)=W*x(t1:t2,:);
  15. end
  16. xconstant=ones(N*T,1);
  17. [nobs K]=size(x); %size返回的是行和列
  18. et=ones(T,1);
  19. en=ones(N,1);
  20. %
  21. % All models include spatial and time period fixed effects
  22. %
  23. % ----------------------------------------------------------------------------------------
  24. % No spatial interaction effects
  25. model=3;
  26. [ywith,xwith,meanny,meannx,meanty,meantx]=demean(y,x,N,T,model);
  27. results=ols(ywith,xwith);
  28. vnames=strvcat('logcit','logp','logy'); % should be changed if x is changed
  29. prt_reg(results,vnames);
  30. intercept=mean(y)-mean(x)*results.beta;
  31. sfe=meanny-meannx*results.beta-kron(en,intercept);
  32. tfe=meanty-meantx*results.beta-kron(et,intercept);
  33. yme = y - mean(y);
  34. ent=ones(N*T,1);
  35. error=y-kron(tfe,en)-kron(et,sfe)-x*results.beta-kron(ent,intercept);
  36. rsqr1 = error'*error;
  37. rsqr2 = yme'*yme;
  38. FE_rsqr2 = 1.0 - rsqr1/rsqr2 % r-squared including fixed effects
  39. sige=results.sige*((nobs-K)/nobs);
  40. loglik=-nobs/2*log(2*pi*sige)-1/(2*sige)*results.resid'*results.resid

  41. % ----------------------------------------------------------------------------------------
  42. % Spatial lag model
  43. info.lflag=0; % required for exact results
  44. info.model=3;
  45. info.fe=0; % Do not print intercept and fixed effects; use info.fe=1 to turn on
  46. % New routines to calculate effects estimates
  47. results=sar_panel_FE(y,x,W,T,info);
  48. vnames=strvcat('logcit','logp','logy');
  49. % Print out coefficient estimates
  50. prt_sp(results,vnames,1);
  51. % Print out effects estimates
  52. spat_model=0;
  53. direct_indirect_effects_estimates(results,W,spat_model);
  54. % panel_effects_sar(results,vnames,W);

  55. % ----------------------------------------------------------------------------------------
  56. % Spatial error model
  57. info.lflag=0; % required for exact results
  58. info.model=3;
  59. info.fe=0; % Do not print intercept and fixed effects; use info.fe=1 to turn on
  60. % New routines to calculate effects estimates
  61. results=sem_panel_FE(y,x,W,T,info);
  62. % Print out coefficient estimates
  63. prt_sp(results,vnames,1);

  64. % ----------------------------------------------------------------------------------------
  65. % Model with exogenous interaction effects
  66. model=3;
  67. [ywith,xwith,meanny,meannx,meanty,meantx]=demean(y,[x wx],N,T,model);
  68. results=ols(ywith,xwith);
  69. vnames=strvcat('logcit','logp','logy','W*logp','W*logy');
  70. prt_reg(results,vnames);
  71. intercept=mean(y)-mean([x wx])*results.beta;
  72. sfe=meanny-meannx*results.beta-kron(en,intercept);
  73. tfe=meanty-meantx*results.beta-kron(et,intercept);
  74. yme = y - mean(y);
  75. ent=ones(N*T,1);
  76. error=y-kron(tfe,en)-kron(et,sfe)-[x wx]*results.beta-kron(ent,intercept);
  77. rsqr1 = error'*error;
  78. rsqr2 = yme'*yme;
  79. FE_rsqr2 = 1.0 - rsqr1/rsqr2 % r-squared including fixed effects
  80. sige=results.sige*((nobs-K)/nobs);
  81. loglik=-nobs/2*log(2*pi*sige)-1/(2*sige)*results.resid'*results.resid
  82. % ----------------------------------------------------------------------------------------
  83. % Spatial Durbin model
  84. info.lflag=0; % required for exact results
  85. info.model=3;
  86. info.fe=0; % Do not print intercept and fixed effects; use info.fe=1 to turn on
  87. info.bc=1;
  88. % New routines to calculate effects estimates
  89. results=sar_panel_FE(y,[x wx],W,T,info);
  90. % Print out coefficient estimates
  91. prt_sp(results,vnames,1);
  92. % Print out effects estimates
  93. spat_model=1;
  94. direct_indirect_effects_estimates(results,W,spat_model);
  95. %panel_effects_sdm(results,vnames,W);

  96. % ----------------------------------------------------------------------------------------
  97. % Spatial Durbin error model
  98. info.lflag=0; % required for exact results
  99. info.model=3;
  100. info.fe=0; % Do not print intercept and fixed effects; use info.fe=1 to turn on
  101. info.bc=1;
  102. % New routines to calculate effects estimates
  103. results=sem_panel_FE(y,[x wx],W,T,info);
  104. % Print out coefficient estimates
  105. prt_sp(results,vnames,1);

  106. % ----------------------------------------------------------------------------------------
  107. % SAC / SARAR / Clifford-Ord/ Kelejian-Prucha model
  108. info.lflag=0; % required for exact results
  109. info.model=3;
  110. info.fe=0; % Do not print intercept and fixed effects; use info.fe=1 to turn on
  111. info.bc=1;
  112. results=sac_panel_FE(y,x,W,T,info);
  113. vnames=strvcat('logcit','logp','logy');
  114. prt_spnew(results,vnames,1);
  115. % Print out effects estimates
  116. spat_model=0;
  117. direct_indirect_effects_estimates(results,W,spat_model);
  118. % ----------------------------------------------------------------------------------------
  119. % Full model
  120. info.model=3;
  121. info.fe=0; % Do not print intercept and fixed effects; use info.fe=1 to turn on
  122. info.bc=1;
  123. results=sac_panel_FE(y,[x wx],W,T,info);
  124. vnames=strvcat('logcit','logp','logy','W*logp','W*logy');
  125. prt_spnew(results,vnames,1);
  126. % Print out effects estimates
  127. spat_model=1;
  128. direct_indirect_effects_estimates(results,W,spat_model);
复制代码
更多代码信息参见以下网站
Pual Elhost的主页

http://www.regroningen.nl/elhorst/software.shtml
James Lesage 个人主页
http://www.spatial-econometrics.com/
kelley pace个人网站
http://www.spatial-statistics.com/
Donald J. Lacombe个人主页
http://community.wvu.edu/~djlacombe/
Journal of Statistical Software杂志首页
http://www.jstatsoft.org/


    熟悉论坛请点击新手指南
下载说明
1、论坛支持迅雷和网际快车等p2p多线程软件下载,请在上面选择下载通道单击右健下载即可。
2、论坛会定期自动批量更新下载地址,所以请不要浪费时间盗链论坛资源,盗链地址会很快失效。
3、本站为非盈利性质的学术交流网站,鼓励和保护原创作品,拒绝未经版权人许可的上传行为。本站如接到版权人发出的合格侵权通知,将积极的采取必要措施;同时,本站也将在技术手段和能力范围内,履行版权保护的注意义务。
(如有侵权,欢迎举报)
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

GMT+8, 2026-1-9 13:22