# Days 18-19: Reference management in R

### by Danielle Navarro, 15 May 2018

I’m in the middle of writing a paper today, and writing it in LaTeX as I usually do. As much as LaTeX frustrates me, I do think it’s a really nice tool for writing print-friendly documents. It handles floats nicely, creates beautiful PDF documents, and generally does most of the things that you want of a system designed to typeset paginated documents. On the other hand, it’s not great for generating HTML, doesn’t play very nicely with R, and is … quirky. For the most part I like it, but not everything is appropriate for LaTeX. Sometimes you really do want to generate your documents in R Markdown and the like, and sometimes that means you might want to insert citations. LaTeX already has a very nice system for managing references (BibTeX), given that the citation function in R already supplies BibTeX entries it would be nice to have a clean way to integrate this into an R Markdown document.The RefManageR package allows you to do this in a not-completely-painful fashion.

## An example

Okay, so here’s a couple of sentences from the paper I’m working on…

We examine human performance on several variations of the vanishing bandits task, involving different levels of volatility (i.e. rate of change to the reward distribution), comparing it to a standard restless bandit task in which options do not vanish. To provide a point of comparison, we follow Speekenbrink and Konstantinidis (2015) and apply a Bayesian reinforcement learning model, namely the Kalman filter learning rule (Daw, O’Doherty, Dayan, Seymour, and Dolan, 2006; Kalman, 1960) using a decision rule that selects options with probability proportional to the likelihood that they are the maximum utility option, an approach referred to as Thompson sampling (Thompson, 1933).

[1] N. D. Daw, J. P. O’Doherty, P. Dayan, et al. “Cortical substrates for exploratory decisions in humans”. In: Nature 441.7095 (2006), p. 876.

[2] R. E. Kalman. “A new approach to linear filtering and prediction problems”. In: Journal of basic Engineering 82.1 (1960), pp. 35-45.

[3] M. Speekenbrink and E. Konstantinidis. “Uncertainty and exploration in a restless bandit problem”. In: Topics in cognitive science 7.2 (2015), pp. 351-367.

[4] W. R. Thompson. “On the likelihood that one unknown probability exceeds another in view of the evidence of two samples”. In: Biometrika 25.3/4 (1933), pp. 285-294.

## Producing this with RefManageR

The passage cites four articles, using three different styles. The first is an in text citation, the second is a parenthetical citation with multiple references, and the third is a single parenthetical citation. The references are stored in a bibtex file vanishing.bib that stores these and a number of other citations that are relevant to the paper.

Producing the passage above is surprisingly easy. First I load the package and import the file…

library(RefManageR)
vb <- ReadBib(file = "vanishing.bib")

Then I write as I normally would in LaTeX, but using the Cite and Citet functions in R rather than the LaTeX equivalent:

We examine human performance on several variations of the vanishing
bandits task, involving different levels of volatility (i.e. rate of
change to the reward distribution), comparing it to a standard
restless bandit task in which options do not vanish. To provide a
point of comparison, we follow Speekenbrink and Konstantinidis (2015)
and apply a Bayesian reinforcement learning model, namely the Kalman
filter learning rule (Daw, O'Doherty, Dayan, et al., 2006; Kalman, 1960)
using a decision rule that selects options with probability proportional
to the likelihood that they are the maximum utility option, an approach
referred to as Thompson sampling (Thompson, 1933).

Then at the end I insert the reference list:

PrintBibliography(vb)

A quick look at the package suggests that it’s reasonably flexible, though to anyone coming from LaTeX it does feel a little clunky. That’s not a criticism though - reference management is tedious and neither the R language nor markdown (or HTML, to be honest) is really designed to handle it. I think I’d probably be a bit frustrated trying to write full length scientific papers with this, but even so I really love this - it’s nice to have a tool at hand that will let me manage citations in a pretty decent way without having to sacrifice all the things I like about R Markdown. Among other things, I can see it being pretty handy for writing blog posts! 😀