dimanche 5 juillet 2020

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:

  • How is this possible?
  • Can we do something to get the same numbers in both ways of running the code?
  • What numbers do you (the reader) get when running this program and/or only the chunck?



Aucun commentaire:

Enregistrer un commentaire