I have a line of experimental data and need to plot confidence interval

13 views (last 30 days)
I have a singular line and need to plot a 95% confidence interval, but where I plot it I get horizontal upper and lower bounds where as the line of best fit would clearly have a positive gradient.
I can't release the raw data so I have made an arbitrary line for this, but my code is below. I haven't really done statistics before and I am just helping out a friend so please no judgement and explanations on where I am wrong and how to fix very much appreciated
x = [1:1:100]
y = x
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’
plot(x, ones(size(x)).*yMean,'k') %Plot Mean Of All Experiments
hold on
plot(x, (ones(size(x)).*yCI95(1)+yMean),'m')
hold on
plot(x, (ones(size(x)).*yCI95(2)+yMean),'m')

Answers (2)

Torsten
Torsten on 6 Dec 2022
N = size(y,2);
instead of
N = size(y,1);
  1 Comment
Katherine
Katherine on 6 Dec 2022
Aha I see that i think my experimental data is transposed so it is the correct way of doing it. Inmy orginal matlab I have transposed the data an dused your N, to get an N of the rows (669) and this still gives me a horizontal answer

Sign in to comment.


Star Strider
Star Strider on 6 Dec 2022
I recognise my code (although it’s likely fairly old).
This calculates the plots the confidence intervals for a matrix of experiments where each column is a different experiment (although I’d have to see the entire code to be certain). It’s not intended to do curve-fitting.
If you’re doing a regression and want the confidence intervals on the line, use fitlm (or fitnlm) and then the appropriate predict function to return them. There are also other options in the Statistics and Machine Learning Toolbox.

Community Treasure Hunt

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

Start Hunting!