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

附件下载

所在主题:
文件名:  joe_99m——hansen.zip
资料下载链接地址: https://bbs.pinggu.org/a-1018542.html
本附件包括:
  • 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
附件大小:
737.76 KB   举报本内容
附件中是hansen的门槛回归的matlab程序和论文,
由于对matlab实在不了解,希望某位大神可以把附件中的matlab程序改写为R程序,
非常感谢!

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

非常感谢!!

这是matlab程序,方便没有装matlab的同学查看


  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 errorshet 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 errorshet 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;
复制代码




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

扫码加我 拉你入群

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

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

GMT+8, 2025-12-21 13:04