Vincent
Vincent

Reputation: 723

PDF/Tex-file produced by knitr contains different random numbers from those generated with same code in console

Knitr is great at integrating text and output from code. The goal of using this is often called 'reproducible research'. Now when this research contains random numbers (e.g. for multiple imputations) then the key to reproducibility is the set.seed function in R.

But somehow the two types of reproducible are not compatible.

Look at the short Rnw-file below.

\documentclass{article}

\begin{document}

When I run the below chunck in the console I get (every time) the following numbers:

$$8, 94, 96, 92, 99, 57, 44, 45, 35, 43, 44, 71$$

However, when I run the enitre Rnw-program using the 'Compile pdf' button in Rstudio I get (every time) the following numbers:

$$34, 97, 52, 68, 2, 57, 76, 20, 96, 68, 27, 29$$

What number do \emph{you} get?

<<minimalexample, results = 'markup'>>=
set.seed(20200705)
sample(seq(1, 100), 12, replace = T)
@

\end{document}

My questions are:

Upvotes: 1

Views: 68

Answers (1)

Count Orlok
Count Orlok

Reputation: 1007

I managed to reproduce your results on R 4.0.2. Try running the following line in both your console and your knitr file:

RNGkind()

If I'm not mistaken, you'll see the following (different) outputs depending on where you're running it:

# In the console:
RNGkind()
# [1] "Mersenne-Twister" "Inversion"        "Rounding" 

# In the knitr file:
RNGkind()
# [1] "Mersenne-Twister" "Inversion"        "Rejection"     

Reading through the documentation, you find that it's the sample.kind that's different. From the above link:

sample.kind can be "Rounding" or "Rejection", or partial matches to these. The former was the default in versions prior to 3.6.0: it made sample noticeably non-uniform on large populations, and should only be used for reproduction of old results.

Seems like your console is for some reason stuck with an old version of sample generation. To make it run with the modern, "Rejection" version, execute the following in your console:

RNGkind(sample.kind = "Rejection")

You should then get the same random sequence as in your knitr file.

Upvotes: 3

Related Questions