Hi I'm Mike Marin and in this video we're going to discuss implementing a
bootstrap approach for building a confidence interval with R (Programming Language), to compare
a numeric variable for two different groups. This can be thought of as an
alternative approach to building a confidence interval for the difference
in means using large sample approaches. In separate videos we explain the
concept and general approach behind building this confidence interval. We
also show how to conduct a bootstrap hypothesis test on this exact same data!
"We can ask R to calculate the median weight only for those on the casein feed type"
as well as you can find links to all these videos in the video description
below; you can also find links to the R-Script and the data use there as well
It's highly recommended that you watch some of those videos before watching
this one; now let's import the data and remind ourselves of the data and context
a little bit: let's quickly add this data to the data view in case we want to look
at it that way and we can remind ourselves this dataset has two
variables weight as well as feed type, so it's looking at weight gain of chicks on
one of two different feed types casein or meatmeal, and we can also see this
dataset has 23 observations in total: 12 chicks on the casein feed type 11
on meatmeal. In order to explore the weight of these two different feed types
we can make side by side box plots here; now in the previous video when
discussing confidence intervals we decided we'd build a confidence interval
for two different estimates: the first estimate being the difference in means
of the two groups and the second estimate being the difference in medians
of the two groups. At the end of this R script we also provide code for building
a confidence interval for the difference in the 80th percentile of the weight
gain for the two different feed types now let's have R calculate each of
these two estimates; first we can look at having R calculate the mean for each
of the feed types here we can ask R (Programming Language) to calculate the mean weight only for those
whose feed is casein and the mean weight only for those whose feed is meat meal
and as always I try to include some different slightly more sophisticated
ways of doing things in R; so you can explore the following code and see how
we can make use of the width command (function)
Now let's calculate the sample or the
observed difference in means and let's store them in an object called
Obs.Diff.In.Means so we can do that here and taking a look at it we can see the difference
is 46.67 so the mean weight for casein on average was about
46.67 grams higher than it was for meat meal and again a fancier
way of doing that making use of the diff or difference command, with and a tapply
statement now we can quickly have R do the same for the observed or the
sample difference in medians so again here is calculating the median for each
group we can do that with a with command and let's calculate the
difference in medians and we'll store this in abs.diff.in.medians and again
here we can see that the sample difference in medians was seventy nine
In other words the median weight for a casein was 79 grams higher
than it was for meat meal. Now let's start discussing implementing a
bootstrap approach to building a confidence interval for each of these
estimates it's worth mentioning that R does have packages built for doing
bootstrapping you can see one of them here is the boot package so you can take
a look at that on your own if you want but I prefer to show you how to build
it yourself rather than rely on a package which may
or may not be easy to work with I always prefer to build some of these things
from principles myself; so when building this confidence interval the first thing
I'm going to do is I'm going to set a seed here: this isn't completely
necessary but the reason that I do this is so that I can get the exact same set
of bootstrap re-samples anytime I run this code then the nc here is the number
of casein observations to resample for each bootstrap sample recall that we had
12 observations for casein diet or feed type, nm is the number of meat meal
observations to resample again for each of the bootstrap samples and B is the
number of bootstrap samples that we'd like to take so here we'll take 100,000
bootstrap re-samples, and it's worth recalling that increasing
B or the number of bootstrap samples does not increase the amount of
information in our data, now because a confidence interval uses the observed or
the sample estimate as the point of reference we're going to sample with
replacement from casein and from meat meal separately. So to do this first we'll
produce a matrix of casein bootstrap resamples, we're going to take B equals
100,000 bootstrap samples each of size nc=12 so in other words we're going to ask
R to resample with replacement from the observed casein measurements and to
take B times nc, or a hundred thousand times 12 observations. We'll have this
stored in a matrix called boot.casein where each column of the matrix is a
bootstrap re-sample. So let's go ahead and have R do that for us: here we can
see that was done pretty quickly now we can ask R to do the exact same but for
the meat meal group. Now it's a good idea to check and make sure that R did what
we thought it did: so first we can check the dimensions of each of those matrices
to make sure that looks right. We can see they're 12 rows by a hundred
thousand columns or 11 rows by a hundred thousand columns so that looks
good so far! the dimensions look right now let's also check just the first five
rows and first five columns of each just to make sure that they've been populated
with data and they're not sitting there empty so again we can see here's the
casein bootstrap samples and the meat meal and so it looks good looks like
R has done what we hoped it was doing now let's calculate the bootstrap estimates
of the difference in means. Here I'm going to make use of the colmeans
command to calculate the mean of the columns. You'll recall that each column
is a bootstrap sample so I'm going to ask R to calculate the mean for casein
in each bootstrap sample and then subtract from that the mean of meat meal
in each bootstrap sample and we're going to store all of this in Boot.Diff.In.Means
so let's submit that and have R do
that here and again let's quickly check that to make sure R has done what we
thought it has; we can see this Boot.Diff.In.Means consists of a hundred thousand
elements which is what we would hope and taking a look at the first ten
difference in means we can see here is the first ten bootstrap estimates of the
difference in means now we can do the same for estimate number two the
difference in medians here we want to calculate the median of each column
right of the bootstrap samples, and to do so I'm going to make use of apply
statement and I'm going to apply the median function to columns which is
indicated by margin equals two argument so let's submit that here and again it's
good just to check to make sure that R did what we thought it did we can see
this boot.diff.in.medians consists of one hundred thousand elements or
estimates here's the first ten bootstrap estimates of the difference in medians
so now let's get to building those confidence intervals. Now the first thing
to mention is there are slightly different approaches to building a
confidence interval using a bootstrap approach; the most commonly used
approaches are the percentile method, the basic method, the normal method, and the
bias corrected method, in this video we'll focus on implementing the
percentile method as it's the most intuitive and we're going to leave it to
you to read a bit more about the others if you'd like. The percentile method
takes the entire set of bootstrap estimates and in order to form a 95%
confidence interval, the second and a half percentile of all the bootstrap
estimates or bootstrap distribution as used as the lower bound for the interval
and the ninety seven and a half percentile of the bootstrap estimates is used
as the upper bound. In other words the range of the middle 95% of bootstrap
estimates is used to form the 95% confidence interval. So let's have R (software)
calculate the second and a half percentile as well as the ninety seven
and a half percentile of the bootstrap estimates first for the difference in
means. Here we can use the quantile command and asking R for the
bootstrap difference in means find the second and a half percentile and now we
can ask it to find the 97.5 percentile so here we'd say that we're 95%
confident the true or population difference in means is somewhere between
about - 4 up to positive 96.8 or using casein as our reference diet we
can phrase that as we're 95% confident that the mean weight for casein is
somewhere between about 4 grams lower than meat meal up to 96.8 grams higher
than meat meal. Similarly we can construct the 95% confidence interval
for the difference in medians using the percentile method so let's do that here:
again we're in asked R for the second and a half percentile of all the
bootstrap difference in medians as well as the 97.5 percentile for
the bootstrap difference in medians. Here we can say that we're 95% confident that
the median weight for casein is somewhere between about 24.5
grams lower up to a 116 grams higher than meat meal. Now taking note
that both of these intervals contain 0, from this we can deduce that the means
or the medians are not statistically significantly different. It's worth
recalling that throughout all of our videos we regularly remind ourselves of
the difference between statistical significance and scientific significance
while these differences do not appear to be statistically significant there is
evidence that the feed types may differ so you'll recall that we only had 23
total observations 12 casein and 11 meat meal and I'd say looking at this
data definitely warrants further investigation. now in the R-script
we've included some extra code to construct confidence intervals using the
basic method for a confidence interval as well as some code for confidence
intervals where the estimate is the difference in the 80th percentile of
weight for each of the feed type so you can feel free to explore these on your own!
Thanks for Watching! Don't forget to Subscribe