%
% Generate Table 1, Panel B for Top R&D Spenders
%

t0       = 1972;
t1       = 2012;
n        = 500;

tt       = t0-1949:t1-1949;
load top500rd.dat
t        = top500rd(1,tt);
gvkey    = top500rd(2:1+n,tt);
naics    = top500rd(2+n:1+2*n,tt);
sic      = top500rd(2+2*n:1+3*n,tt);
xrd      = top500rd(2+3*n:1+4*n,tt);
ii       = find(xrd<0);
xrd(ii)  = 0;
sumrd    = top500rd(2+4*n,tt);

xad      = top500rd(3+4*n:2+5*n,tt);
ii       = find(xad<-998.);
xad(ii)  = 0;
sumad    = top500rd(3+5*n,tt);

xce      = top500rd(4+5*n:3+6*n,tt);
ii       = find(xce<-998.);
xce(ii)  = 0;
sumce    = top500rd(4+6*n,tt);

at       = top500rd(5+6*n:4+7*n,tt);
ii       = find(at<-998.);
at(ii)   = 0;
sumat    = top500rd(5+7*n,tt);

sale     = top500rd(6+7*n:5+8*n,tt);
ii       = find(sale<-998.);
sale(ii) = 0;
sumsl    = top500rd(6+8*n,tt);

emp      = top500rd(7+8*n:6+9*n,tt);
ii       = find(emp<-998.);
emp(ii)  = 0;
sumem    = top500rd(7+9*n,tt);


ii = find(t==2008);
m  = 100;
m  = 500;
figure(1)
plot(t,sum(xrd(1:m,:))/sum(xrd(1:m,ii)), ...
     t,sum(xad(1:m,:))/sum(xad(1:m,ii)), ...
     t,sum(xce(1:m,:))/sum(xce(1:m,ii)))
title(['Investments of the Top ',int2str(m), ...
       ' Domestic R&D Spenders (2007=1)'])
legend('R&D','Advertising','Capital','Location','Northwest')
figure(2)
plot(t,sumrd/sumrd(ii),t,sumad/sumad(ii),t,sumce/sumce(ii))
title('Investments of All Domestic Companies (2007=1)')
legend('R&D','Advertising','Capital','Location','Northwest')


disp('    Statistics for Top 500 R&D Spenders')
disp('============================================')
disp(' Type of         % of total      % Decline  ')
disp(' Data              in 2008         2008-09  ')
disp('============================================')
disp(sprintf('   R&D             %5.1f           %5.1f    ', ...
[sum(xrd(1:m,ii))/sumrd(ii)*100,(sum(xrd(1:m,ii+1))/sum(xrd(1:m,ii))-1)*100]))
disp(sprintf('   Ads             %5.1f           %5.1f    ', ...
[sum(xad(1:m,ii))/sumad(ii)*100,(sum(xad(1:m,ii+1))/sum(xad(1:m,ii))-1)*100]))
disp(sprintf('   Capital         %5.1f           %5.1f    ', ...
[sum(xce(1:m,ii))/sumce(ii)*100,(sum(xce(1:m,ii+1))/sum(xce(1:m,ii))-1)*100]))
disp(sprintf('   Sales           %5.1f           %5.1f    ', ...
[sum(sale(1:m,ii))/sumsl(ii)*100,(sum(sale(1:m,ii+1))/sum(sale(1:m,ii))-1)*100]))
disp(sprintf('   Employees       %5.1f           %5.1f    ', ...
[sum(emp(1:m,ii))/sumem(ii)*100,(sum(emp(1:m,ii+1))/sum(emp(1:m,ii))-1)*100]))
disp('============================================')

%Note: declines for all companies:
% ([sumrd(ii+1)/sumrd(ii),sumad(ii+1)/sumad(ii),sumce(ii+1)/sumce(ii)]-1)*100

