Knitt(r)ing

One of the things that fills my Januaries and Februaries is the preparation of the in-course assessment (ICA) and final exam, for the course I teach at UCL.

This year is the first time I’m teaching on the new module on Bayesian methods in health economics (page 40 of the students’ handbook) so I had to prepare both without relying to any previous scripts (although I did have lots of practicals/exercises, which I could use for the actual lectures, luckily!).

Anyway, earlier last year, Ioannis organised a short workshop in which Achim Zeileis showed us how to use his R package “exams” $-$ this is kind of cool; basically you can create a database of questions (including computations, which are directly performed in R), then use things like Sweave or Knitr to automatically produce the pdf file with your exam (or ICA) text and, possibly the solution. It’s even possible to randomise the questions so as to produce new versions of the exam.

I have not used directly this package, but I’ve been doing lots of knitt®ing of late to create the files (incidentally, that’s not a picture of me knitting). It does take some time to set up the whole procedure, but by doing some clever combination of R and LaTeX, it’s possible to highly automatise the process.

For example, one can simply put in the .Rnw file a command \newif\IfShowSol and then create two sub-chunks of text, one where \IfShowSol is TRUE and put suitable text to describe the solution to the exercise; and another one where \IfShowSol is FALSE and nothing else is shown. Something like:

\IfShowSoltrue What is the probability that $X>2.5$? \IfShowSol Simple to compute by just summing the simulated values for which $X>2.5$ and then dividing by the number of simulations \else \fi

would show the solutions in the resulting .pdf file (while substituting \IfShowSoltrue with \IfShowSolfalse wouldn’t!).