sample function gives different result in console and in knitted document when seed is set

扶醉桌前 提交于 2020-01-30 09:18:05

问题


I'm creating a document in a Rmarkdown file and knitting to HTML for file submission. Generating seeded samples using the sample function provide different results in the console and the knitted file.

I am using R Studio version 1.0.153 and R 3.6.0

edit: I have updated R Studio to version 1.2.1335 and am still having this issue

set.seed(1)
rnorm(1)
sample(1:10, 1)

in the console and knitted file, the value for rnorm(1) is the same, however, in the console, I see that I have sampled 6 in the console, and 7 in the knitted document


回答1:


R 3.6.0 changed the sampling method. With the new method (default or Rejection) I get 7. With the old one I get 6:

set.seed(1)
rnorm(1)
#> [1] -0.6264538
sample(1:10, 1)
#> [1] 7

set.seed(1, sample.kind = "Rounding")
#> Warning in set.seed(1, sample.kind = "Rounding"): non-uniform 'Rounding'
#> sampler used
rnorm(1)
#> [1] -0.6264538
sample(1:10, 1)
#> [1] 6

Created on 2019-05-23 by the reprex package (v0.2.1)

So it seems you have somehow set sample.kind = "Rounding" in the console. You can check this from the output of RNGkind().



来源:https://stackoverflow.com/questions/56268011/sample-function-gives-different-result-in-console-and-in-knitted-document-when-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!