楼主: xuelida
48680 143

[程序分享] [下载]面板数据模型的matlab程序   [推广有奖]

121
xiuxiujingzi 发表于 2014-1-7 09:28:31
怎么用呢?楼主?

122
bailu520 发表于 2014-6-20 18:08:49
[tongue]

123
浪迹商海 发表于 2014-8-20 08:29:03
楼主略搞笑啊。。。 明明是空间计量包中的一小部分程序  还硬说成是面板数据模型  我真是呵呵

124
791935570 学生认证  发表于 2014-11-11 11:06:07
xinwq 发表于 2006-3-17 20:31
谢谢,很好
顶啊啊啊啊啊啊

125
Lisrelchen 发表于 2014-11-11 12:18:18
  1. clear all;

  2. A=wk1read('cigarette.wk1',1,0);
  3. W1=wk1read('Spat-Sym-US.wk1');
  4. % Dataset downloaded from www.wiley.co.uk/baltagi/
  5. % Source:  Baltagi and Levin (1992) and Baltagi, Griffin and Xiong (2000).
  6. % Description:  Panel Data, 46 U.S. States over the period 1963-1992.

  7. % Spatial weights matrix constructed by Elhorst


  8. %
  9. % written by: J.Paul Elhorst 9/2004
  10. % University of Groningen
  11. % Department of Economics
  12. % 9700AV Groningen
  13. % the Netherlands
  14. % j.p.elhorst@eco.rug.nl
  15. %
  16. % dimensions of the problem
  17. T=30; % number of time periods
  18. N=46; % number of regions
  19. nobs=N*T;
  20. % row-normalize W
  21. W=normw(W1); % function of LeSage
  22. y=A(:,[3]); % column number in the data matrix that corresponds to the dependent variable
  23. x=A(:,[4,5,6]); % column numbers in the data matrix that correspond to the independent variables
  24. xconstant=ones(nobs,1);
  25. % ----------------------------------------------------------------------------------------
  26. % pooled model corrected for spatial autocorrelation, including intercept
  27. info.lflag=0; % required for exact results
  28. info.model=0;
  29. results=sar_panel(y,[xconstant x],W,T,info); % Elhorst
  30. vnames=strvcat('logcit','constant','logp','logpn','logy');
  31. prt_sp(results,vnames,1); % Elhorst
  32. % ----------------------------------------------------------------------------------------
  33. % spatial fixed effects + spatial autocorrelation
  34. info.lflag=0;
  35. info.model=1;
  36. results=sar_panel(y,x,W,T,info);
  37. vnames=strvcat('logcit','logp','logpn','logy');
  38. prt_sp(results,vnames,1);
  39. % ----------------------------------------------------------------------------------------
  40. % time period fixed effects + spatial autocorrelation
  41. info.lflag=0;
  42. info.model=2;
  43. results=sar_panel(y,x,W,T,info);
  44. vnames=strvcat('logcit','logp','logpn','logy');
  45. prt_sp(results,vnames,1);
  46. % ----------------------------------------------------------------------------------------
  47. % spatial and time period fixed effects + spatial autocorrelation
  48. info.lflag=0;
  49. info.model=3;
  50. results=sar_panel(y,x,W,T,info);
  51. vnames=strvcat('logcit','logp','logpn','logy');
  52. prt_sp(results,vnames,1);
复制代码


126
Lisrelchen 发表于 2014-11-11 12:19:58
  1. clear all;

  2. A=wk1read('cigarette.wk1',1,0);
  3. W1=wk1read('Spat-Sym-US.wk1');
  4. % Dataset downloaded from www.wiley.co.uk/baltagi/
  5. % Spatial weights matrix constructed by Elhorst
  6. %
  7. % written by: J.Paul Elhorst 9/2004
  8. % University of Groningen
  9. % Department of Economics
  10. % 9700AV Groningen
  11. % the Netherlands
  12. % j.p.elhorst@eco.rug.nl
  13. %
  14. % dimensions of the problem
  15. T=30; % number of time periods
  16. N=46; % number of regions
  17. nobs=N*T;
  18. % row-normalize W
  19. W=normw(W1); % function of LeSage
  20. y=A(:,[3]); % column number in the data matrix that corresponds to the dependent variable
  21. x=A(:,[4,5,6]); % column numbers in the data matrix that correspond to the independent variables
  22. xconstant=ones(nobs,1);
  23. % ----------------------------------------------------------------------------------------
  24. % pooled model corrected for spatial autocorrelation, including intercept
  25. info.lflag=0; % required for exact results
  26. info.model=0;
  27. results=sem_panel(y,[xconstant x],W,T,info); % Elhorst
  28. vnames=strvcat('logcit','constant','logp','logpn','logy');
  29. prt_sp(results,vnames,1); % Elhorst
  30. % ----------------------------------------------------------------------------------------
  31. % spatial fixed effects + spatial autocorrelation
  32. info.lflag=0;
  33. info.model=1;
  34. results=sem_panel(y,x,W,T,info);
  35. vnames=strvcat('logcit','logp','logpn','logy');
  36. prt_sp(results,vnames,1);
  37. % ----------------------------------------------------------------------------------------
  38. % time period fixed effects + spatial autocorrelation
  39. info.lflag=0;
  40. info.model=2;
  41. results=sem_panel(y,x,W,T,info);
  42. vnames=strvcat('logcit','logp','logpn','logy');
  43. prt_sp(results,vnames,1);
  44. % ----------------------------------------------------------------------------------------
  45. % spatial and time period fixed effects + spatial autocorrelation
  46. info.lflag=0;
  47. info.model=3;
  48. results=sem_panel(y,x,W,T,info);
  49. vnames=strvcat('logcit','logp','logpn','logy');
  50. prt_sp(results,vnames,1);
复制代码


127
Lisrelchen 发表于 2014-11-11 12:21:08
  1. function llike = f_sarpanel(rho,detval,epe0,eped,epe0d,n,T)
  2. % PURPOSE: evaluates concentrated log-likelihood for the
  3. %  spatial panel autoregressive model using sparse matrix algorithms
  4. % ---------------------------------------------------
  5. %  USAGE:llike = f_sar(rho,detval,epe0,eped,epe0d,n,T)
  6. %  where: rho  = spatial autoregressive parameter
  7. %         detval = a matrix with vectorized log-determinant information
  8. %         epe0   = see below
  9. %         eped   = see below
  10. %         eoe0d  = see below
  11. %         n      = # of obs
  12. %         T      = number of time points
  13. %          b0 = AI*xs'*ys;
  14. %          bd = AI*xs'*Wys;
  15. %          e0 = ys - xs*b0;
  16. %          ed = Wys - xs*bd;
  17. %          epe0 = e0'*e0;
  18. %          eped = ed'*ed;
  19. %          epe0d = ed'*e0;
  20. % ---------------------------------------------------
  21. %  RETURNS: a  scalar equal to minus the log-likelihood
  22. %           function value at the parameter rho
  23. % ---------------------------------------------------

  24. % written by: James P. LeSage 1/2000
  25. % University of Toledo
  26. % Department of Economics
  27. % Toledo, OH 43606
  28. % jlesage@spatial-econometrics.com

  29. % partly rewritten by J.P. Elhorst 9/2004 to account for spatial panels
  30. % "Specification and Estimation of Spatial Panel Data Models",
  31. % International Regional Science Review, Vol. 26, pp. 244-268.

  32. if nargin == 7
  33. gsize = detval(2,1) - detval(1,1);
  34. % Note these are actually log detvalues
  35. i1 = find(detval(:,1) <= rho + gsize);
  36. i2 = find(detval(:,1) <= rho - gsize);
  37. i1 = max(i1);
  38. i2 = max(i2);
  39. index = round((i1+i2)/2);
  40. if isempty(index)
  41. index = 1;
  42. end;

  43. detm = detval(index,2);

  44. z = epe0 - 2*rho*epe0d + rho*rho*eped;

  45. llike = (n/2)*log(z) - T*detm;

  46. else

  47. error('f_sar: Wrong # of input arguments');

  48. end;
复制代码


128
Lisrelchen 发表于 2014-11-11 12:23:13
  1. function lik = f_sempanel(rho,eD,W,detval,T)
  2. % PURPOSE: evaluates concentrated log-likelihood for the
  3. %  spatial panel error model using sparse matrix algorithms
  4. % ---------------------------------------------------
  5. %  USAGE:llike = f_sem(rho,eD,W,detm,T)
  6. %  where: rho  = spatial error parameter
  7. %         eD   = begls residuals
  8. %         W    = spatial weight matrix
  9. %         detm =  matrix with [rho log determinant] values
  10. %                computed in sem_panel.m using one of
  11. %                Kelley Pace's routines
  12. %         T    = number of time points
  13. % ---------------------------------------------------
  14. %  RETURNS: a  scalar equal to minus the log-likelihood
  15. %           function value at the parameter rho
  16. % ---------------------------------------------------

  17. % written by: James P. LeSage 1/2000
  18. % University of Toledo
  19. % Department of Economics
  20. % Toledo, OH 43606
  21. % jlesage@spatial-econometrics.com

  22. % partly rewritten by J.P. Elhorst 4/2004 to account for spatial panels
  23. % "Specification and Estimation of Spatial Panel Data Models",
  24. % International Regional Science Review, Vol. 26, pp. 244-268.

  25. n = length(W);
  26. gsize = detval(2,1) - detval(1,1);
  27. i1 = find(detval(:,1) <= rho + gsize);
  28. i2 = find(detval(:,1) <= rho - gsize);
  29. i1 = max(i1);
  30. i2 = max(i2);
  31. index = round((i1+i2)/2);
  32. if isempty(index)
  33. index = 1;
  34. end;
  35. detm = detval(index,2);
  36. B = speye(n) - rho*sparse(W);
  37. Be=zeros(n*T,1);
  38. for t=1:T
  39.     t1=1+(t-1)*n;t2=t*n;
  40.     Be([t1:t2],1)= B*eD([t1:t2],1);
  41. end
  42. epe = Be'*Be;
  43. %lik = (n*T/2)*log(2*pi*epe) - T*detm;
  44. lik = (n*T/2)*log(epe) - T*detm;
复制代码


129
Lisrelchen 发表于 2014-11-11 12:24:21
  1. function llike = f2_sarpanel(parm,y,x,W,detval,T)
  2. % PURPOSE: evaluates log-likelihood -- given ML estimates
  3. %  spatial panel autoregressive model using sparse matrix algorithms
  4. % ---------------------------------------------------
  5. %  USAGE:llike = f2_sar(parm,y,X,W,ldet,T)
  6. %  where: parm = vector of maximum likelihood parameters
  7. %                parm(1:k-2,1) = b, parm(k-1,1) = rho, parm(k,1) = sige
  8. %         y    = dependent variable vector (n x 1)
  9. %         X    = explanatory variables matrix (n x k)
  10. %         W    = spatial weight matrix
  11. %         ldet = matrix with [rho log determinant] values
  12. %                computed in sar.m using one of Kelley Pace's routines  
  13. %         T    = number of time points
  14. % ---------------------------------------------------
  15. %  RETURNS: a  scalar equal to minus the log-likelihood
  16. %           function value at the ML parameters
  17. % ---------------------------------------------------

  18. % written by: James P. LeSage 1/2000
  19. % University of Toledo
  20. % Department of Economics
  21. % Toledo, OH 43606
  22. % jlesage@spatial.econometrics.com

  23. % partly rewritten by J.P. Elhorst 9/2004 to account for spatial panels
  24. % "Specification and Estimation of Spatial Panel Data Models",
  25. % International Regional Science Review, Vol. 26, pp. 244-268.

  26. n = length(y);
  27. k = length(parm);
  28. b = parm(1:k-2,1);
  29. rho = parm(k-1,1);
  30. sige = parm(k,1);
  31. n1=n/T;

  32. gsize = detval(2,1) - detval(1,1);
  33. i1 = find(detval(:,1) <= rho + gsize);
  34. i2 = find(detval(:,1) <= rho - gsize);
  35. i1 = max(i1);
  36. i2 = max(i2);
  37. index = round((i1+i2)/2);
  38. if isempty(index)
  39. index = 1;
  40. end;
  41. detm = detval(index,2);

  42. e=y-x*b;
  43. for t=1:T
  44.     t1=1+(t-1)*n1;t2=t*n1;
  45.     e([t1:t2],1)= e([t1:t2],1)-rho*sparse(W)*y([t1:t2],1);
  46. end

  47. epe = e'*e;
  48. tmp2 = 1/(2*sige);
  49. llike = -(n/2)*log(2*pi*sige) + T*detm - tmp2*epe;
复制代码


130
seanlee91 发表于 2015-1-2 15:42:25
感谢楼主分享

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-1 19:36