When working with students and postdocs, I often provide “skeletons”, i.e., canonical examples of code that illustrate something that I want them to do in another context. Example: running some code in parallel. Here, I will carry out a very simple and naive sensitivity analysis of the value of the basic reproduction number $$\mathcal{R}_0$$ in the basic Kermack and McKendrick SIR model.

First, we need the function that we will evaluate for a given point in parameter space. Note that we pass parameters as a list rather than an ordered t-uple of parameters; this allows to not have to worry about the order in which parameters are presented.

The following function converts one or several parameter values given in the list by lapply or parLapply into a value of $$\mathcal{R}_0$$. Note the little trick, which allows to set as many parameters as are provided in the element of the list received as argument.

Set up parameters by default. If not changed by the function above, they take these values.

Set up the list of parameters that are going to vary. For illustration, we do 10,000 computations for varying values of $$\beta$$, 10,000 for varying values of $$S_0$$ and 10,000 simulations for varying values of both $$\beta$$ and $$S_0$$.

The code can be run in parallel or iteratively. Note one interesting quirk: the function argument to parLapply is called fun, while that to lapply is called FUN. You have to love R. One core is set aside to avoid rendering the computer completely unresponsive; on a cluster with head node, you would of course be able to devote all cores to computations. (But then again, on a cluster with head node, you might also be using a scheduler.)

Last little piece: let us plot the range of values taken by $$\mathcal{R}_0$$ when the various parameters are varied, to show sensitivity of $$\mathcal{R}_0$$.