This repository has been archived on 2021-05-17. You can view files and clone it, but cannot push or open issues or pull requests.
scientificComputing/bootstrap/exercises/meandiffsignificance.m

49 lines
1.1 KiB
Matlab

%% (a) generate data:
n = 200;
mx = -40.0;
my = -40.5;
x = randn(n, 1) + mx;
y = randn(n, 1) + my;
%% (b) plot histograms:
subplot(1, 2, 1);
bmin = min([x; y]);
bmax = max([x; y]);
bins = bmin:(bmax-bmin)/20.0:bmax;
hist(x, bins, 'facecolor', 'b');
hold on
hist(y, bins, 'facecolor', 'r');
xlabel('x and y')
ylabel('counts')
hold off
% permutation test:
[md, ds, dq] = meandiffpermutation(x, y, nperm, alpha);
%% (c) difference of means:
fprintf('difference of means = %.2fmV\n', md);
%% (f) pdf of the differences:
[h, b] = hist(ds, 20);
h = h/sum(h)/(b(2)-b(1)); % normalization
%% (g) significance:
fprintf('difference of means at 5%% significance = %.2fmV\n', dq);
if md >= dq
fprintf('--> difference of means %.2fmV is significant\n', md);
else
fprintf('--> %.2fmV is not a significant difference of means\n', md);
end
%% plot:
subplot(1, 2, 2)
bar(b, h, 'facecolor', 'b');
hold on;
bar(b(b>=dq), h(b>=dq), 'facecolor', 'r');
plot([md md], [0 4], 'r', 'linewidth', 2);
xlabel('Difference of means');
ylabel('Probability density of H0');
hold off;
savefigpdf(gcf, 'meandiffsignificance.pdf', 12, 6);