(c) 2018 Justin Bois. With the exception of pasted graphics, where the source is noted, this work is licensed under a Creative Commons Attribution License CC-BY 4.0. All code contained herein is licensed under an MIT license.
This document was prepared at Caltech with financial support from the Donna and Benjamin M. Rosen Bioengineering Center.
This tutorial was generated from a Jupyter notebook. You can download the notebook here.
As you can imagine, it is quite useful to be able to generate, or "draw," bootstrap replicates. Now, you will write a function, draw_bs_reps()
to do this automatically. You will want to include this in a module so you can use it over and over again. (I will not be providing it in the bootcamp_utils
module; I want you to write this yourself.)
- Define a function with call signature
draw_bs_reps(data, func=np.mean, size=1)
, wherefunc
is a function that takes in an array and returns a statistic. Examples that could be passed in asfunc
arenp.mean
,np.std
,np.median
, or a user-defined function.size
is the number of replicates to generate.- Write a good doc string.
- Define
n
to be the length of the inputdata
array.- Use a list comprehension to compute a list of bootstrap replicates.
- Return the replicates as a Numpy array.
If you like, you can use the bs_replicate()
function from the previous lesson.
Now that you have the function, feel free to play around with it and compute bootstrap replicates with the finch beak or other data you want to play with. This is a function you may use over and over again in your research, so you might want to keep it in a module you will use going forward. Or, you can install the dc_stat_think
module that I wrote using pip
, which has this and many other useful functions for bootstrapping.
To help you visualize how bootstrap samples can show what you might expect by repeating experiments, we will make a plot of ECDFs of lots of bootstrap samples.
- Load in the beak depth data from 1975.
- Generate the
x
andy
values for the ECDF of the data and plot them as before. Plot them with opacity = 0.3.- Write a
for
loop to do the following 100 times:
- Generate a bootstrap sample from the data set using
np.random.choice()
.- Compute the
x
andy
values for the ECDF of the bootstrap sample.- Add these samples to a
DataFrame
.- Plot the bootstrap sample ECDFs in a different color with opacity = 0.01.
In order to do this, you will need to allow for a larger data frame for your plot using
alt.data_transformers.enable('default', max_rows=None)
or by using
alt.data_transformers.enable('json')
Be aware of the caveats of that, though.