reverse-shooting

Matlab scripts for reverse shooting
Log | Files | Refs | README

withbetw.m (4017B)


      1 % withbetw.m
      2 %
      3 %     Computes within and between variances using two methods:
      4 %
      5 % METHOD #1:
      6 %
      7 %     Estimates the within and between variance for a panel variable.  The
      8 %     between variance is the cross sectional variance of the fixed effects.
      9 %     The within variance is the average (over time) of the cross sectional
     10 %     variance in the Eit.
     11 %
     12 %          Var(yit) = Var(MUi) + Var(Eit)
     13 %
     14 %     It is easy to show that this identity will hold in sample as well
     15 %     provided we do not use any degrees of Freedom corrections in
     16 %     calculating the variances.
     17 %
     18 %     This is just ANOVA (see Larsen & Marx, p. 494ff).  For unbalanced
     19 %     panels, you have to weight the "between" fixed effects -- p.499ff.
     20 %
     21 %
     22 % METHOD #2:
     23 %
     24 %     Compute within and between variances symmetrically.  Within is the
     25 %     same as in Method #1.  Between is the time series average of the cross
     26 %     sectional variation in growth rates.  With this method, I believe
     27 %     there is no reason for the within and between to add to anything.
     28 %
     29 %     INPUTS --
     30 %          y:     (T*K)xN Matrix of Data (Stacked Vertically)
     31 %          K:     Number of variables passed in y
     32 %          vname: Variable name to be printed (optional)
     33 %          snam:  Country names (optional)
     34 %          unbal: unbal=1 ==> allow unbalanced panels.
     35 
     36 
     37 function []=withbetw(y,K,vname,snam,unbal);
     38 
     39 if exist('K')~=1; K=1; end;
     40 if exist('unbal')~=1; unbal=0; end;
     41 if exist('vname')~=1; vname=vdummy('NoName',K); end;
     42 [TK N]=size(y);
     43 T=TK/K;
     44 if unbal~=1;
     45    % Delete any country with missing data
     46    data=packr([(1:N)' y']);
     47    smpl=data(:,1);
     48    data(:,1)=[];
     49    y=data';
     50 else;
     51    % We want an unbalanced panel.  So we delete any country with any
     52    % variable missing all data.
     53    bad=zeros(K,N);
     54    for i=1:K;
     55       beg=(i-1)*T+1;
     56       fin=beg+T-1;
     57       bad(i,:)=allc(isnan(y(beg:fin,:)));
     58    end;
     59    bad=(sum(bad)~=0);
     60    y(:,bad)=[];
     61    smpl=~bad;
     62 end;
     63 
     64 [TK N]=size(y);
     65 fprintf('Observations Kept in the Sample: %5.0f\n',N);
     66 if exist('snam')==1; say(snam(smpl,:)); end;
     67 disp ' ';
     68 disp ' ';
     69 disp 'METHOD #1:  Between = Variance of Fixed Effects';
     70 disp '--------------------------------------------------------------------------';
     71 disp '                      Variance Decomposition    ';
     72 disp 'Variable   Total    Between   Share   Within   Share   B/W    FStat   PVal';  
     73 disp '--------------------------------------------------------------------------';
     74 fmt='%10.6f %10.6f %6.2f %10.6f %6.2f %7.4f %6.3f %6.3f';
     75 
     76 % Now, let's run through for each variable
     77 for i=1:K;
     78    beg=(i-1)*T+1;
     79    fin=beg+T-1;
     80    yi=y(beg:fin,:);
     81 
     82    ydev=demean(panshape(yi,0,0,unbal));
     83    vt=1/length(ydev)*ydev'*ydev;
     84    mui=demean(panshape(yi,33,0,unbal));
     85    vb=1/length(mui)*mui'*mui; 		% The 33 returns vector of means
     86    ytil=panshape(yi,1,0,unbal);
     87    tk=length(ytil);
     88    vw=1/tk*ytil'*ytil;
     89    factor=(tk-N)/(N-1);
     90    F=factor*vb/vw;
     91    pval=1-fcdf(F,N-1,tk-N);
     92    cshow(vname(i,:),[vt vb vb/vt vw vw/vt vb/vw F pval],fmt);
     93 
     94 end;
     95 disp '--------------------------------------------------------------------------';
     96 
     97 %$$$ disp ' ';
     98 %$$$ disp ' ';
     99 %$$$ disp 'METHOD #2:  Between = TS Average of Cross Sectional Variance';
    100 %$$$ disp '------------------------------------------------------------';
    101 %$$$ disp '                    Variance Decomposition    ';
    102 %$$$ disp 'Variable    Between        Within        Between/Within';  
    103 %$$$ disp '------------------------------------------------------------';
    104 %$$$ fmt='%13.6f %13.6f %13.4f';
    105 %$$$ 
    106 %$$$ % Now, let's run through for each variable
    107 %$$$ T=TK/K;
    108 %$$$ for i=1:K;
    109 %$$$    beg=(i-1)*T+1;
    110 %$$$    fin=beg+T-1;
    111 %$$$    yi=y(beg:fin,:);
    112 %$$$ 
    113 %$$$    ydev=demean(panshape(yi,0,0,unbal));
    114 %$$$    vt=1/length(ydev)*ydev'*ydev;
    115 %$$$    ytil=panshape(yi,1,0,unbal);
    116 %$$$    vw=1/length(ytil)*ytil'*ytil;
    117 %$$$    ytilb=panshape(yi',1,0,unbal);
    118 %$$$    vb=1/length(ytilb)*ytilb'*ytilb;
    119 %$$$    
    120 %$$$    cshow(vname(i,:),[vb vw vb/vw],fmt);
    121 %$$$ end;
    122 %$$$ disp '------------------------------------------------------------';
    123 
    124 
    125