How to use stacked bar charts to draw multiple confidence intervals

9 visualizaciones (últimos 30 días)
How to use stacked bar charts to draw multiple confidence intervals as follows
My sample data is as follows: the point estimated coefficient is
coef = [-0.0186
0.0057
-0.0067
-0.0007
0
-0.0295
-0.0517
-0.0651
-0.0689
-0.0862
-0.0866
];
The lower bounds for the point estimated coefficient at 1%, 5%, 10%, 15% and 20% levels are
Lower_Bound = [
-0.061944 -0.051528 -0.04632 -0.042792 -0.040104
-0.04203 -0.03056 -0.024825 -0.02094 -0.01798
-0.05314 -0.04198 -0.0364 -0.03262 -0.02974
-0.044302 -0.033824 -0.028585 -0.025036 -0.022332
0 0 0 0 0
-0.07723 -0.06576 -0.060025 -0.05614 -0.05318
-0.103042 -0.090704 -0.084535 -0.080356 -0.077172
-0.121602 -0.108024 -0.101235 -0.096636 -0.093132
-0.132368 -0.117116 -0.10949 -0.104324 -0.100388
-0.152506 -0.136572 -0.128605 -0.123208 -0.119096
-0.183092 -0.159904 -0.14831 -0.140456 -0.134472
];
The upper bounds for the point estimated coefficient at 1%, 5%, 10%, 15% and 20% levels are
Upper_Bound = [0.024744 0.014328 0.00912 0.005592 0.002904
0.05343 0.04196 0.036225 0.03234 0.02938
0.03974 0.02858 0.023 0.01922 0.01634
0.042902 0.032424 0.027185 0.023636 0.020932
0 0 0 0 0
0.01823 0.00676 0.001025 -0.00286 -0.00582
-0.000358 -0.012696 -0.018865 -0.023044 -0.026228
-0.008598 -0.022176 -0.028965 -0.033564 -0.037068
-0.005432 -0.020684 -0.02831 -0.033476 -0.037412
-0.019894 -0.035828 -0.043795 -0.049192 -0.053304
0.009892 -0.013296 -0.02489 -0.032744 -0.038728
];
  3 comentarios
Umar
Umar el 18 de Ag. de 2024

Hi @ Hongyun,

First, I will define the coefficients, lower bounds, and upper bounds as provided in your question.

% Coefficients
coef = [-0.0186; 0.0057; -0.0067; -0.0007; 0; -0.0295; -0.0517; -0.0651;     
-0.0689; -0.0862; -0.0866];
% Lower bounds for confidence intervals
   Lower_Bound = [
    -0.061944, -0.051528, -0.04632, -0.042792, -0.040104;
    -0.04203, -0.03056, -0.024825, -0.02094, -0.01798;
    -0.05314, -0.04198, -0.0364, -0.03262, -0.02974;
    -0.044302, -0.033824, -0.028585, -0.025036, -0.022332;
    0, 0, 0, 0, 0;
    -0.07723, -0.06576, -0.060025, -0.05614, -0.05318;
    -0.103042, -0.090704, -0.084535, -0.080356, -0.077172;
    -0.121602, -0.108024, -0.101235, -0.096636, -0.093132;
    -0.132368, -0.117116, -0.10949, -0.104324, -0.100388;
    -0.152506, -0.136572, -0.128605, -0.123208, -0.119096;
    -0.183092, -0.159904, -0.14831, -0.140456, -0.134472
   ];
% Upper bounds for confidence intervals
    Upper_Bound = [
    0.024744, 0.014328, 0.00912, 0.005592, 0.002904;
    0.05343, 0.04196, 0.036225, 0.03234, 0.02938;
    0.03974, 0.02858, 0.023, 0.01922, 0.01634;
    0.042902, 0.032424, 0.027185, 0.023636, 0.020932;
    0, 0, 0, 0, 0;
    0.01823, 0.00676, 0.001025, -0.00286, -0.00582;
    -0.000358, -0.012696, -0.018865, -0.023044, -0.026228;
    -0.008598, -0.022176, -0.028965, -0.033564, -0.037068;
    -0.005432, -0.020684, -0.02831, -0.033476, -0.037412;
    -0.019894, -0.035828, -0.043795, -0.049192, -0.053304;
    0.009892, -0.013296, -0.02489, -0.032744, -0.038728
  ];

Then calculate the heights of the bars to create the stacked bar chart. The heights for the lower bounds will be the difference between the coefficients and the lower bounds, while the heights for the upper bounds will be the difference between the upper bounds and the coefficients.

% Calculate the heights for the lower and upper bounds
lower_heights = coef - Lower_Bound;
upper_heights = Upper_Bound - coef;

Once you have the heights for the lower and upper bounds, you can create the stacked bar chart using the bar function.

% Create the stacked bar chart
figure;
bar([lower_heights, upper_heights], 'stacked');
% Set the x-axis labels
set(gca, 'XTickLabel', {'1%', '5%', '10%', '15%', '20%'});
xlabel('Confidence Levels');
ylabel('Coefficient Values');
 title('Stacked Bar Chart of Coefficients with Confidence Intervals');
legend({'Lower Bound', 'Upper Bound'}, 'Location', 'Best');

Finally, you can customize the chart to enhance its readability and presentation. This includes adding grid lines, adjusting colors, and ensuring that the legend is clear.

% Customize the appearance
grid on;
   % Custom colors for lower and upper bounds
   colormap([0.8 0.2 0.2; 0.2 0.8 0.2]);

Please see attached.

Feel free to adjust the aesthetics and parameters to better fit your specific needs and preferences. If you have any further questions, please let me know.

Hongyun
Hongyun el 18 de Ag. de 2024
@Umar Hi bro, you did a good work. But the drawing results you provided are significantly different from the figures I want to get. As shown in the sample drawing, the estimated coefficient is located at the center, and the upper and lower bounds should be symmetrically distributed around the estimated coefficient. Just like this

Iniciar sesión para comentar.

Respuesta aceptada

Umar
Umar el 18 de Ag. de 2024
Editada: Umar el 18 de Ag. de 2024

Hi @ Hongyun,

So,your goal is to create a visually intuitive representation of these coefficients such that they are centered at zero on a stacked bar chart, with lower and upper bounds extending symmetrically around them. To achieve this, I need to adjust how I calculate and plot the heights of the bars in relation to the coefficients. Here’s an updated version of the code,

% Coefficients
coef = [-0.0186; 0.0057; -0.0067; -0.0007; 0; -0.0295; -0.0517; -0.0651;        
-0.0689; -0.0862; -0.0866];
% Lower bounds for confidence intervals
Lower_Bound = [
  -0.061944, -0.051528, -0.04632, -0.042792, -0.040104;
  -0.04203, -0.03056, -0.024825, -0.02094, -0.01798;
  -0.05314, -0.04198, -0.0364, -0.03262, -0.02974;
  -0.044302, -0.033824, -0.028585, -0.025036, -0.022332;
  0, 0, 0, 0, 0;
  -0.07723, -0.06576, -0.060025, -0.05614, -0.05318;
  -0.103042, -0.090704, -0.084535, -0.080356, -0.077172;
  -0.121602, -0.108024, -0.101235, -0.096636, -0.093132;
  -0.132368, -0.117116, -0.10949, -0.104324, -0.100388;
  -0.152506, -0.136572, -0.128605, -0.123208, -0.119096;
  -0.183092, -0.159904, -0.14831, -0.140456, -0.134472
];
% Upper bounds for confidence intervals
Upper_Bound = [
  0.024744, 0.014328, 0.00912, 0.005592, 0.002904;
  0.05343, 0.04196, 0.036225, 0.03234, 0.02938;
  0.03974, 0.02858, 0.023, 0.01922, 0.01634;
  0.042902, 0.032424, 0.027185, 0.023636, 0.020932;
  0, 0, 0, 0, 0;
  0.01823, 0.00676, 0.001025, -0.00286, -0.00582;
  -0.000358, -0.012696, -0.018865, -0.023044, -0.026228;
  -0.008598, -0.022176, -0.028965, -0.033564, -0.037068;
  -0.005432, -0.020684, -0.02831, -0.033476, -0.037412;
  -0.019894, -0.035828, -0.043795, -0.049192, -0.053304;
  0.009892, -0.013296, -0.02489, -0.032744, -0.038728
];
% Calculate heights for plotting
lower_heights = coef - Lower_Bound; % Height from coefficient to lower bound
upper_heights = Upper_Bound - coef; % Height from coefficient to upper 
bound
% Prepare data for stacked bar chart
data_to_plot = [Lower_Bound(:, 1), lower_heights, upper_heights];
% Create stacked bar chart
figure;
hBar = bar(data_to_plot, 'stacked');
% Set x-axis labels and other properties
set(gca, 'XTickLabel', {'1%', '5%', '10%', '15%', '20%'});
xlabel('Confidence Levels');
ylabel('Coefficient Values');
title('Stacked Bar Chart of Coefficients with Confidence Intervals');
legend({'Lower Bound', 'Coefficient', 'Upper Bound'}, 'Location', 'Best');
% Customize appearance
grid on;
colormap([0.8 0.2 0.2; 0 0 1; 0.2 0.8 0.2]); % Custom colors for each part

Please see attached.

So, after going through @dpb comments, I did concur with his comments. It was my fault not paying attention to comments.

% Coefficients
coef = [-0.0186, 0.0057, -0.0067, -0.0007, 0, -0.0295, -0.0517, 
-0.0651, ...
      -0.0689, -0.0862, -0.0866];
% Lower bounds for confidence intervals
Lower_Bound = [ -0.061944, -0.051528, -0.04632, -0.042792, 
-0.040104;
              -0.04203, -0.03056, -0.024825, -0.02094, -0.01798;
              -0.05314, -0.04198, -0.0364, -0.03262, -0.02974;
              -0.044302, -0.033824, -0.028585, -0.025036, -0.022332;
              0, 0, 0, 0, 0;
              -0.07723, -0.06576, -0.060025, -0.05614, -0.05318;
              -0.103042, -0.090704, -0.084535, -0.080356, -0.077172;
              -0.121602, -0.108024, -0.101235, -0.096636, -0.093132;
              -0.132368, -0.117116, -0.10949, -0.104324, -0.100388;
              -0.152506, -0.136572, -0.128605, -0.123208, -0.119096;
              -0.183092, -0.159904, -0.14831, -0.140456, -0.134472];
% Upper bounds for confidence intervals
Upper_Bound = [  0.024744, 0.014328, 0.00912, 0.005592, 0.002904;
               0.05343, 0.04196, 0.036225, 0.03234, 0.02938;
               0.03974, 0.02858, 0.023, 0.01922, 0.01634;
               0.042902, 0.032424, 0.027185, 0.023636, 0.020932;
               0, 0, 0, 0, 0;
               0.01823, 0.00676, 0.001025, -0.00286, -0.00582;
              -0.000358, -0.012696, -0.018865, -0.023044, -0.026228;
              -0.008598, -0.022176, -0.028965, -0.033564, -0.037068;
              -0.005432, -0.020684, -0.02831, -0.033476, -0.037412;
              -0.019894, -0.035828, -0.043795, -0.049192, -0.053304;
               0.009892, -0.013296, -0.02489, -0.032744, -0.038728];
% Reshape coef to match dimensions
coef_matrix = repmat(coef', 1, size(Lower_Bound, 2)); 
% Calculate differences for plotting
lower_heights = coef_matrix - Lower_Bound; % Height from coefficient 
to lower bound
upper_heights = Upper_Bound - coef_matrix; % Height from coefficient 
to upper bound
% Prepare data for stacked bar chart
data_to_plot = [Lower_Bound, lower_heights, upper_heights];
% Create stacked bar chart
figure;
hBar = bar(data_to_plot, 'stacked');
% Set x-axis labels and other properties
set(gca, 'XTickLabel', {'1%', '5%', '10%', '15%', '20%'});
xlabel('Confidence Levels');
ylabel('Coefficient Values');
title('Stacked Bar Chart of Coefficients with Confidence 
Intervals');
legend({'Lower Bound', 'Coefficient', 'Upper Bound'}, 'Location',   'Best');
% Customize appearance
grid on;
colormap([0.8 0.2 0.2; 0 0 1; 0.2 0.8 0.2]); % Custom colors for 
each part

Please see updated attached plot.

Hope this helps. Please let me know if you have any further questions.

  27 comentarios
Hongyun
Hongyun el 21 de Ag. de 2024
@Umar @dpb In the six questions I have raised in the past, you have been actively helping me solve them. I am truly grateful and hope that we can become good partners in the future.
Umar
Umar el 22 de Ag. de 2024
Hi @Hongyun,
Thank you for your kind words. I am pleased to hear that my assistance has been helpful in addressing the questions you raised. It is always my goal to provide support and facilitate effective communication. I share your hope for a fruitful partnership moving forward.

Iniciar sesión para comentar.

Más respuestas (1)

dpb
dpb el 18 de Ag. de 2024
Editada: dpb el 18 de Ag. de 2024
coef = [-0.0186 0.0057 -0.0067 -0.0007 0 -0.0295 -0.0517 -0.0651 -0.0689 -0.0862 -0.0866];
Lower_Bound = [ -0.061944 -0.051528 -0.04632 -0.042792 -0.040104
-0.04203 -0.03056 -0.024825 -0.02094 -0.01798
-0.05314 -0.04198 -0.0364 -0.03262 -0.02974
-0.044302 -0.033824 -0.028585 -0.025036 -0.022332
0 0 0 0 0
-0.07723 -0.06576 -0.060025 -0.05614 -0.05318
-0.103042 -0.090704 -0.084535 -0.080356 -0.077172
-0.121602 -0.108024 -0.101235 -0.096636 -0.093132
-0.132368 -0.117116 -0.10949 -0.104324 -0.100388
-0.152506 -0.136572 -0.128605 -0.123208 -0.119096
-0.183092 -0.159904 -0.14831 -0.140456 -0.134472];
Upper_Bound = [ 0.024744 0.014328 0.00912 0.005592 0.002904
0.05343 0.04196 0.036225 0.03234 0.02938
0.03974 0.02858 0.023 0.01922 0.01634
0.042902 0.032424 0.027185 0.023636 0.020932
0 0 0 0 0
0.01823 0.00676 0.001025 -0.00286 -0.00582
-0.000358 -0.012696 -0.018865 -0.023044 -0.026228
-0.008598 -0.022176 -0.028965 -0.033564 -0.037068
-0.005432 -0.020684 -0.02831 -0.033476 -0.037412
-0.019894 -0.035828 -0.043795 -0.049192 -0.053304
0.009892 -0.013296 -0.02489 -0.032744 -0.038728];
CI=[Lower_Bound Upper_Bound];
dCI=diff(CI,1,2);
M=[CI(:,1) dCI];
bar(M,'stacked')
The problem with @Umar's solution is that bar() adds up all the data by row so that all the negative values in the original add below the line and all the positives above which all end up negating each other so the total is about zero.
To fool Mother Bar(), you have to start at the bottom and let it add the differences; then you'll get the bars centered about zero as the example. The above, however, also shows that you have to order the percentages from center to end in both directions facing the signs; you'll see the tails are both at the bottom in the above with the given orientation...we'll leave that as "exercise for Student"...
ADDENDUM:
OK, had a few minutes...try this for starters --
dCL=-diff(Lower_Bound,1,2); % difference between pcts, signed lower bounds
dCU=-diff(Upper_Bound,1,2); % ditto for upper tail percentages
dZ=Upper_Bound(:,1)-Lower_Bound(:,end); % the difference from last lower to first upper %age
M=[Lower_Bound(:,1) dCL dZ dCU]; % combine differences starting with bottom tail absolute
figure
hB=bar(M,'stacked','FaceColor','flat'); % plot, prepare to change colors by colormap
m=colormap(bone(7)); m=m(2:end-1,:); % pick over range of given colormap excluding black/white
N=numel(hB)/2; % how many each half of percentages
for i=1:N % iterate over that number
j=N+i; % the remaining (upper tail)
hB(i).CData=m(i,:); % set lower tail bars
hB(j).CData=m(i,:); % and upper tail bars to match symmetrically
end
NOTA BENE: The negation of the differences -- as given in order from 0-100%, the change in magnitude is positive for the LH/lower tail and negative for the upper/RH. Hence besides the order of both tails being at the bottom of the bar sections already noted, the tail percentages are plotted on the wrong ends; negative values belonging to the RH tail are below the axis. The negation of the calculation of the differences fixes this so they will be on the correct end.
Also, since bar() works only with the cumulative sum of values above and below the zero axis value, to move up the axis from the last lower percentage one must calculate and add the difference between the last given LH point and the first RH point (dZ in above code), otherwise the tail percentages for the upper tail would just begin at the origin adding only the tail percentage differences.
Will take some futzing with colormap to get better gradation, but above shows the technique to control the individual bar colors -- each section of each bar is a separate bar() handle.
  3 comentarios
Hongyun
Hongyun el 19 de Ag. de 2024
@dpb @Umar Thank you for your answer. I really appreciate it! You are the best partners and also very top programming mentors for me.
Umar
Umar el 19 de Ag. de 2024
Hi @ Hongyun,
Thank you for your kind words. I truly appreciate your feedback and am delighted to hear that you find our partnership valuable. It is a pleasure to support you on your programming journey, and I look forward to continuing our collaboration. If there’s anything specific you would like to discuss or any further assistance you need, please do not hesitate to reach out.

Iniciar sesión para comentar.

Etiquetas

Productos


Versión

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by