Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix seeds/PRNG usage #412

Merged
merged 19 commits into from
May 11, 2023
Merged

Fix seeds/PRNG usage #412

merged 19 commits into from
May 11, 2023

Conversation

fweber144
Copy link
Collaborator

This is a major fix for PRNG usage within projpred, see the NEWS.md entries added here. The commit messages provide more details.

With this PR, we also make use of the fix in the vignettes: Now, we only need to set a seed once at the beginning (except for the comparison of latent and traditional projection, but this has nothing to do with the seeds issue fixed here).

use `NA` for seed arguments by default (to avoid that the PRNG state
(`.Random.seed`) is reset after each call to a function which has a seed
argument, which causes PRNG results to be less random than they should be).
`cv_varsel()` themselves. This concerns K-fold CV based on `cvfun`. The breaking
change consists of the possibility of different results since the PRNG state
when generating the CV folds would be a different one when using the new default
of `seed = NA` in `cv_folds()` (so the folds could be composed differently).

Using `seed = sample.int(.Machine$integer.max, 1)` in this special `cv_folds()`
call should not be a problem as the PRNG state that `cv_folds()` resets to upon
exit is never used again for generating a seed. (After this special `cv_folds()`
call, the next PRNG-using code is `kmeans()`.) Nevertheless, this complicated
construction can be avoided in the feature when introducing other breaking
changes (e.g., in an upcoming UI overhaul).
because argument `seed` (or `.seed`) might consist of a lazily evaluated
expression that makes use of the PRNG (so `seed` in `if (!is.na(seed))` advances
the PRNG state).
did with the former default of `.seed = sample.int(.Machine$integer.max, 1)`,
but I realized this only now.
`summary.vsel()` call to have the performance metrics table really match the
plot.
@fweber144 fweber144 merged commit ee88bb0 into stan-dev:master May 11, 2023
@fweber144 fweber144 deleted the seeds_fixes branch May 11, 2023 07:40
@fweber144
Copy link
Collaborator Author

I forgot to add a short illustration of the issue and the fix for it:

data("df_gaussian", package = "projpred")
df_gaussian <- df_gaussian[1:29, ]
dat <- data.frame(y = df_gaussian$y, df_gaussian$x)
library(rstanarm)
rfit <- stan_glm(y ~ X1 + X2 + X3 + X4 + X5,
                 data = dat,
                 chains = 1,
                 iter = 500,
                 seed = 1140350788,
                 refresh = 0)

### Use the installed version 2.5.0 (or load it via devtools::load_all()):
library(projpred)
###
### Load a version > 2.5.0 containing the fix for this seed issue (or
### attach it via `library(projpred)` if installed):
# devtools::load_all()
###

set.seed(467845213)
vs1 <- varsel(rfit,
              nclusters = 23,
              nclusters_pred = 25)
vs2 <- varsel(rfit,
              nclusters = 23,
              nclusters_pred = 25)
smmry1 <- summary(vs1)
smmry1 <- smmry1[setdiff(names(smmry1), "family")]
smmry2 <- summary(vs2)
smmry2 <- smmry2[setdiff(names(smmry2), "family")]
identical(smmry1, smmry2)
## --> `TRUE` with projpred 2.5.0, `FALSE` with a projpred version > 2.5.0
## containing the fix for this seed issue.
vs1 <- vs1[setdiff(names(vs1), c("refmodel", "search_path"))]
vs2 <- vs2[setdiff(names(vs2), c("refmodel", "search_path"))]
identical(vs1, vs2)
## --> `TRUE` with projpred 2.5.0, `FALSE` with a projpred version > 2.5.0
## containing the fix for this seed issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant