fancyaverage.m (1117B)
1 function avg=fancyaverage(x,years,musthave,printyearcodes); 2 % function avg=fancyaverage(x,years,musthave,printyearcodes); 3 % 4 % This procedure constructs averages of the columns of x, where the 5 % number of nonmissing observations may vary across column. 6 % 7 % Example x=pwt56 investment rates, 1970:1992 8 % avg=fancyaverage(x,(1970:1992)',(1970:1980)'); 9 % Will return the average investment rate for each country, where the 10 % average is taken over as many years as possible in the 1970:1992 11 % period. For countries without data in 1970:1980, the value will be NaN. 12 % 13 % Note, it is assumed that years(1) corresponds to x(1,:) 14 % 15 % If printyearcodes is present, then use these codes to print the years 16 % used for each observation. 17 18 yr0=years(1)-1; 19 20 badcty=isnan(mean(x(musthave-yr0,:)))'; % if cty has nan in musthave years 21 22 [T K]=size(x); 23 avg=zeros(K,1)*NaN; 24 for i=1:K; 25 if ~badcty(i); 26 goodobs=~isnan(x(:,i)); 27 avg(i)=sum(x(goodobs,i))/sum(goodobs); 28 end; 29 if exist('printyearcodes')==1; 30 fprintf([printyearcodes(i,:) ' ']); 31 say(years(goodobs)); 32 end; 33 end;