vendredi 15 janvier 2021

Unexpected behavior when using R sample function with rpy2?

I need to cross-validate an R code in python. My code contains lots of pseudo-random number generations, so, for an easier comparison, I decided to use rpy2 to generate those values in my python code "from R".

As an example, in R, I have:

set.seed(1234)
runif(4)
[1] 0.1137034 0.6222994 0.6092747 0.6233794

In python, using rpy2, I have:

import rpy2.robjects as robjects
set_seed = robjects.r("set.seed")
runif =  robjects.r("runif")
set_seed(1234)
print(runif(4))
[1] 0.1137034 0.6222994 0.6092747 0.6233794

as expected (values are similar). However, I face a strange behavior with the R sample function (equivalent to the numpy.random.choice function).

As the simplest reproducible example, I have in R:

set.seed(1234)
sample(5)
[1] 1 3 2 4 5

while in python I have:

sample =  robjects.r("sample")
set_seed(1234)
print(sample(5))
[1] 4 5 2 3 1

The results are different. Could anyone explain why this happens and/or provide a way to get similar values in R and python using the R sample function?




Aucun commentaire:

Enregistrer un commentaire