[bootstrap] improved code
This commit is contained in:
parent
e1c6c32db0
commit
430bdfb7fd
@ -1,10 +1,10 @@
|
||||
nsamples = 100;
|
||||
nresamples = 1000;
|
||||
|
||||
% draw a SRS (simple random sample, "Stichprobe") from the population:
|
||||
x = randn( 1, nsamples );
|
||||
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) )
|
||||
% draw a simple random sample ("Stichprobe") from the population:
|
||||
x = randn(1, nsamples);
|
||||
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))
|
||||
|
||||
% bootstrap the mean:
|
||||
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
|
||||
mus(i) = mean(xr); % compute statistic of the resampled SRS
|
||||
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
|
||||
for i = 1:nresamples
|
||||
x = randn( 1, nsamples ); % draw a new SRS
|
||||
musrs(i) = mean( x ); % compute its mean
|
||||
x = randn(1, nsamples); % draw a new SRS
|
||||
musrs(i) = mean(x); % compute its mean
|
||||
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:
|
||||
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:
|
||||
nperm = 1000;
|
||||
@ -16,12 +16,12 @@ for i=1:nperm
|
||||
yr=y(randperm(length(y))); % shuffle y
|
||||
rs(i) = corr(xr, yr);
|
||||
end
|
||||
[h,b] = hist(rs, 20 );
|
||||
[h,b] = hist(rs, 20);
|
||||
h = h/sum(h)/(b(2)-b(1)); % normalization
|
||||
|
||||
% significance:
|
||||
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
|
||||
fprintf('--> correlation r=%.2f is significant\n', rd);
|
||||
else
|
||||
@ -32,7 +32,7 @@ end
|
||||
bar(b, h, 'facecolor', 'b');
|
||||
hold on;
|
||||
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');
|
||||
ylabel('Probability density of H0');
|
||||
hold off;
|
||||
|
Reference in New Issue
Block a user