Lately, I’ve been using more complicated estimators for both coursework
and my own research. Often times, these estimators don’t have very
pretty analytical solutions for their variance-covariance matrices. One
way to get around this, is to bootstrap your estimator to get valid
uncertainty estimates. Unfortunately, bootstrapping requires me to use
for loops (which I despise). While for loops do tend to make for
more readable code, they are hilariously slow in R.
After some searching around the internet, I ran across one potential fix
to help speed up bootstrap calculations: parallel programming. Using the
foreach package in R, I can speed up my bootstrap quite easily. The
following example demonstrates the performance gains of using a
foreach loop instead of for loop.
Regular For Loop
Okay not terrible… but let’s see how the parallelized version of for
Parallel For Loop
Clearly there are some pretty large performance gains here. Moreover,
the syntax behind the foreach loop still retains the readibility of
the regular for loop. In sum, we can get some pretty large performance
gains from some very easy tweaks to our R code.