Bootstrapping Two Medians with the "bootstrp" function

9 views (last 30 days)
Any idea on how to implement the Bootstrapping Two Medians method - here below explained - with the "bootstrp" function?
  1. Bootstrap each sample separately, creating the sampling distribution for each median.
  2. Then calculate the difference between the medians, and create the sampling distribution of those differences. This is the sampling distribution we care about.
  3. Once we have that distribution we can establish a confidence interval on that, and report the result.
  4. If the confidence interval does not include 0, we can reject the null hypothesis that there is no difference between the medians of the two conditions.
% the two samples to be used as inputs
x = normrnd(10, 2, [100, 1]);
y = normrnd(12, 3, [100, 1]);
% (1) Bootstrap each sample separately
boot_x = ?
boot_y = ?

Answers (1)

Ayush
Ayush on 30 Oct 2024
Hi @Sim,
To implement Bootstrapping Two Medians method using the bootstrp function in MATLAB, you can follow these steps:
  1. Utilize the "bootstrp" function to generate bootstrap samples for each individual dataset separately. You can read more about "bootstrp" function here: https://www.mathworks.com/help/stats/bootstrp.html
  2. For each bootstrap sample, try to compute the median using the “median” function. You can read more about it here: https://www.mathworks.com/help/matlab/ref/double.median.html
  3. Try to Calculate the differences between the medians of the two bootstrap samples.
  4. Use the differences to compute the confidence interval.
  5. Check if the confidence interval includes zero. This technique is called Hypothesis Testing”.
Here's how you could implement this in MATLAB:
% Generating the two samples
x = normrnd(10, 2, [100, 1]);
y = normrnd(12, 3, [100, 1]);
% Bootstrap each sample separately
numBootstraps = 1000;
bootFun = @(data) median(data);
% Generating bootstrap samples and calculate medians
boot_x = bootstrp(numBootstraps, bootFun, x);
boot_y = bootstrp(numBootstraps, bootFun, y);
median_diff = boot_x - boot_y;
alpha = 0.05; % For a 95% confidence interval
CI = prctile(median_diff, [100 * (alpha/2), 100 * (1 - alpha/2)]);
fprintf('Confidence Interval for the difference of medians: [%f, %f]\n', CI(1), CI(2));
% Hypothesis testing
if CI(1) > 0
fprintf('Reject the null hypothesis: there is a significant difference.\n');
elseif CI(2) < 0
fprintf('Reject the null hypothesis: there is a significant difference.\n');
else
fprintf('Fail to reject the null hypothesis: no significant difference.\n');
end
you can read more about "prctile" function here:
Hope it helps!
  2 Comments
Sim
Sim on 30 Oct 2024
Thanks, this looks like a typical AI generated answer. ChatGPT? Gemini?
Ayush
Ayush on 31 Oct 2024
The question revolves around the standard usage of the "bootstrp" function. You should be able to write the code yourself with ease by referring to its documentation here: https://www.mathworks.com/help/stats/bootstrp.html.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!