我正在写一个R package,花了一个星期来追踪一个错误,并从多个角度为它编写了测试。其中之一是统计抽样的平均结果在一定范围内。我在抽样之前添加了set.seed(1)
,以确保可重复性。采样使用rstan
和stats::rbinom
。
我打印两个系数的平均值,并且两次打印的测试结果不同。第一次运行给出:
...
Chain 1: COMPLETED.
[1] 0.3247559
[1] -0.1190542
...
第二个给出:
Chain 1: COMPLETED.
[1] 0.3181746
[1] -0.1384806
为什么这是不可复制的?我应该再播种吗?
使用包中许多功能的完整测试是:
test_that("Adaptive non-parametric learning with posterior samples works well", {
german <- load_dataset(list(name = k_german_credit))
n_bootstrap <- 100
# Get posterior samples
prior_variance <- 100
bayes_logit_model <- rstan::stan_model(file = get_rstan_file())
train_dat <- list(n = german$n,
p = german$n_cov + 1,
x = cbind(1, german$x),
y = 0.5 * (german$y + 1),
beta_sd = sqrt(prior_variance))
set.seed(1)
stan_vb <- rstan::vb(bayes_logit_model,
data = train_dat,
output_samples = n_bootstrap,
seed = 123,
iter = 100)
stan_vb_sample <- rstan::extract(stan_vb)$beta
# Use these samples in ANPL with multiple cores
anpl_samples <- anpl(dataset = german,
concentration = 1,
n_bootstrap = n_bootstrap,
posterior_sample = stan_vb_sample,
threshold = 1e-8,
num_cores = 2)
col_means <- colMeans(anpl_samples)
print(col_means[21])
print(col_means[22])
expect_true((col_means[21] >= 0.29) && (col_means[21] <= 0.3),
"The average coefficient for column 21 is as expected")
expect_true((col_means[22] >= -0.15) && (col_means[22] <= -0.14),
"The average coefficient for column 22 is as expected")
})