JeffreyRacine / R-Package-np

R package np (Nonparametric Kernel Smoothing Methods for Mixed Data Types)
https://socialsciences.mcmaster.ca/people/racinej
47 stars 18 forks source link

is the random.seed in npsigtest local or global #31

Closed waynelapierre closed 3 years ago

waynelapierre commented 3 years ago

I would like to ask whether the random.seed in npsigtest local or global. If it is global, then I need to pay attention to my other bootstrap and simulation codes after using the npsigtest function. Thanks.

JeffreyRacine commented 3 years ago

Hi Wayne,

The seed is local only and does not affect things outside of the function. Here is the snippet that checks for a global seed and saves it first when entering any function that has the option random.seed=42... once the function is entered the first order of business is:

## Save seed prior to setting

if(exists(".Random.seed", .GlobalEnv)) {
  save.seed <- get(".Random.seed", .GlobalEnv)
  exists.seed = TRUE
} else {
  exists.seed = FALSE
}

set.seed(random.seed)

Then, before exiting the function you will see

## Restore seed

if(exists.seed) assign(".Random.seed", save.seed, .GlobalEnv)

You can always experiment to confirm, but this should reassure you that it won't interfere with replicating simulation experiments that use their own seed.

Jeff

waynelapierre commented 3 years ago

thanks for the clarification!