matlab - calculate the 95 % interval around the mean

16,815

Solution 1

Calculate the quantile using quantile: or if you know the distribution, multiply the standard deviation with the correct quantile value.

Solution 2

I know this is an old question but for the record, here is a function called confidence_intervals() that will give any confidence intervals for a dataset and can be used with the errorbar() function in Matlab. It can also be used, given the optional argument, to find the confidence intervals with the log-normal variance.

As in your example, the code becomes:

aa = [1,2,3,2,1,3,5,3,4,8,9,7;...
      11,12,3,21,1,3,15,3,4,8,19,7;...
      21,2,3,2,1,23,5,3,34,84,9,7]';

errorbar( 1:12, mean(aa), confidence_intervals( aa, 95 ) )
Share:
16,815
KatyB
Author by

KatyB

Updated on June 04, 2022

Comments

  • KatyB
    KatyB almost 2 years

    If I have a vector of monthly-averaged values like

    aa = [1,2,3,2,1,3,5,3,4,8,9,7;...
        11,12,3,21,1,3,15,3,4,8,19,7;...
        21,2,3,2,1,23,5,3,34,84,9,7]';
    

    where each column refers to the monthly-averaged values from different locations and each row represents the month of year. I can calculate the average of all of the sites as:

    mean_a = nanmean(aa,2);
    

    and thus can plot the averages of these as:

    plot(1:12, mean_a);
    

    How would I now calculate the 95 % confidence interval around these mean values?

    Any advice would be appreciated.

    My attempt:

    Assuming a normal distribution:

    aa = [1,2,3,2,1,3,5,3,4,8,9,7;...
        11,12,3,21,1,3,15,3,4,8,19,7;...
        21,2,3,2,1,23,5,3,34,84,9,7]';
    
    mean_a = nanmean(aa,2);
    sem = (nanstd(aa')./sqrt(size(aa,2))).*1.96;
    errorbar(1:12,mean_a,sem);
    
  • patrik
    patrik almost 10 years
    Well the 95 percentile confidence interval around them mean is approximately mean +- std*1.96. so it seems so.
  • Enrico Anderlini
    Enrico Anderlini about 7 years
    Thanks for sharing!