Goal: Create a graph with 95% confidence intervals. Issue using patch.

16 visualizzazioni (ultimi 30 giorni)
Hello. I am trying to insert 95% confidence intrervals on a graph and keep getting errors with the patch function.
data = csvread('35hzD.csv');
x=data(:,1);
y=data(:,2);
% x = 1:100; % Create Independent Variable
% y = randn(50,100); % Create Dependent Variable ‘Experiments’ Data
N = size(y,1); % Number of ‘Experiments’ In Data Set
yMean = mean(y); % Mean Of All Experiments At Each Value Of ‘x’
ySEM = std(y)/sqrt(N); % Compute ‘Standard Error Of The Mean’ Of All Experiments At Each Value Of ‘x’
CI95 = tinv([0.025 0.975], N-1); % Calculate 95% Probability Intervals Of t-Distribution
yCI95 = bsxfun(@times, ySEM, CI95(:)); % Calculate 95% Confidence Intervals Of All Experiments At Each Value Of ‘x’
figure
plot(x, yMean,'p') % Plot Mean Of All Experiments
hold on
% plot(x, yCI95+yMean,'-r') % Plot 95% Confidence Intervals Of All Experiments
Z1 = str2double({yCI95(1,:)});
Z2 = str2double(fliplr(yCI95(2,:)));
patch([x, fliplr(x)], [Z1, Z2], 'b', 'EdgeColor','none', 'FaceAlpha',0.25)
hold off
I kept getting this error:
Error using patch
Vectors must be the same length.
So I added in the Z1 and Z2 hoping it would solve the problem, but it didn't. The workspace indicates that the vectors in patch are not the same size, but I cannot figure out where this error is being caused. Here is an image on my workspace below.
Alternatively, I have also tried using the paramci funciton, but have been unsuccessful. Thanks for the help!
pd = fitdist(x,'Normal')
ci = paramci(pd,'Alpha',.01)
  2 Commenti
Star Strider
Star Strider il 8 Apr 2022
It is not possible to open or import ‘35hzD.xls’. Excel says that it is corrupted.
the cyclist
the cyclist il 8 Apr 2022
It's actually a CSV file, not Excel. If you change the name to 35hzD.csv it will work.

Accedi per commentare.

Risposte (1)

the cyclist
the cyclist il 7 Apr 2022
It's unclear to me why you are doing the str2double and cell array manipulations on yCI95, but it works if you fix that up:
x = 1:100; % Create Independent Variable
y = randn(50,100); % Create Dependent Variable ‘Experiments’ Data
N = size(y,1); % Number of ‘Experiments’ In Data Set
yMean = mean(y); % Mean Of All Experiments At Each Value Of ‘x’
ySEM = std(y)/sqrt(N); % Compute ‘Standard Error Of The Mean’ Of All Experiments At Each Value Of ‘x’
CI95 = tinv([0.025 0.975], N-1); % Calculate 95% Probability Intervals Of t-Distribution
yCI95 = bsxfun(@times, ySEM, CI95(:)); % Calculate 95% Confidence Intervals Of All Experiments At Each Value Of ‘x’
figure
plot(x, yMean,'p') % Plot Mean Of All Experiments
hold on
% plot(x, yCI95+yMean,'-r') % Plot 95% Confidence Intervals Of All Experiments
Z1 = yCI95(1,:);
Z2 = fliplr(yCI95(2,:));
patch([x, fliplr(x)], [Z1, Z2], 'b', 'EdgeColor','none', 'FaceAlpha',0.25)
hold off
  4 Commenti
Jenna Ceraso
Jenna Ceraso il 7 Apr 2022
How can I modify the calculation of Z1 and Z2 or my equation so that the vector shpae isn't an issue? Thanks so much for your help.
the cyclist
the cyclist il 8 Apr 2022
It seems that you don't understand your own code, or your own data, or both. Where did these data and code come from?
The comments in the code (and also the random data created in the MATLAB code) suggest that the "real" data are suppose to consist of several "experiments", where each experiment measures y at many values of x.
But the data you load is just one value of y for each value of x.
I could make a guess at what to do with this situation, just to make the code finish. But, I would be more concerned about making the code accurately calculate what you intend.
Do you understand what the code is supposed to do, and what the data mean? These are things you should figure out for yourself.

Accedi per commentare.

Categorie

Scopri di più su Structures in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by