楼主: kk22boy
3686 9

[问答] 最高悬赏500论坛币,帮忙改个matlab程序 [推广有奖]

  • 8关注
  • 95粉丝

一叶知秋

已卖:9095份资源

学科带头人

51%

还不是VIP/贵宾

-

TA的文库  其他...

计量经济与统计

SSCI、权威 论文写作及投稿经验

威望
1
论坛币
57240 个
通用积分
130.1721
学术水平
437 点
热心指数
492 点
信用等级
393 点
经验
68023 点
帖子
1586
精华
0
在线时间
2192 小时
注册时间
2005-3-10
最后登录
2025-5-25

初级热心勋章 初级信用勋章

楼主
kk22boy 发表于 2011-12-22 09:46:55 |AI写论文
100论坛币
附件中是hansen的门槛回归的matlab程序和论文,
由于对matlab实在不了解,希望某位大神可以把附件中的matlab程序改写为R程序,
非常感谢!

如果能改写为类似lm(formula,data="")可以直接修改回归方程,读取变量和数据的形式,奖励可以增至200,
如果能修改至动态面板,奖励增加到500

非常感谢!!

这是matlab程序,方便没有装matlab的同学查看
paper.pdf (286.74 KB)
joe_99m——hansen.zip (737.76 KB) 本附件包括:
  • data.m
  • data.out
  • DATA.PRG
  • graphic.tkf
  • invest.doc
  • INVEST.FMT
  • invest.txt
  • thresh.out
  • THRESH_P.asv
  • THRESH_P.m
  • THRESH_P.PRG
  • Readme.txt
  • invest.dta

  1. %%%%%%%%%%%%%%
  2. % THRESH_P.M %
  3. %%%%%%%%%%%%%%
  4. % This is a Matlab program file.
  5. % It replicates the estimation, testing and graphs reported in
  6. % "Threshold Effects in Non-Dynamic Panels:
  7. % Estimation, Testing and Inference"

  8. % For questions, please contact

  9. % Bruce E. Hansen
  10. % Department of Economics
  11. % Social Science Building
  12. % University of Wisconsin
  13. % Madison, WI 53706-1393
  14. % bhansen@ssc.wisc.edu
  15. % http://www.ssc.wisc.edu/~bhansen/


  16. % This program file loads the GAUSS dataset "invest.fmt".
  17. % It creates the output file "thresh.out"
  18. function thresh_p;
  19. global nt;
  20. global t;
  21. global n;
  22. global max_lag;
  23. global thresh;
  24. global cf;
  25. global qn;
  26. global qq1;
  27. global vgraph_;
  28. global tt;
  29. global yt;
  30. global xt;
  31. global ct;
  32. global ty;
  33. global cc;
  34. global k;
  35. global tt;
  36. global qn1;

  37. load invest.txt;
  38. t = 15;
  39. nt = length(invest(:,1));
  40. n = nt/t;

  41. i = invest(:,1);     % investment/assets                             
  42. q = invest(:,2);     % Tobin's Q                                    
  43. c = invest(:,3);     % cash-flow/assets                              
  44. d = invest(:,4);     % debt/assets              


  45. qn = 400;            % number of quantiles to examine                %
  46. conf_lev = .95;      % confidence level for threshold                %
  47. vgraph_ = 1;         % set to 1 to graph likelihood ratios           %
  48. boot_1_ = 300;       % # of replications, 0 for no bootstrap, single (300) %
  49. boot_2_ = 300;       % # of replications, 0 for no bootstrap, double (300) %
  50. boot_3_ = 300;       % # of replications, 0 for no bootstrap, triple (300) %
  51. trim_1_ = .01;       % percentage to trim before search, single      %
  52. trim_2_ = .01;       % percentage to trim before search, double      %
  53. trim_3_ = .05;       % percentage to trim before search, triple      %

  54. max_lag = 1;
  55. tt = t-max_lag;
  56. ty = n*(t-max_lag-1);

  57. y  = lag_v(i,0);  yt = tr(y);
  58. cf = lag_v(c,1);  ct = tr(cf);
  59. q1 = lag_v(q,1);
  60. d1 = lag_v(d,1);      % set to threshold variable %

  61. x=[q1,q1.^2,q1.^3,d1,q1.*d1];
  62. k=length(x(1,:));
  63. xt=zeros(length(yt(:,1)),k);
  64. j=1;
  65. while j<=k
  66.     xt(:,j)=tr(x(:,j));
  67.     j=j+1;
  68. end;


  69. thresh=d1;
  70. dd=unique(thresh);
  71. qnt1=qn*trim_1_;
  72. sq=trim_1_:(1/qn):(trim_1_+(1/qn)*(qn-2*qnt1));
  73. qq1=dd(floor(sq*length(dd(:,1))));
  74. qn1=length(qq1(:,1));
  75. cc=-2*log(1-sqrt(conf_lev));
  76. out=fopen('out.txt','wt');
  77. fprintf(out,'Number of Firms:                 %u\n',n);
  78. fprintf(out,'Number of Years used:            %u\n',tt);
  79. fprintf(out,'Total Observations:              %u\n',ty);
  80. fprintf(out,'Number of Quantiles:             %u\n',qn);
  81. fprintf(out,'Confidence Level:                %f\n',conf_lev);
  82. fprintf(out,'\n');
  83. fprintf(out,'\n');
  84. fprintf(out,'*********************************************\n');
  85. fprintf(out,'\n');
  86. fprintf(out,'\n');
  87. sse0 = sse_calc(yt,[xt,ct]);
  88. fprintf(out,'Zero Threshold Model\n');
  89. fprintf(out,'Sum of Squared Errors:           %f\n',sse0);
  90. fprintf(out,'\n');
  91. fprintf(out,'\n');
  92. fprintf(out,'*********************************************\n');
  93. fprintf(out,'\n');
  94. fprintf(out,'\n');

  95. fprintf(out,'Single Threshold Model\n');
  96. fprintf(out,'\n');
  97. fclose(out);
  98. rhat1=model(0,trim_1_,boot_1_,0);
  99. out=fopen('out.txt','a+');
  100. fprintf(out,'*********************************************\n');
  101. fprintf(out,'\n');
  102. fprintf(out,'\n');

  103. fprintf(out,'Double Threshold Model\n');
  104. fprintf(out,'Trimming Percentage:             %f\n',trim_2_);
  105. fprintf(out,'\n');
  106. fprintf(out,'First Iteration\n');
  107. fclose(out);
  108. rhat2=model(rhat1,trim_2_,boot_2_,2);
  109. out=fopen('out.txt','a+');
  110. fprintf(out,'Second Iteration\n');
  111. fclose(out);
  112. rhat1=model(rhat2,trim_2_,0,1);
  113. out=fopen('out.txt','a+');
  114. fprintf(out,'\n');
  115. fprintf(out,'\n');
  116. fprintf(out,'*********************************************\n');
  117. fprintf(out,'\n');
  118. fprintf(out,'\n');

  119. fprintf(out,'Triple Threshold Model\n');
  120. fprintf(out,'Trimming Percentage:             %f\n',trim_3_);
  121. fprintf(out,'\n');
  122. fclose(out);
  123. rhat3=model([rhat1;rhat2],trim_3_,boot_3_,3);
  124. out=fopen('out.txt','a+');
  125. fprintf(out,'\n');
  126. fprintf(out,'\n');
  127. fprintf(out,'*********************************************\n');
  128. fprintf(out,'\n');
  129. fprintf(out,'\n');
  130. fclose(out);
  131. % Functions %
  132. function r=tr(y);
  133. global n;
  134. global tt;
  135. yf=reshape(y',tt,n)';
  136. yfm=yf-mean(yf')'*ones(1,length(yf(1,:)));
  137. yfm=yfm(:,1:tt-1)';
  138. ind=0;
  139. for i=1:length(yfm(1,:))
  140.     for j=1:length(yfm(:,1))
  141.         if ind==0
  142.             r=yfm(j,i);
  143.             ind=1;
  144.         else
  145.             r=[r;yfm(j,i)];
  146.         end;
  147.     end;
  148. end;



  149. function ss=sse_calc(y,x)
  150. e=y-x*(y'/x')';
  151. ss=e'*e;

  152. function r=lag_v(x,lagn);
  153. global max_lag;
  154. global t;
  155. global n;
  156. y=reshape(x',t,n)';
  157. y=y(:,(1+max_lag-lagn):(t-lagn))';
  158. ind=0;
  159. for i=1:length(y(1,:))
  160.     for j=1:length(y(:,1))
  161.         if ind==0
  162.             r=y(j,i);
  163.             ind=1;
  164.         else
  165.             r=[r;y(j,i)];
  166.         end;
  167.     end;
  168. end;

  169. function sse=thr_sse(y,q,r);
  170. global thresh;
  171. global cf;
  172. global xt;
  173. global ct;
  174. n=length(q(:,1));
  175. sse=zeros(n,1);
  176. qi=1;
  177. while qi<=n
  178.     if r==0
  179.         rr=q(qi);
  180.     else
  181.         rr=[r;q(qi)];
  182.     end;
  183.     rr=sortrows(rr,1);
  184.     xx=[xt,ct];
  185.     j=1;   
  186.     while j<=length(rr(:,1));
  187.         d=(thresh<rr(j));
  188.         xx=[xx,tr(cf.*d)];
  189.         j=j+1;
  190.     end;
  191.     sse(qi)=sse_calc(y,xx);
  192.     qi=qi+1;
  193. end;

  194. function [rsse,rqq]=r_est(y,r,trim_);
  195. global qn;
  196. global qn1;
  197. global qq1;
  198. if max(r)'==0
  199.     qq=qq1;
  200.     rr=0;
  201. else
  202.     rr=sortrows(r,1);
  203.     i=(1:qn1)';   
  204.     nn=sum((qq1*ones(1,length(rr)))<(ones(length(qq1(:,1)),1)*(rr')));
  205.     qnt=qn*trim_;
  206.     ii=(((i*ones(1,length(nn)))<=(ones(length(i(:,1)),1)*(nn+qnt)))-((i*ones(1,length(nn)))<=(ones(length(i(:,1)),1)*(nn-qnt))))*ones(length(rr(:,1)),1);
  207.     ind=0;
  208.     for j=1:length(ii)
  209.         if ii(j)==0
  210.             if ind==0
  211.                 qq=qq1(j);
  212.                 ind=1;
  213.             else
  214.                 qq=[qq;qq1(j)];
  215.             end;
  216.         end;
  217.     end;
  218. end;
  219. sse=thr_sse(y,qq,rr);
  220. [temp,rihat]=min(sse);
  221. clear temp;
  222. rsse=sse(rihat);
  223. rqq=qq(rihat);

  224. function rhat=model(r,trim_,rep,it);
  225. global qq1;
  226. global vgraph_;
  227. global yt;
  228. global ty;
  229. global cc;
  230. global xt;
  231. global thresh;
  232. global cf;
  233. global n;
  234. global k;
  235. global ct;
  236. global tt;
  237. global qn1;
  238. global qn;
  239. if max(r)==0
  240.     qq=qq1;
  241.     rr=0;
  242. else
  243.     rr=sortrows(r,1);
  244.     i=(1:qn1)';
  245.     ind=0;
  246.     for ij=1:length(rr)
  247.         if ind==0
  248.            nn=sum(qq1<rr(ij));
  249.            ind=1;
  250.         else
  251.             nn=[nn,sum(qq1<rr(ij))];
  252.         end;
  253.     end;        
  254.     qnt=qn*trim_;
  255.     inn1=(i*ones(1,length(nn))<=(ones(length(i),1)*(nn+qnt)));
  256.     inn2=(i*ones(1,length(nn))<=(ones(length(i),1)*(nn-qnt)));
  257.     ii=(inn1-inn2)*ones(length(rr(:,1)),1);
  258.     ind=0;
  259.     for j=1:length(ii)
  260.         if ii(j)==0
  261.             if ind==0
  262.                 qq=qq1(j);
  263.                 ind=1;
  264.             else
  265.                 qq=[qq;qq1(j)];
  266.             end;
  267.         end;
  268.     end;
  269. end;
  270. sse=thr_sse(yt,qq,rr);
  271. [temp,rihat]=min(sse);
  272. clear temp;
  273. rhat=qq(rihat);
  274. sse1=sse(rihat);
  275. lr=(sse/sse1-1)*ty;
  276. ind=0;
  277. temp=(lr<cc);
  278. for j=1:length(qq(:,1))
  279.     if temp(j)==1
  280.         if ind==0
  281.             rhats=qq(j);
  282.             ind=1;
  283.         else
  284.             rhats=[rhats;qq(j)];
  285.         end;
  286.     end;
  287. end;
  288. if vgraph_==1
  289.     figure;
  290.     plot(qq,lr,qq,ones(length(qq),1)*cc);
  291.     grid on;
  292.     if it==0
  293.         title('Figure 1 LConfidence Interval Construction in Single Threshold Model');
  294.         xlabel('Threshold Parameter');
  295.     end;
  296.     if it==1
  297.         title('Figure 3 LConfidence Interval Construction in Double Threshold Model');
  298.         xlabel('First Threshold Parameter');
  299.     end;
  300.     if it==2
  301.         title('Figure 2 LConfidence Interval Construction in Double Threshold Model');
  302.         xlabel('Second Threshold Parameter');  
  303.     end;
  304.     if it==3
  305.         title('Confidence Interval Construction in Triple Threshold Model');
  306.         xlabel('Third Threshold Parameter');  
  307.     end;
  308.     ylabel('Likelihood Ratio');               
  309. end;
  310. out=fopen('out.txt','a+');     
  311. if abs(max(r)')>0
  312.     fprintf(out,'Fixed Thresholds:   \n');
  313.     for i=1:length(rr)
  314.         fprintf(out,'%f   ',rr(i));
  315.     end;
  316.     fprintf(out,'\n');
  317.     rrr=sortrows([rr;rhat],1);
  318. else
  319.     rrr=rhat;
  320. end;

  321. fprintf(out,'Threshold Estimate:              %f\n',rhat);
  322. fprintf(out,'Confidence Region:               %f   %f\n',min(rhats),max(rhats));
  323. fprintf(out,'Sum of Squared Errors:           %f\n',sse1);
  324. fprintf(out,'Trimming Percentage:             %f\n',trim_);
  325. fprintf(out,'\n');
  326. fprintf(out,'\n');

  327. nr=length(rrr(:,1));
  328. xx=xt;
  329. dd=zeros(length(thresh(:,1)),nr);
  330. j=1;
  331. while j<=nr
  332.     dd(:,j)=(thresh<rrr(j));
  333.     d=dd(:,j);
  334.     if j>1;
  335.         d=d-dd(:,j-1);
  336.     end;
  337.     xx=[xx,tr(cf.*d)];
  338.     j=j+1;
  339. end;
  340. d=1-dd(:,nr);
  341. xx=[xx,tr(cf.*d)];
  342. xxi=inv(xx'*xx);
  343. beta=xxi*(xx'*yt);
  344. e=yt-xx*beta;
  345. xxe=xx.*(e*ones(1,length(xx(1,:))));
  346. sehet=sqrt(diag(xxi*xxe'*xxe*xxi));
  347. sehomo=sqrt(diag(xxi*(e'*e))/(ty-n-length(xx(1,:))));
  348. fprintf(out,'Thresholds');
  349. for j=1:length(rrr)
  350.     fprintf(out,'%f   ',rrr(j));
  351. end;
  352. fprintf(out,'\n');
  353. fprintf(out,'\n');
  354. fprintf(out,'Regime-independent Coefficients   standard errors  het standard errors\n');
  355. for j=1:k
  356.     fprintf(out,'%f      %f      %f\n',beta(j),sehomo(j),sehet(j));
  357. end;
  358. fprintf(out,'\n');
  359. fprintf(out,'Regime- dependent Coefficients     standard errors  het standard errors\n');
  360. for j=k+1:k+nr+1
  361.     fprintf(out,'%f      %f      %f\n',beta(j),sehomo(j),sehet(j));
  362. end;
  363. fprintf(out,'\n');
  364. fprintf(out,'\n');

  365. if rep>0
  366.     xx=[xt,ct];
  367.     if abs(max(rr))>0
  368.         j=1;
  369.         while j<=length(rr(:,1))
  370.             xx=[xx,tr(cf.*(thresh<rr(j)))];
  371.             j=j+1;
  372.         end;
  373.     end;
  374.     yp=xx*(yt'/xx')';
  375.     e=yt-yp;
  376.     sse0=e'*e;
  377.     lrt=(sse0/sse1-1)*ty;
  378.     fprintf(out,'LR Test for threshold effect:    %f\n',lrt);
  379.     fprintf(out,'\n');
  380.     fprintf(out,'\n');
  381.     stats=zeros(rep,1);

  382.     j=1;
  383.     while j<=rep
  384.         eb=reshape(e',tt-1,n)';
  385.         ind=0;
  386.         y=eb(ceil(unifrnd(0,1,n,1)*n),:)';
  387.         for i=1:length(y(1,:))
  388.             for jjj=1:length(y(:,1))
  389.                 if ind==0
  390.                     rrrrr=y(jjj,i);
  391.                     ind=1;
  392.                 else
  393.                     rrrrr=[rrrrr;y(jjj,i)];
  394.                 end;
  395.             end;
  396.         end;
  397.         yb=yp+rrrrr;
  398.         clear rrrrr;
  399.         sse0=sse_calc(yb,[xt,ct]);
  400.         [sse1,rhat_b]=r_est(yb,0,trim_);
  401.         rrr=rhat_b;
  402.         if abs(max(r))>0
  403.             jj=1;
  404.             while jj<=length(r(:,1))
  405.                 sse0=sse1;
  406.                 [sse1,rhat_b]=r_est(yb,rrr,trim_);
  407.                 rrr=[rrr;rhat_b];
  408.                 jj=jj+1;
  409.             end;
  410.         end;
  411.         lrt_b=(sse0/sse1-1)*ty;
  412.         stats(j)=lrt_b;
  413.         fprintf(out,'Bootstrap Replication:     %f   %f\n',j,lrt_b);
  414.         j=j+1;
  415.     end;
  416.     fprintf(out,'\n');
  417.     fprintf(out,'\n');
  418.     stats=sortrows(stats,1);
  419.     crits=stats(ceil([.90;.95;.99]*rep));
  420.     fprintf(out,'Number of Bootstrap replications:%f\n',rep);
  421.     fprintf(out,'Bootstrap p-value :              %f\n',mean(stats>lrt));
  422.     fprintf(out,'Critical Values:                 %f\n',crits);
  423.     fprintf(out,'\n');
  424.     fprintf(out,'\n');
  425.     fclose(out);
  426. end;
复制代码


最佳答案

epoh 查看完整内容

老兄不用花钱的 这原本就有三种code Gauss,matlab,R http://www.ssc.wisc.edu/~bhansen/progs/joe_99.html joe_99r
关键词:MATLAB程序 500论坛币 MATLAB 悬赏500 matla 论坛 matlab 程序
已有 1 人评分热心指数 收起 理由
红楼结社 + 5 热心帮助其他会员

总评分: 热心指数 + 5   查看全部评分

如果该贴对您有些许帮助,希望你能回复一下或者评一下热心指数!谢谢!

沙发
epoh 发表于 2011-12-22 09:46:56
老兄不用花钱的
这原本就有三种code
Gauss,matlab,R
http://www.ssc.wisc.edu/~bhansen/progs/joe_99.html
joe_99r
    joe_99r.zip (124.09 KB) 本附件包括:
  • invest.dat
  • thresh_p.R
  • invest.doc
  • DATA.R


已有 2 人评分论坛币 学术水平 热心指数 信用等级 收起 理由
lyleconometrics + 2 + 1 + 1 + 1 精彩帖子
kk22boy + 5 + 5 + 5 呵呵,谢谢epoh兄!

总评分: 论坛币 + 2  学术水平 + 6  热心指数 + 6  信用等级 + 6   查看全部评分

藤椅
kk22boy 发表于 2011-12-22 10:13:01
epoh 发表于 2011-12-22 10:06
老兄不用花钱的
这原本就有三种code
Gauss,matlab,R
呵呵,epoh兄每次回复都那么及时,让我等真是不知如何感谢

另外,以前去hansen网站浏览过,没见过hansen放过R程序呀,呵呵,可能是自己太孤陋寡闻了~

看来R的确有成为主流计量软件的趋势了,呵呵

最后,感谢epoh兄了哈!!
如果该贴对您有些许帮助,希望你能回复一下或者评一下热心指数!谢谢!

板凳
kk22boy 发表于 2011-12-22 10:23:57
奇怪,为什么我不能设置最佳答案呢,时间不到?
如果该贴对您有些许帮助,希望你能回复一下或者评一下热心指数!谢谢!

报纸
kk22boy 发表于 2011-12-22 10:41:36
呵呵,看了下更新时间,关于R的程序都是2011年后更新的,看来hansen老师也对R有研究啦

补充一下,如果有其他同学能改成类似lm(formula,data="")等可以直接使用的函数,奖励依旧不变

可以设置出售帖,我来购买

呵呵



已有 1 人评分学术水平 热心指数 信用等级 收起 理由
epoh + 1 + 1 + 1 鼓励积极发帖讨论

总评分: 学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

如果该贴对您有些许帮助,希望你能回复一下或者评一下热心指数!谢谢!

地板
epoh 发表于 2011-12-22 10:47:51
kk22boy 发表于 2011-12-22 10:41
呵呵,看了下更新时间,关于R的程序都是2011年后更新的,看来hansen老师也对R有研究啦

补充一下,如果有 ...
补个winrats code
joe_99_rats
   joe_99_rats.rar (120.56 KB)
Panel Regression - Estimation by Fixed Effects
Dependent Variable INVA
Panel(15) of Annual Data From   1//1974:01 To    565//1987:01
Usable Observations   7910      Degrees of Freedom  7339
Total Observations   8474      Skipped/Missing      564
Centered R**2     0.410889      R Bar **2   0.365135
Uncentered R**2   0.807075      T x R**2    6383.960
Mean of Dependent Variable      0.0887200480
Std Error of Dependent Variable 0.0619148484
Standard Error of Estimate      0.0493327859
Sum of Squared Residuals        17.861098726
Regression F(570,7339)                8.9803
Significance Level of F           0.00000000
Log Likelihood                   12875.03147

   Variable                  Coeff     Std Error       T-Stat        Signif
******************************************************************
1.  Q{1}                 0.0104   8.9317e-04     11.63377  0.00000000
2.  Q2{1}         2.1291e-04   2.5598e-05     -8.31758  0.00000000
3.  Q3{1}         1.1672e-06   1.9501e-07      5.98531  0.00000000
4.  DEBTA{1}         -0.0222   4.2453e-03     -5.23262  0.00000017
5.  QD{1}         1.6349e-03   1.4222e-03      1.14954  0.25036936
6.  CFA{1}               0.0715   4.5259e-03     15.79410  0.00000000

Single Threshold       0.01582
Sum of Squared Residuals      17.78551
F Test vs 0 Threshold      33.61933
Double Threshold       0.01582       0.54180
Sum of Squared Residuals      17.72983
F Test vs 1 Threshold      24.83754
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
kk22boy + 5 + 5 + 5 谢谢epoh兄!

总评分: 学术水平 + 5  热心指数 + 5  信用等级 + 5   查看全部评分

7
tmdxyz 发表于 2011-12-23 08:05:18
epoh 发表于 2011-12-22 09:46
老兄不用花钱的
这原本就有三种code
Gauss,matlab,R
瞧这人品,刚刚地

8
剑气留形 发表于 2013-4-16 16:40:17
epoh 发表于 2011-12-22 09:46
老兄不用花钱的
这原本就有三种code
Gauss,matlab,R
不知道这位仁兄对这个程序熟么 我最近要用这个程序  但得改函数形式  身为门外汉不知从哪下手 不知能否指点一二? 非常感谢

9
加油小麦 发表于 2015-7-3 13:15:29
好赞的东西,可惜才看到,谢谢楼主

10
苏培 在职认证  发表于 2016-3-9 19:34:43
非常好的东西,留个印迹。

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

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-20 08:02