clear
clc
close all
set(0,'DefaultFigureVisible','off');

years=[1850 1860 1880 1900 1910 1920 1930 1940 1950 1960 1970 1980 1990 2000]';

coeffIV20=years*0;
seIV20=years*0;
coeffIV20_me=years*0;
seIV20_me=years*0;
coeffIV20_de=years*0;
seIV20_de=years*0;

% starting 1840 the numbers are

sd_me20 =   0.0891327;
sd_de20 =   0.0104676;


for i=1:length(years)

file=strcat('../Data/Temp/Coeffs_',num2str(years(i)),'.csv');

[T]=readmatrix(file);


coeffIV20_me(i)=T(1,2)*sd_me20;
seIV20_me(i)=T(2,2)*sd_me20;

coeffIV20_de(i)=T(3,2)*sd_de20;
seIV20_de(i)=T(4,2)*sd_de20;   
    
    
end

writematrix([years coeffIV20/sd_me20 seIV20/sd_me20 coeffIV20_me/sd_me20 seIV20_me/sd_me20 coeffIV20_de/sd_de20 seIV20_de/sd_de20],'../Data/Temp/CoeffsIV20.csv')

 
color{1}=[0.00000 0.44700 0.74100];


tmp = cell(0);
tmp{1} = 'xticklabel style={/pgf/number format/1000 sep={}}';  % this is important to prevent scientific format
tmp{2} = 'yticklabel style={/pgf/number format/fixed}';  % for numbers along the tick marks
tmp{3} = 'axis lines=left';   
tmp{4} = 'scaled y ticks=false'; 

% Create a new figure for both panels
figure;

%% ----------- Top panel: Mean Exposure -----------
subplot(2,1,1);
bK  = coeffIV20_me;
seK = 1.67*seIV20_me;

s = shadedErrorBar(years,bK,seK,'lineprops','b','patchSaturation',0.1);
hold on;
plot(years,0*years,'k--'); 
ylabel('Coeff on Mean Exposure');
s.patch.FaceColor = color{1};
s.edge(1).LineStyle='none';
s.edge(2).LineStyle='none';
s.mainLine.Color = color{1};
s.mainLine.MarkerSize=8;
s.mainLine.Marker='.';
s.mainLine.MarkerEdgeColor='k';
hold off;
legend('hide');
title('A. Coefficient on Mean Exposure');

%% ----------- Bottom panel: Concentration Exposure -----------
subplot(2,1,2);
bK  = coeffIV20_de;
seK = 1.67*seIV20_de;

s = shadedErrorBar(years,bK,seK,'lineprops','b','patchSaturation',0.1);
hold on;
plot(years,0*years,'k--'); 
ylabel('Coeff on Concentration Exposure');
s.patch.FaceColor = color{1};
s.edge(1).LineStyle='none';
s.edge(2).LineStyle='none';
s.mainLine.Color = color{1};
s.mainLine.MarkerSize=8;
s.mainLine.Marker='.';
s.mainLine.MarkerEdgeColor='k';
hold off;
legend('hide');
title('B. Coefficient on Task Concentration Exposure');

set(gcf,'PaperPositionMode','auto');
exportgraphics(gcf, '../Results/FigureA2_EstimatedCoefficientsbyDecade.jpg')





%% ----------- Plot with spillover -----------

clear
clc
close all

years=[ 1910 1920 1930 1940 1950 1960 1970 1980 1990 2000]';

coeffIV20_sp=years*0;
seIV20_sp=years*0;
coeffIV20_me=years*0;
seIV20_me=years*0;
coeffIV20_de=years*0;
seIV20_de=years*0;

% post 1910
sd_me20 = 0.0480954;
sd_de20 = 0.0050343;
sd_sp20 = 0.7806554;  
% starting 1840 the numbers are


for i=1:length(years)



    
file=strcat('../Data/Temp/Coeffs_withspill_',num2str(years(i)),'.csv');

[T]=readmatrix(file);

 
 
    coeffIV20_me(i)=T(1,2) ;
    seIV20_me(i)=T(2,2) ;

    coeffIV20_de(i)=T(3,2) ;
    seIV20_de(i)=T(4,2) ;   
    
    coeffIV20_sp(i)=T(5,2) ;
    seIV20_sp(i)=T(6,2)  ;   
    
end


    

  

writematrix([years   coeffIV20_me/sd_me20/100 seIV20_me/sd_me20/100 coeffIV20_de/sd_de20/100 seIV20_de/sd_de20/100 coeffIV20_sp/sd_sp20/100 seIV20_sp/sd_sp20/100 ],'../Data/Temp/CoeffsIV20_withspill.csv')

 
%figure(1); plot(years, [coeffIV20 coeffIV20+1.65*seIV20 coeffIV20-1.65*seIV20 0*years]);

%figure(2); subplot(2,1,1); plot(years, [coeffIV20_me coeffIV20_me+1.65*seIV20_me coeffIV20_me-1.65*seIV20_me 0*years]);
%subplot(2,1,2); plot(years, [coeffIV20_de coeffIV20_de+1.65*seIV20_de coeffIV20_de-1.65*seIV20_de 0*years]);

color{1}=[0.00000 0.44700 0.74100];


tmp = cell(0);
tmp{1} = 'xticklabel style={/pgf/number format/1000 sep={}}';  % this is important to prevent scientific format
tmp{2} = 'yticklabel style={/pgf/number format/fixed}';  % for numbers along the tick marks
tmp{3} = 'axis lines=left';   
tmp{4} = 'scaled y ticks=false'; 

%% Combined three-panel figure
figure;
tiledlayout(3,1,'Padding','compact','TileSpacing','compact');

%% ----- (A) Mean Exposure -----
nexttile;
bK  = coeffIV20_me;
seK = 1.67 * seIV20_me;

s = shadedErrorBar(years,bK,seK,'lineprops','b','patchSaturation',0.1);
hold on;
plot(years,0*years,'k--');
ylabel('Coeff on Mean Exposure');
s.patch.FaceColor = color{1};
s.edge(1).LineStyle='none';
s.edge(2).LineStyle='none';
s.mainLine.Color = color{1};
s.mainLine.MarkerSize = 8;
s.mainLine.Marker = '.';
s.mainLine.MarkerEdgeColor = 'k';
hold off;
legend('hide');
title('A. Coefficient on Mean Exposure');

%% ----- (B) Task Concentration Exposure -----
nexttile;
bK  = coeffIV20_de;
seK = 1.67 * seIV20_de;

s = shadedErrorBar(years,bK,seK,'lineprops','b','patchSaturation',0.1);
hold on;
plot(years,0*years,'k--');
ylabel('Coeff on Concentration Exposure');
s.patch.FaceColor = color{1};
s.edge(1).LineStyle='none';
s.edge(2).LineStyle='none';
s.mainLine.Color = color{1};
s.mainLine.MarkerSize = 8;
s.mainLine.Marker = '.';
s.mainLine.MarkerEdgeColor = 'k';
hold off;
legend('hide');
title('B. Coefficient on Task Concentration Exposure');

%% ----- (C) Spillover Measure -----
nexttile;
bK  = coeffIV20_sp;
seK = 1.67 * seIV20_sp;

s = shadedErrorBar(years,bK,seK,'lineprops','b','patchSaturation',0.1);
hold on;
plot(years,0*years,'k--');
ylabel('Coeff on Spillover Measure');
xlabel('Year');
s.patch.FaceColor = color{1};
s.edge(1).LineStyle='none';
s.edge(2).LineStyle='none';
s.mainLine.Color = color{1};
s.mainLine.MarkerSize = 8;
s.mainLine.Marker = '.';
s.mainLine.MarkerEdgeColor = 'k';
hold off;
legend('hide');
title('C. Coefficient on Spillover Measure');

%% ----- Save neatly to PNG -----
set(gcf,'PaperPositionMode','auto');              % keep spacing as on screen
set(gcf,'Position',[100 100 700 1000]);           % figure size in pixels
exportgraphics(gcf,'../Results/FigureA3_EstimatedCoefficientsbyDecade_withSpill.png', ...
    'Resolution',300,'BackgroundColor','white');









