iv.m (3233B)
1 % iv [u,N,K,stdest,b,vcv,sOID]=iv(y,x,w,title,depv,indv,instv) 2 % Instrumental Variables Estimation (Works with Panel data also) 3 % 4 % y = dependent variable Nx1 5 % x = rhs variables NxK 6 % w = instruments NxJ J>=K 7 8 function [u,N,K,stdest,beta,vcv,sOID]=iv(y,x,w,title,depv,indv,instv); 9 10 sOID=[]; 11 12 Pw = w*inv(w'*w)*w'; % The projection matrix for w 13 xxinv = inv(x'*Pw*x); 14 beta = xxinv*x'*Pw*y; 15 u = y-x*beta; % Use the real x's here! 16 17 % Now, just copy the remainder from ols.m 18 19 if x(:,1) ~= 1; disp 'No constant term in regression'; end; 20 if exist('prevest')~=1; prevest=0; end; 21 if title == 0; title = 'Instrumental Variables Estimation'; end; 22 if depv == 0; depv = ' '; end; 23 24 [N K] = size(x); 25 if indv == 0; indv=vdummy('x',K); end; 26 27 28 dof = N-K-prevest; 29 sigma2=u'*u/dof; 30 stdest=sqrt(sigma2); 31 vcv =sigma2*xxinv; 32 se =sqrt(diag(vcv)); 33 tstat = beta./se; 34 ybar = 1/N*(ones(N,1)'*y); 35 rsq = 1-(u'*u)/(y'*y - N*ybar^2); 36 rbar = 1-sigma2/((y'*y - N*ybar^2)/(N-1)); 37 38 % Compute White robust standard errors 39 40 robvcv=zeros(K,K); 41 xhat=Pw*x; 42 for i=1:N 43 robvcv = robvcv + u(i)^2*xhat(i,:)'*xhat(i,:); 44 end % i 45 robvcv = (N/(dof))*xxinv*robvcv*xxinv; 46 roberr = sqrt(diag(robvcv)); 47 trob = beta./roberr; 48 49 50 % Test of OID Restrictions (See Econometrics Notes, Newey Section (end) back 51 % of page 5: Based on quadratic form for W'e/sqrt(T). 52 53 dofOID=size(w,2)-size(x,2); 54 if dofOID>0; 55 sOID=(w'*u)'*inv(sigma2*w'*w)*(w'*u); 56 pval=1-chi2cdf(sOID,dofOID); 57 end; 58 59 % Hausman-Wu test versus OLS; Problem: often VV is not invertible (pd) 60 %bols = inv(x'*x)*x'*y; 61 %q=bols-beta; 62 %VV=sigma2*(xxinv - inv(x'*x)); 63 %det(VV) 64 %sHW=q'*inv(VV)*q; 65 %pHW=1-chi2cdf(sHW,K); 66 67 disp '============================================================================='; 68 disp ' '; 69 disp ' '; 70 disp ' ------------------------------------------------------------'; 71 fprintf([' ', title, '\n']); 72 disp ' ------------------------------------------------------------'; 73 disp ' '; 74 fprintf([' NOBS: ', num2str(N),' Dependent Variable: ',depv,'\n']); 75 fprintf([' RHS Vars: ', num2str(K),'\n']); 76 fprintf([' D of F: ', num2str(dof), '\n']); 77 disp ' '; 78 fprintf([' R-Squared: ', num2str(rsq)]); 79 fprintf([' S.E.E.: ', num2str(stdest), '\n']); 80 fprintf([' RBar^2: ', num2str(rbar)]); 81 fprintf([' Residual SS: ', num2str(u'*u), '\n']); 82 disp ' '; 83 if dofOID>0; 84 fprintf(' OverID Test -- Statistic: %6.2f DoF: %4.0f Pval: %6.3f\n',[sOID dofOID pval]); 85 end; 86 %fprintf(' Hausman-Wu Test -- Stat: %6.2f DoF: %4.0f Pval: %6.3f\n',[sHW K pHW]); 87 disp ' '; 88 disp ' Standard Robust Robust'; 89 disp 'Variable Beta Error t-stat Error t-stat'; 90 disp '-------- ---- -------- ------ ------ ------'; 91 disp ' '; 92 fmt='%16.6f %12.6f %8.2f %12.6f %8.2f'; 93 results=[beta se tstat roberr trob]; 94 for i=1:K; 95 fprintf(indv(i,:)); 96 fprintf(1,fmt,results(i,:)); 97 fprintf('\n'); 98 end 99 disp ' '; 100 fprintf('Instruments: '); 101 say(instv); % List the instruments 102 disp ' '; 103 disp '============================================================================='; 104 %end; % if PRINT==0