Ask an expert. Trust the answer.

Your academic and career questions answered by verified experts

How to compute confidence intervals and plot them on a bar plot

Date: 2022-12-01 12:08:04

How can I plot a bar out of a

data = 1x10 cell , where each value in the cell has a different dimension like 3x100, 3x40, 66x2 etc.

My goal is to get a bar plot, where I would have 10 group of bars and in every group three bars for each of the values. On the bar, I want it to be shown the median of the values, and I want to calculate the confidence interval and show it additionally. 

 

On this example there are not group of bars, but my point is to show you how I want the confidence intervals shown. On the site, where I found this example they offer a solution where they have this command line

e1 = errorbar(mean(data), ci95);

but I have the problem that it can't find any ci95

So, are there any other effective ways to do it, without installing or downloading additional services? 

Expert Answer:

s: 

I've found Patrick Happel's answer to not work because the figure window (and therefore the variable b) gets cleared out by subsequent calls to errorbar. Simply adding a hold on command takes care of this. To avoid confusion, here's a new answer that reproduces all of Patrick's original code, plus my small tweak: 

 

%% Old answer %Just to be safe, let's clear everything clear all data = cell(1,10); % Random length of the data l = randi(500, 10, 1) + 50; % Random "width" of the data, with 3 more likely w = randi(4, 10, 1); w(w==4) = 3; % random "direction" of the data d = randi(2, 10, 1); % sigma of the data (in fraction of mean) sigma = rand(10,1) / 3; % means of the data dmean = randi(150,10,1); dsigma = dmean.*sigma; for c = 1 : 10 if d(c) == 1 data{c} = randn(l(c), w(c)) .* dsigma(c) + dmean(c); else data{c} = randn(w(c), l(c)) .* dsigma(c) + dmean(c); end end %============================================ %Next thing is % On the bar, I want it to be shown the median of the values, and I % want to calculate the confidence interval and show it additionally. % %Are you really sure you want to plot the median? The median of some data %is not connected to the variance of the data, and hus no type of error %bars are required. I guess you want to show the mean. If you really want %to show the median, a box plot might be a better alternative. % %The following code computes and plots the mean in a bar plot: %============================================ means = zeros(numel(data),3); stds = zeros(numel(data),3); n = zeros(numel(data),3); for c = 1:numel(data) d = data{c}; if size(d,1) < size(d,2) d = d'; end cols = size(d,2); means(c, 1:cols) = nanmean(d); stds(c, 1:cols) = nanstd(d); n(c, 1:cols) = sum(~isnan((d))); end b = bar(means); %% New code %This ensures that b continues to reference existing data in the next for %loop, as the graphics objects can otherwise be deleted. hold on %% Continuing Patrick Happel's answer %============================================ %Now, we need to compute the length of the error bars. Typical choices are %the standard deviation of the data (already computed by the code above, %stored in stds), the standard error or the 95% confidence interval (which %is the 1.96fold of the standard error, assuming the underlying data %follows a normal distribution). %============================================ % for standard deviation use stds % for standard error ste = stds./sqrt(n); % for 95% confidence interval ci95 = 1.96 * ste; %============================================ %Last thing is to plot the error bars. Here I chose the ci95 as you asked %in your question, if you want to change that, simply change the variable %in the call to errorbar: %============================================ for c = 1:3 size(means(:, c)) size(b(c).XData) e = errorbar(b(c).XData + b(c).XOffset, means(:,c), ci95(:, c)); e.LineStyle = 'none'; end

 

Why Matlabhelpers ?

Looking for reliable MATLAB assignment help? Our expert MATLAB tutors deliver high-quality, easy-to-understand solutions tailored to your academic needs. Whether you're studying at Monash University, the University of Sydney, UNSW, or the University of Melbourne, we provide trusted MATLAB assistance to help you excel. Contact us today for the best MATLAB solutions online and achieve academic success!

MATLAB Assignment Help Services

Personalized Tutoring: Get one-on-one guidance from our MATLAB experts. Whether you're tackling basic concepts or advanced algorithms, we provide clear, step-by-step explanations to help you master MATLAB with confidence.

Assignment Assistance: Struggling with tight deadlines or complex assignments? Our team offers end-to-end support, from problem analysis to code development and debugging, ensuring your assignments meet the highest academic standards.

Project Development: Need expert help with your MATLAB research project? We assist in designing and implementing robust solutions, covering project planning, data collection, coding, simulation, and result analysis.

Coursework Support: Enhance your understanding of MATLAB with our comprehensive coursework assistance. We help you grasp lecture concepts, complete lab exercises, and prepare effectively for exams.

Thesis and Dissertation Guidance: Incorporate MATLAB seamlessly into your thesis or dissertation. Our experts provide support for data analysis, modeling, and simulation, ensuring your research is methodologically sound and impactful.

Contact us on WhatsApp for MATLAB help

Contact us on Telegram for MATLAB solutions