[bootstrap] finished code for new exercises
This commit is contained in:
parent
c9dd1ffbe6
commit
af45ae7f9e
@ -37,7 +37,7 @@ jan.benda@uni-tuebingen.de}
|
|||||||
commentstyle=\itshape\color{darkgray},
|
commentstyle=\itshape\color{darkgray},
|
||||||
breaklines=true,
|
breaklines=true,
|
||||||
breakautoindent=true,
|
breakautoindent=true,
|
||||||
columns=flexible,
|
% columns=flexible,
|
||||||
frame=single,
|
frame=single,
|
||||||
xleftmargin=1em,
|
xleftmargin=1em,
|
||||||
xrightmargin=1em,
|
xrightmargin=1em,
|
||||||
@ -239,7 +239,7 @@ differ in their mean by means of a permutation test.
|
|||||||
\end{parts}
|
\end{parts}
|
||||||
\begin{solution}
|
\begin{solution}
|
||||||
\lstinputlisting{meandiffpermutation.m}
|
\lstinputlisting{meandiffpermutation.m}
|
||||||
%\lstinputlisting{meandiffplots.m}
|
\lstinputlisting{meandiffplot.m}
|
||||||
\lstinputlisting{meandiffsignificance.m}
|
\lstinputlisting{meandiffsignificance.m}
|
||||||
\includegraphics[width=1\textwidth]{meandiffsignificance}
|
\includegraphics[width=1\textwidth]{meandiffsignificance}
|
||||||
\end{solution}
|
\end{solution}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
function [md, ds, dq] = meandiffpermutation(x, y, nperm, alpha)
|
function [md, ds, dq] = meandiffpermutation(x, y, nperm, alpha)
|
||||||
% Permutation test for difference of means of two independent samples.
|
% Permutation test for difference of means of two independent samples.
|
||||||
%
|
%
|
||||||
% [md, ds] = meandiffpermutation(x, y, nperm, alpha);
|
% [md, ds, dq] = meandiffpermutation(x, y, nperm, alpha);
|
||||||
%
|
%
|
||||||
% Arguments:
|
% Arguments:
|
||||||
% x: vector with the samples of the x data set.
|
% x: vector with the samples of the x data set.
|
||||||
|
40
bootstrap/exercises/meandiffplot.m
Normal file
40
bootstrap/exercises/meandiffplot.m
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
function meandiffplot(x, y, md, ds, dq, k, nrows)
|
||||||
|
% Plot histogram of data sets and of null hypothesis for differences in mean.
|
||||||
|
%
|
||||||
|
% meandiffplot(x, y, md, ds, dq, k, rows);
|
||||||
|
%
|
||||||
|
% Arguments:
|
||||||
|
% x: vector with the samples of the x data set.
|
||||||
|
% y: vector with the samples of the y data set.
|
||||||
|
% md: difference of means of the two data sets.
|
||||||
|
% ds: vector containing the differences of the means of the resampled data sets
|
||||||
|
% dq: minimum difference of the means considered significant.
|
||||||
|
% k: current row for the plot panels.
|
||||||
|
% nrows: number of rows of panels in the figure.
|
||||||
|
|
||||||
|
%% (b) plot histograms:
|
||||||
|
subplot(nrows, 2, k*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
|
||||||
|
|
||||||
|
%% (f) pdf of the differences:
|
||||||
|
[h, b] = hist(ds, 20);
|
||||||
|
h = h/sum(h)/(b(2)-b(1)); % normalization
|
||||||
|
|
||||||
|
%% plot:
|
||||||
|
subplot(nrows, 2, k*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;
|
||||||
|
end
|
@ -1,48 +1,37 @@
|
|||||||
%% (a) generate data:
|
|
||||||
n = 200;
|
n = 200;
|
||||||
mx = -40.0;
|
mx = -40.0;
|
||||||
my = -40.5;
|
nperm = 1000;
|
||||||
|
alpha = 0.05;
|
||||||
|
|
||||||
|
%% (h) repeat for various means of the y-data set:
|
||||||
|
mys = [-40.1, -40.2, -40.5];
|
||||||
|
for k=1:length(mys)
|
||||||
|
|
||||||
|
%% (a) generate data:
|
||||||
|
my = mys(k);
|
||||||
x = randn(n, 1) + mx;
|
x = randn(n, 1) + mx;
|
||||||
y = randn(n, 1) + my;
|
y = randn(n, 1) + my;
|
||||||
|
|
||||||
%% (b) plot histograms:
|
%% (d), (e) permutation test:
|
||||||
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);
|
[md, ds, dq] = meandiffpermutation(x, y, nperm, alpha);
|
||||||
|
|
||||||
%% (c) difference of means:
|
%% (c) difference of means:
|
||||||
|
fprintf('\nmean x = %.1fmV, mean y = %.1fmV\n', mx, my);
|
||||||
fprintf(' difference of means = %.2fmV\n', md);
|
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:
|
%% (g) significance:
|
||||||
fprintf(' difference of means at 5%% significance = %.2fmV\n', dq);
|
fprintf(' difference of means at 5%% significance = %.2fmV\n', dq);
|
||||||
if md >= dq
|
if md >= dq
|
||||||
fprintf('--> difference of means %.2fmV is significant\n', md);
|
fprintf(' --> measured difference of means is significant\n');
|
||||||
else
|
else
|
||||||
fprintf('--> %.2fmV is not a significant difference of means\n', md);
|
fprintf(' --> measured difference of means is not significant\n');
|
||||||
|
end
|
||||||
|
|
||||||
|
subplot(length(mys), 2, k*2-1);
|
||||||
|
title(sprintf('mx=%.1fmV, my=%.1fmV', mx, my))
|
||||||
|
|
||||||
|
%% (b), (f) plot histograms of data and pdf of differences:
|
||||||
|
meandiffplot(x, y, md, ds, dq, k, length(mys));
|
||||||
end
|
end
|
||||||
|
|
||||||
%% plot:
|
savefigpdf(gcf, 'meandiffsignificance.pdf', 12, 10);
|
||||||
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);
|
|
||||||
|
Reference in New Issue
Block a user