0

I have re-run with the same seed but cannot produce same results in R studio.

Will set.seed() lose track when calling different user packages, R6 classes and methods?

Have you come across similar situation and how to fix it?

Thanks a lot

closeAllConnections()
rm(list=ls())
gc()

set.seed(123456)
require(PackageA)  #user defined package

fileName <- "N:/Risk Management/abc.json";

configuration <- JsonConfigurationReader$readConfiguration(fileName); #R6 class and method in PackageA

instance <- someClass$new(configuration); #R6 class and method in PackageA

instance$execute()  #will produce some random numbers in this method

closeAllConnections()
rm(list=ls())
gc()
waith
  • 21
  • 1
  • 3
  • 4
    Different packages can use different functions to generate "randomness" rather than using the built in R methods. Exactly how to fix this depends completely on the code in the package. In order to help you any further you'd need to include a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – MrFlick Dec 13 '18 at 06:31
  • Sorry it would be quite difficult to include a full reproducible example as one package calls another package and so on. But the methods to generate random numbers inside different packages are just using rnorm, runif and mvrnorm. I don't understand the set.seed() in the beginning would not fix the results across different runs. – waith Dec 16 '18 at 10:10
  • 1
    @MrFlick you are right that the randomness does depend on the function and package. I have changed the function to mvtnorm::rmvnorm(.., method = "chol") instead of MASS::mvrnorm() using "eigen". The results are reproducible now with set.seed(). Thanks. – waith Dec 18 '18 at 23:09
  • As you commented, waith, MASS:mvrnorm() is not reproducible even when using a fixed seed. For another alternative and a detailed explanation, see: https://www.rdocumentation.org/packages/rockchalk/versions/1.8.129/topics/mvrnorm – Fato39 Jan 13 '19 at 21:25

1 Answers1

1

The issue would come if you are using diffrent R versions (3.5 vs 3.6 and above). The version upto 3.5 will show same results but for the same code the versions 3.6 will show a diffrent results. However version 3.6 and 4.0 will have same results.

Rahul
  • 11
  • 3