diff --git a/R/draws.R b/R/draws.R index 9c8f8afb0..b00649978 100644 --- a/R/draws.R +++ b/R/draws.R @@ -478,10 +478,6 @@ extract_data_nmar_as_na <- function(longdata) { #' @export draws.bayes <- function(data, data_ice = NULL, vars, method, ncores = 1, quiet = FALSE) { - if (!is.na(method$seed)) { - set.seed(method$seed) - } - longdata <- longDataConstructor$new(data, vars) longdata$set_strategies(data_ice) diff --git a/R/mcmc.R b/R/mcmc.R index be8848698..d84d35477 100644 --- a/R/mcmc.R +++ b/R/mcmc.R @@ -61,7 +61,6 @@ fit_mcmc <- function( n_imputations <- method$n_samples burn_in <- method$burn_in - seed <- method$seed burn_between <- method$burn_between same_cov <- method$same_cov @@ -114,13 +113,7 @@ fit_mcmc <- function( ) ) - assert_that( - !is.na(seed), - !is.null(seed), - is.numeric(seed), - msg = "mcmc seed is invalid" - ) - sampling_args$seed <- seed + sampling_args$seed <- sample.int(.Machine$integer.max, 1) stan_fit <- record({ do.call(sampling, sampling_args) diff --git a/R/methods.R b/R/methods.R index c652cdf94..406f22824 100644 --- a/R/methods.R +++ b/R/methods.R @@ -44,10 +44,7 @@ #' @param type a character string that specifies the resampling method used to perform inference #' when a conditional mean imputation approach (set via `method_condmean()`) is used. Must be one of `"bootstrap"` or `"jackknife"`. #' -#' @param seed a numeric that specifies the seed to be used in the call to Stan. This -#' argument is passed onto the `seed` argument of [rstan::sampling()]. Note that -#' this is only required for `method_bayes()`, for all other methods you can achieve -#' reproducible results by setting the seed via `set.seed()`. See details. +#' @param seed deprecated. Please use `set.seed()` instead. #' #' @details #' @@ -93,14 +90,20 @@ method_bayes <- function( burn_between = 50, same_cov = TRUE, n_samples = 20, - seed = sample.int(.Machine$integer.max, 1) + seed = NULL ) { + if (!is.null(seed)) { + warning(paste0( + "The `seed` argument to `method_bayes()` has been deprecated", + " please use `set.seed()` instead" + )) + } + x <- list( burn_in = burn_in, burn_between = burn_between, same_cov = same_cov, - n_samples = n_samples, - seed = seed + n_samples = n_samples ) return(as_class(x, c("method", "bayes"))) } diff --git a/data-raw/create_print_test_data.R b/data-raw/create_print_test_data.R index 9c324d629..71c2c120d 100644 --- a/data-raw/create_print_test_data.R +++ b/data-raw/create_print_test_data.R @@ -107,14 +107,14 @@ set.seed(413) dobj <- get_data(40) suppressWarnings({ + set.seet(859) drawobj_b <- draws( data = dobj$dat, data_ice = dobj$dat_ice, vars = dobj$vars, method = method_bayes( n_samples = 50, - burn_between = 1, - seed = 859 + burn_between = 1 ) ) }) diff --git a/man/method.Rd b/man/method.Rd index 0ba22c87c..e8077e0b9 100644 --- a/man/method.Rd +++ b/man/method.Rd @@ -13,7 +13,7 @@ method_bayes( burn_between = 50, same_cov = TRUE, n_samples = 20, - seed = sample.int(.Machine$integer.max, 1) + seed = NULL ) method_approxbayes( @@ -61,10 +61,7 @@ matrix will be fit for each group as determined by the \code{group} argument of In the case of \code{method_condmean(type = "jackknife")} this argument must be set to \code{NULL}. See details.} -\item{seed}{a numeric that specifies the seed to be used in the call to Stan. This -argument is passed onto the \code{seed} argument of \code{\link[rstan:stanmodel-method-sampling]{rstan::sampling()}}. Note that -this is only required for \code{method_bayes()}, for all other methods you can achieve -reproducible results by setting the seed via \code{set.seed()}. See details.} +\item{seed}{deprecated. Please use \code{set.seed()} instead.} \item{covariance}{a character string that specifies the structure of the covariance matrix to be used in the imputation model. Must be one of \code{"us"} (default), \code{"toep"}, diff --git a/tests/testthat/_snaps/print.md b/tests/testthat/_snaps/print.md index 6f549fb3e..989ca93a3 100644 --- a/tests/testthat/_snaps/print.md +++ b/tests/testthat/_snaps/print.md @@ -255,7 +255,6 @@ burn_between: 1 same_cov: TRUE n_samples: 50 - seed: 859 --- diff --git a/tests/testthat/test-mcmc.R b/tests/testthat/test-mcmc.R index 7fb72148f..422f4d544 100644 --- a/tests/testthat/test-mcmc.R +++ b/tests/testthat/test-mcmc.R @@ -529,8 +529,7 @@ test_that("fit_mcmc can recover known values with same_cov = FALSE", { n_samples = 250, burn_in = 100, burn_between = 3, - same_cov = FALSE, - seed = 8931 + same_cov = FALSE ) ### No missingness @@ -604,36 +603,17 @@ test_that("fit_mcmc can recover known values with same_cov = FALSE", { }) -test_that("invalid seed throws an error", { - - set.seed(301) - sigma <- as_vcov(c(6, 4, 4), c(0.5, 0.2, 0.3)) - dat <- get_sim_data(50, sigma) - - dat_ice <- dat %>% - group_by(id) %>% - arrange(desc(visit)) %>% - slice(1) %>% - ungroup() %>% - mutate(strategy = "MAR") - - vars <- set_vars( - visit = "visit", - subjid = "id", - group = "group", - covariates = "sex", - strategy = "strategy", - outcome = "outcome" - ) - - expect_error( - draws( - dat, - dat_ice, - vars, - method_bayes(n_samples = 2, seed = NA), - quiet = TRUE - ), - regexp = "mcmc seed is invalid" +test_that("seed argument to method_bayes is depreciated", { + expect_warning( + { + method <- method_bayes( + n_samples = 250, + burn_in = 100, + burn_between = 3, + same_cov = FALSE, + seed = 1234 + ) + }, + regexp = "seed.*deprecated" ) }) diff --git a/tests/testthat/test-print.R b/tests/testthat/test-print.R index f661573ef..fa592370d 100644 --- a/tests/testthat/test-print.R +++ b/tests/testthat/test-print.R @@ -104,8 +104,7 @@ test_that("print - bayesian", { vars = dobj$vars, method = method_bayes( n_samples = 50, - burn_between = 1, - seed = 859 + burn_between = 1 ), quiet = TRUE ) diff --git a/tests/testthat/test-reproducibility.R b/tests/testthat/test-reproducibility.R index fdc8e70fd..04809a9fb 100644 --- a/tests/testthat/test-reproducibility.R +++ b/tests/testthat/test-reproducibility.R @@ -86,7 +86,7 @@ test_that("Results are Reproducible", { -test_that("bayes - seed argument works without set.seed", { +test_that("bayes - set.seed produces identical results", { sigma <- as_vcov(c(2, 1, 0.7), c(0.5, 0.3, 0.2)) dat <- get_sim_data(200, sigma, trt = 8) %>% @@ -111,17 +111,16 @@ test_that("bayes - seed argument works without set.seed", { ) meth <- method_bayes( - seed = 1482, burn_between = 5, burn_in = 200, - n_samples = 2 + n_samples = 6 ) - set.seed(49812) + set.seed(1234) x <- suppressWarnings({ draws(dat, dat_ice, vars, meth, quiet = TRUE) }) - set.seed(2414) + set.seed(1234) y <- suppressWarnings({ draws(dat, dat_ice, vars, meth, quiet = TRUE) }) diff --git a/vignettes/advanced.html b/vignettes/advanced.html index d0a958b43..84a8d0aeb 100644 --- a/vignettes/advanced.html +++ b/vignettes/advanced.html @@ -714,7 +714,7 @@
To further illustrate this for a simple example, assume that a new strategy is to be implemented as follows: - The marginal mean of the imputation distribution is equal to the marginal mean trajectory for the subject according to their assigned group and covariates up to the ICE. diff --git a/vignettes/quickstart.Rmd b/vignettes/quickstart.Rmd index ac8a64f32..56b8cee88 100644 --- a/vignettes/quickstart.Rmd +++ b/vignettes/quickstart.Rmd @@ -117,8 +117,7 @@ vars <- set_vars( method <- method_bayes( burn_in = 200, burn_between = 5, - n_samples = 150, - seed = 675442751 + n_samples = 150 ) # Create samples for the imputation parameters by running the draws() function @@ -347,8 +346,7 @@ vars <- set_vars( method <- method_bayes( burn_in = 200, burn_between = 5, - n_samples = 150, - seed = 675442751 + n_samples = 150 ) diff --git a/vignettes/quickstart.html b/vignettes/quickstart.html index 58495d205..d22421ae8 100644 --- a/vignettes/quickstart.html +++ b/vignettes/quickstart.html @@ -441,34 +441,35 @@
Note the use of set_vars()
which specifies the names of the key variables
within the dataset and the imputation model. Additionally, note that whilst vars$group
and vars$visit
are added as terms to the imputation model by default, their interaction is not,
@@ -693,15 +694,15 @@
The table of values shown in the print message for poolObj
can also be extracted using the as.data.frame()
function:
as.data.frame(poolObj)
@@ -709,18 +710,18 @@ 6 Pool
#> 1 trt_4 -0.09180645 0.6826279 -1.43949684 1.2558839 8.931772e-01
#> 2 lsm_ref_4 -1.61581996 0.4862316 -2.57577141 -0.6558685 1.093708e-03
#> 3 lsm_alt_4 -1.70762640 0.4749573 -2.64531931 -0.7699335 4.262148e-04
-#> 4 trt_5 1.28644500 0.9277209 -0.54590272 3.1187927 1.674969e-01
-#> 5 lsm_ref_5 -4.11444658 0.6617013 -5.42140736 -2.8074858 4.341299e-09
-#> 6 lsm_alt_5 -2.82800159 0.6453115 -4.10255468 -1.5534485 2.131897e-05
-#> 7 trt_6 1.90919233 0.9961837 -0.05886381 3.8772485 5.716563e-02
-#> 8 lsm_ref_6 -6.08225789 0.7116785 -7.48832324 -4.6761925 1.254942e-14
-#> 9 lsm_alt_6 -4.17306556 0.6934318 -5.54301764 -2.8031135 1.257113e-08
-#> 10 trt_7 2.06527821 1.1246338 -0.15780585 4.2883623 6.837825e-02
-#> 11 lsm_ref_7 -6.93702520 0.8174548 -8.55359983 -5.3204506 3.240396e-14
-#> 12 lsm_alt_7 -4.87174699 0.7866796 -6.42696291 -3.3165311 6.097291e-09
These outputs gives an estimated difference of -2.065 (95% CI -0.158 to 4.288) -between the two groups at the last visit with an associated p-value of 0.068.
+2.136 (95% CI -0.078 to 4.350) +between the two groups at the last visit with an associated p-value of 0.058.