[bootstrap] improved code
This commit is contained in:
parent
e1c6c32db0
commit
430bdfb7fd
@ -1,10 +1,10 @@
|
|||||||
nsamples = 100;
|
nsamples = 100;
|
||||||
nresamples = 1000;
|
nresamples = 1000;
|
||||||
|
|
||||||
% draw a SRS (simple random sample, "Stichprobe") from the population:
|
% draw a simple random sample ("Stichprobe") from the population:
|
||||||
x = randn( 1, nsamples );
|
x = randn(1, nsamples);
|
||||||
fprintf('%-30s %-5s %-5s %-5s\n', '', 'mean', 'stdev', 'sem' )
|
fprintf('%-30s %-5s %-5s %-5s\n', '', 'mean', 'stdev', 'sem')
|
||||||
fprintf('%30s %5.2f %5.2f %5.2f\n', 'single SRS', mean( x ), std( x ), std( x )/sqrt(nsamples) )
|
fprintf('%30s %5.2f %5.2f %5.2f\n', 'single SRS', mean(x), std(x), std(x)/sqrt(nsamples))
|
||||||
|
|
||||||
% bootstrap the mean:
|
% bootstrap the mean:
|
||||||
mus = zeros(nresamples,1); % vector for storing the means
|
mus = zeros(nresamples,1); % vector for storing the means
|
||||||
@ -13,12 +13,13 @@ for i = 1:nresamples % loop for generating the bootstraps
|
|||||||
xr = x(inx); % resample the original SRS
|
xr = x(inx); % resample the original SRS
|
||||||
mus(i) = mean(xr); % compute statistic of the resampled SRS
|
mus(i) = mean(xr); % compute statistic of the resampled SRS
|
||||||
end
|
end
|
||||||
fprintf('%30s %5.2f %5.2f -\n', 'bootstrapped distribution', mean( mus ), std( mus ) )
|
fprintf('%30s %5.2f %5.2f -\n', 'bootstrapped distribution', mean(mus), std(mus))
|
||||||
|
|
||||||
% many SRS (we can do that with the random number generator, but not in real life!):
|
% many SRS (we can do that with the random number generator,
|
||||||
|
% but not in real life!):
|
||||||
musrs = zeros(nresamples,1); % vector for the means of each SRS
|
musrs = zeros(nresamples,1); % vector for the means of each SRS
|
||||||
for i = 1:nresamples
|
for i = 1:nresamples
|
||||||
x = randn( 1, nsamples ); % draw a new SRS
|
x = randn(1, nsamples); % draw a new SRS
|
||||||
musrs(i) = mean( x ); % compute its mean
|
musrs(i) = mean(x); % compute its mean
|
||||||
end
|
end
|
||||||
fprintf('%30s %5.2f %5.2f -\n', 'sampling distribution', mean( musrs ), std( musrs ) )
|
fprintf('%30s %5.2f %5.2f -\n', 'sampling distribution', mean(musrs), std(musrs))
|
||||||
|
@ -6,7 +6,7 @@ y = randn(n, 1) + a*x;
|
|||||||
|
|
||||||
% correlation coefficient:
|
% correlation coefficient:
|
||||||
rd = corr(x, y);
|
rd = corr(x, y);
|
||||||
fprintf('correlation coefficient of data r = %.2f\n', rd );
|
fprintf('correlation coefficient of data r = %.2f\n', rd);
|
||||||
|
|
||||||
% distribution of null hypothesis by permutation:
|
% distribution of null hypothesis by permutation:
|
||||||
nperm = 1000;
|
nperm = 1000;
|
||||||
@ -16,12 +16,12 @@ for i=1:nperm
|
|||||||
yr=y(randperm(length(y))); % shuffle y
|
yr=y(randperm(length(y))); % shuffle y
|
||||||
rs(i) = corr(xr, yr);
|
rs(i) = corr(xr, yr);
|
||||||
end
|
end
|
||||||
[h,b] = hist(rs, 20 );
|
[h,b] = hist(rs, 20);
|
||||||
h = h/sum(h)/(b(2)-b(1)); % normalization
|
h = h/sum(h)/(b(2)-b(1)); % normalization
|
||||||
|
|
||||||
% significance:
|
% significance:
|
||||||
rq = quantile(rs, 0.95);
|
rq = quantile(rs, 0.95);
|
||||||
fprintf('correlation coefficient of null hypothesis at 5%% significance = %.2f\n', rq );
|
fprintf('correlation coefficient of null hypothesis at 5%% significance = %.2f\n', rq);
|
||||||
if rd >= rq
|
if rd >= rq
|
||||||
fprintf('--> correlation r=%.2f is significant\n', rd);
|
fprintf('--> correlation r=%.2f is significant\n', rd);
|
||||||
else
|
else
|
||||||
@ -32,7 +32,7 @@ end
|
|||||||
bar(b, h, 'facecolor', 'b');
|
bar(b, h, 'facecolor', 'b');
|
||||||
hold on;
|
hold on;
|
||||||
bar(b(b>=rq), h(b>=rq), 'facecolor', 'r');
|
bar(b(b>=rq), h(b>=rq), 'facecolor', 'r');
|
||||||
plot( [rd rd], [0 4], 'r', 'linewidth', 2 );
|
plot([rd rd], [0 4], 'r', 'linewidth', 2);
|
||||||
xlabel('Correlation coefficient');
|
xlabel('Correlation coefficient');
|
||||||
ylabel('Probability density of H0');
|
ylabel('Probability density of H0');
|
||||||
hold off;
|
hold off;
|
||||||
|
Reference in New Issue
Block a user