%BUSEQ   Create summary tables for data in Survey of Current Finances,
%        with a particular focus on business equity.

%        Ellen McGrattan, 5-7-09


%------------------------------------------------------------------------------
% LOAD DATA FROM SCF AND EQUATE TO VARIABLE NAMES
%------------------------------------------------------------------------------
iload    = input('Load data? (1=yes) ');
itype    = input('High-skill includes technicians, sales, service? (1=yes) ');
labels   = ['89'; '92'; '95'; '98'; '01'; '04'];
ndata    = length(labels);
group1   = 'X14>0';
%
% High-skill includes only managerial and professional
%
if (itype~=1);
  group2   = '(X19>0  & (X5901>=13 & X7401==1) & (X6101>=13 & X7411==1))';
  group3   = '(X19>0  & (X5901>=13 & X7401==1) & ~(X6101>=13 & X7411==1))';
  group4   = '(X19>0  & ~(X5901>=13 & X7401==1) & (X6101>=13 & X7411==1))';
  group5   = '(X19>0  & ~(X5901>=13 & X7401==1) & ~(X6101>=13 & X7411==1))';
  group6   = '(X19<=0 & (X5901>=13 & X7401==1))';
  group7   = '(X19<=0 & ~(X5901>=13 & X7401==1))';
else
%
% High-skill includes technicians, sales, services
%
  group2   = ['(X19>0  & (X5901>=13 & (X7401==1 | X7401==2)) & ', ...
              '(X6101>=13 & (X7411==1 | X7411==2)))'];
  group3   = ['(X19>0  & (X5901>=13 & (X7401==1 | X7401==2)) & ', ...
              '~(X6101>=13 & (X7411==1 | X7411==2)))'];
  group4   = ['(X19>0  & ~(X5901>=13 & (X7401==1 | X7401==2)) & ', ...
              '(X6101>=13 & (X7411==1 | X7411==2)))'];
  group5   = ['(X19>0  & ~(X5901>=13 & (X7401==1 | X7401==2)) & ', ...
              '~(X6101>=13 & (X7411==1 | X7411==2)))'];
  group6   = '(X19<=0 & (X5901>=13 & (X7401==1 | X7401==2)))';
  group7   = '(X19<=0 & ~(X5901>=13 & (X7401==1 | X7401==2)))';
end;


for i=1:ndata;
  if (iload==1);
    eval(['load scf',labels(i,:),'.dat']);
  end;
  eval(['names',labels(i,:)])
  eval(['j1=find(',group1,');']);
  eval(['j2=find(',group2,');']);
  eval(['j3=find(',group3,');']);
  eval(['j4=find(',group4,');']);
  eval(['j5=find(',group5,');']);
  eval(['j6=find(',group6,');']);
  eval(['j7=find(',group7,');']);
  wgt           = X42001/5;
  SPOUSE        = (X19>0);
  SE            = (X4106==2 | X4107==3);
  WHRSRMAIN     = X4110.*X4111/52;
  WHRSSMAIN     = X4710.*X4711/52;
  WHRSROTH      = X4507.*X4508/52;
  WHRSSOTH      = X5107.*X5108/52;
  WHRSR         = WHRSRMAIN+WHRSROTH;
  WHRSS         = WHRSSMAIN+WHRSSOTH;
  WHRS          = WHRSR+WHRSS;
  AVGHRS        = (WHRSRMAIN+WHRSSMAIN+WHRSROTH+WHRSSOTH)./(1+SPOUSE);
  KGHOUSE       = max([X513,X526,X604,X614,X623,X716]')'- ...
                  max([X607,X617,X627+X631,X635,X717]')'-X1202;
  KGFARMBUS     = (max(0,X507)/10000).*KGHOUSE;
  KGHOUSE       = KGHOUSE-KGFARMBUS;
  KGORE         = (X1705/10000).*(X1706-X1709)+(X1805/10000).* ...
                  (X1806-X1809)+(X1905/10000).*(X1906-X1909);
  KGBUS         = (X3129-X3130)+(X3229-X3230)+(X3329-X3330)+ ...
                  (X3408-X3409)+(X3412-X3413)+(X3416-X3417)+ ...
                  (X3420-X3421)+(X3424-X3425)+(X3428-X3429);
  KGBUS         = KGBUS+KGFARMBUS;
  KGSTMF        = (X3918-X3920)+(X3833-X3835);
  KGTOTAL       = KGHOUSE+KGORE+KGBUS+KGSTMF;
  WAGE          = X5702;
  INCOME        = X5729;
  whrsr1(i,1)     = meanwgt(WHRSR(j1),wgt(j1));
  whrsr2(i,1)     = meanwgt(WHRSR(j2),wgt(j2));
  whrsr3(i,1)     = meanwgt(WHRSR(j3),wgt(j3));
  whrsr4(i,1)     = meanwgt(WHRSR(j4),wgt(j4));
  whrsr5(i,1)     = meanwgt(WHRSR(j5),wgt(j5));
  whrsr6(i,1)     = meanwgt(WHRSR(j6),wgt(j6));
  whrsr7(i,1)     = meanwgt(WHRSR(j7),wgt(j7));
  whrss1(i,1)     = meanwgt(WHRSS(j1),wgt(j1));
  whrss2(i,1)     = meanwgt(WHRSS(j2),wgt(j2));
  whrss3(i,1)     = meanwgt(WHRSS(j3),wgt(j3));
  whrss4(i,1)     = meanwgt(WHRSS(j4),wgt(j4));
  whrss5(i,1)     = meanwgt(WHRSS(j5),wgt(j5));
  kgbus1(i,1)     = meanwgt(KGBUS(j1),wgt(j1));
  kgbus2(i,1)     = meanwgt(KGBUS(j2),wgt(j2));
  kgbus3(i,1)     = meanwgt(KGBUS(j3),wgt(j3));
  kgbus4(i,1)     = meanwgt(KGBUS(j4),wgt(j4));
  kgbus5(i,1)     = meanwgt(KGBUS(j5),wgt(j5));
  kgbus6(i,1)     = meanwgt(KGBUS(j6),wgt(j6));
  kgbus7(i,1)     = meanwgt(KGBUS(j7),wgt(j7));
  frac1(i,1)      = sum(wgt(j1))/sum(wgt.*(1+SPOUSE))*100;
  frac2(i,1)      = sum(wgt(j2))/sum(wgt.*(1+SPOUSE))*100;
  frac3(i,1)      = sum(wgt(j3))/sum(wgt.*(1+SPOUSE))*100;
  frac4(i,1)      = sum(wgt(j4))/sum(wgt.*(1+SPOUSE))*100;
  frac5(i,1)      = sum(wgt(j5))/sum(wgt.*(1+SPOUSE))*100;
  frac6(i,1)      = sum(wgt(j6))/sum(wgt.*(1+SPOUSE))*100;
  frac7(i,1)      = sum(wgt(j7))/sum(wgt.*(1+SPOUSE))*100;
end;
whrs3 = whrsr3+whrss3;
whrs4 = whrsr4+whrss4;
yr    = [1989,1992,1995,1998,2001,2004]';

disp('RESULTS OF BUSEQ')
disp('----------------')
disp(' ')
disp('   Groups are: ')
disp(['    1. ',group1])
disp(['    2. ',group2])
disp(['    3. ',group3])
disp(['    4. ',group4])
disp(['    5. ',group5])
disp(['    6. ',group6])
disp(['    7. ',group7])
tpop1    = frac1;
tpop2    = frac2+.5*frac3+.5*frac4+frac6;
tpop3    = .5*frac3+.5*frac4+frac5+frac7;
thpc1    = (whrsr1+whrss1).*frac1/100 ;
thpc2    = (whrsr2.*frac2+whrsr3.*frac3+whrsr6.*frac6+ ...
            whrss2.*frac2+whrss4.*frac4)/100 ;
thpc3    = (whrsr4.*frac4+whrsr5.*frac5+whrsr7.*frac7+ ...
            whrss3.*frac3+whrss5.*frac5)/100 ;
tcgn1    = kgbus1.*frac1/100;
tcgn2    = (kgbus2.*frac2+kgbus3.*whrsr3.*frac3./whrs3+ ...
            kgbus6.*frac6+kgbus4.*whrss4.*frac4./whrs4)/100;
tcgn3    = (kgbus4.*whrsr4.*frac4./whrs4+kgbus5.*frac5+ ...
            kgbus7.*frac7+kgbus3.*whrss3.*frac3./whrs3)/100;
disp(' ')
disp( '   Fractions of population (col 2=% households w/ spouse present)');
disp(sprintf('       %4g%7.1f%7.1f%7.1f%7.1f%7.1f%7.1f%7.1f\n', ...
    [yr,frac1,[frac2./frac1,frac3./frac1,frac4./frac1,frac5./frac1, ...
               frac6./frac1,frac7./frac1]*100]'))
disp(' ')
disp( '   Fractions of 3 working groups');
disp(sprintf('       %4g%7.1f%7.1f%7.1f\n', ...
    [yr,[tpop1./frac1,tpop2./frac1,tpop3./frac1]*100]'))
tab  = [100,thpc1(5)/thpc1(2)*100,(thpc1(5)/thpc1(2)-1)*100, ...
           (thpc1(5)/thpc1(2)-1)*100;
        thpc2(2)/thpc1(2)*100,thpc2(5)/thpc1(2)*100, ...
        (thpc2(5)-thpc2(2))/thpc1(2)*100, ...
        (thpc2(5)-thpc2(2))/thpc2(2)*100;
        thpc3(2)/thpc1(2)*100,thpc3(5)/thpc1(2)*100, ...
        (thpc3(5)-thpc3(2))/thpc1(2)*100, ...
        (thpc3(5)-thpc3(2))/thpc3(2)*100];

disp('              Hours Per Household')
disp('---------------------------------------------')
disp('                      1992  2001   Chg  %Chg')
disp('                     ------------------------')
disp(sprintf('  All workers       %6.1f%6.1f%6.1f%6.1f ',tab(1,:)));
disp(sprintf('  Group 2 workers   %6.1f%6.1f%6.1f%6.1f ',tab(2,:)));
disp(sprintf('  Group 3 workers   %6.1f%6.1f%6.1f%6.1f ',tab(3,:)));
disp('---------------------------------------------')

figure(1),plot(yr,[thpc1-thpc1(2),thpc2-thpc2(2)],'-o')
title('Weekly Household Hours Relative to 1992','Fontsize',16)
legend('Total','Group 2')
v=axis;axis([1992,2004,v(3),v(4)])
figure(2),plot(yr,[tcgn1-tcgn1(2),tcgn2-tcgn2(2)],'-o')
title('Business Capital Gains Relative to 1992','Fontsize',16)
legend('Total','Group 2')
v=axis;axis([1992,2004,v(3),v(4)])

