4
votes

I have a set of curves varying over time, that are stored in a MATLAB matrix. Each row of the matrix is one of those curves, unfolding over time. Those are repetitions of a random experiment.

I need to plot the mean of these curves over time, along with the 95% confidence intervals.

My understanding of statistics this is rather poor, but I was suggested to use bootstrap confidence intervals using MATLAB's bootci function.

I implemented a minimal example in MATLAB, but I have some doubts. I hope you can help me gaining a better grasp on this and avoiding dumb mistakes.

Here's the example:

NVARIABLES = 200;
NOBSERVATIONS = 1000;
RESAMPLING = 10000;

DATA = rand(NOBSERVATIONS, NVARIABLES);

[CI, STAT] = bootci(RESAMPLING, @mean, DATA);

MEAN = mean(DATA); % <------- [1]

x = 1:NVARIABLES;

figure;
hold on;
plot(x, MEAN, 'LineWidth', 2);
plot(x, CI(1,:), '--', 'LineWidth', 2); % [2]
plot(x, CI(2,:), '--', 'LineWidth', 2);
% plot(x, MEAN-CI(1,:)); % ?
% plot(x, MEAN+CI(2,:)); % ?
hold off;

Here are my questions:

  • Am I using the function properly?
  • When reporting/plotting the mean, is it correct to plot mean(DATA) (see line 1) or I should plot a mean derived by the bootstrapping procedure? I saw that STAT contains the mean for each bootstrap example, but I don't know whether I should use this information, and how
  • Is it correct to plot the confidence intervals the way I am doing (see line [2]), or I should plot MEAN-CI(1,:) and MEAN+CI(2,:)?

Please find attached the plot generated by the code.

enter image description here

1

1 Answers

3
votes

I can answer Q1 and Q3.

Q1. The first argument needs to be the number of bootstrap samples used in the computation, the second, a function that returns the statistic for which you wish to find the confidence intervals, and the third is the dataset itself that you want to give as input to the function. You will have to ensure if the first argument is correct for you, the rest seems correct.

Q3. What you've done is right - CI gives the range, and not the variation from the mean. There's also another way to plot it, which might be better in certain scenarios, or just based on personal preferences. plot_ci is a function that lets you plot confidence intervals and shows clean patches for these intervals on the same plot. The plots look like this (this is a sample plot, not based on the dataset in the question): Confidence Interval plot

Here's the command:

plot_ci(x,[MEAN,CI(1,:),CI(2,:)],'PatchColor', 'k', 'PatchAlpha', 0.1, 'MainLineWidth', 2, 'MainLineStyle', '-', 'MainLineColor', 'b','LineWidth', 1.5, 'LineStyle','--', 'LineColor', 'k');