Skip to content

Commit

Permalink
Merge pull request #335 from insightsengineering/fix-stan-seed
Browse files Browse the repository at this point in the history
Fix stan seed
  • Loading branch information
gowerc authored Mar 18, 2022
2 parents 08e6dce + 8519613 commit f8f6f13
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 69 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: rbmi
Title: Reference Based Multiple Imputation
Version: 1.1.2
Version: 1.1.3
Authors@R: c(
person("Craig", "Gower-Page", email = "[email protected]", role = c("aut", "cre")),
person("Alessandro", "Noci", email = "[email protected]", role = c("aut")),
Expand Down
6 changes: 6 additions & 0 deletions R/mcmc.R
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ fit_mcmc <- function(
)
)

assert_that(
!is.na(seed),
!is.null(seed),
is.numeric(seed),
msg = "mcmc seed is invalid"
)
sampling_args$seed <- seed

stan_fit <- record({
Expand Down
2 changes: 1 addition & 1 deletion R/methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ method_bayes <- function(
burn_between = 50,
same_cov = TRUE,
n_samples = 20,
seed = NA
seed = sample.int(.Machine$integer.max, 1)
) {
x <- list(
burn_in = burn_in,
Expand Down
5 changes: 1 addition & 4 deletions cran-comments.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
## Resubmission
This is a resubmission. In this version I have:

* Stabilised some of our unit tests to reduce the occurrence of false-positive failures due to random variations from different CPU's/OS's.
* Added a bug report URL and a support site URL to our DESCRIPTION file
* Updated STAN code to hopefully fix the clang-UBSAN error. Please note though we were not able
to reproduce this CRAN error locally so are not 100% if this fix will work.
* Fixed the clang-UBSAN error

## R CMD check results

Expand Down
2 changes: 1 addition & 1 deletion man/method.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 37 additions & 1 deletion tests/testthat/test-mcmc.R
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,8 @@ test_that("fit_mcmc can recover known values with same_cov = TRUE", {
group = dat2$group,
subjid = dat2$id,
visit = dat2$visit,
method = method
method = method,
quiet = TRUE
)

beta_within <- get_within(fit$samples$beta, c(10, 6, 3, 7, 0, 0, 7, 14))
Expand Down Expand Up @@ -602,3 +603,38 @@ 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"
)
})
15 changes: 1 addition & 14 deletions vignettes/advanced.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,6 @@

<title>rbmi: Advanced Functionality</title>

<script>// Pandoc 2.9 adds attributes on both header and div. We remove the former (to
// be compatible with the behavior of Pandoc < 2.8).
document.addEventListener('DOMContentLoaded', function(e) {
var hs = document.querySelectorAll("div.section[class*='level'] > :first-child");
var i, h, a;
for (i = 0; i < hs.length; i++) {
h = hs[i];
if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6
a = h.attributes;
while (a.length > 0) h.removeAttribute(a[0].name);
}
});
</script>

<style type="text/css">
code{white-space: pre-wrap;}
Expand Down Expand Up @@ -787,7 +774,7 @@ <h1><span class="header-section-number">6</span> Custom imputation strategies</h
<span id="cb9-17"><a href="#cb9-17" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; pars &lt;- list(mu = mu, sigma = sigma)</span></span>
<span id="cb9-18"><a href="#cb9-18" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; return(pars)</span></span>
<span id="cb9-19"><a href="#cb9-19" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; }</span></span>
<span id="cb9-20"><a href="#cb9-20" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; &lt;bytecode: 0x7f9546ca2148&gt;</span></span>
<span id="cb9-20"><a href="#cb9-20" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; &lt;bytecode: 0x7ff3aeaeb098&gt;</span></span>
<span id="cb9-21"><a href="#cb9-21" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; &lt;environment: namespace:rbmi&gt;</span></span></code></pre></div>
<p>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.
Expand Down
55 changes: 21 additions & 34 deletions vignettes/quickstart.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,6 @@

<title>rbmi: Quickstart</title>

<script>// Pandoc 2.9 adds attributes on both header and div. We remove the former (to
// be compatible with the behavior of Pandoc < 2.8).
document.addEventListener('DOMContentLoaded', function(e) {
var hs = document.querySelectorAll("div.section[class*='level'] > :first-child");
var i, h, a;
for (i = 0; i < hs.length; i++) {
h = hs[i];
if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6
a = h.attributes;
while (a.length > 0) h.removeAttribute(a[0].name);
}
});
</script>

<style type="text/css">
code{white-space: pre-wrap;}
Expand Down Expand Up @@ -474,7 +461,7 @@ <h1><span class="header-section-number">3</span> Draws</h1>
<span id="cb3-71"><a href="#cb3-71" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; burn_between: 5</span></span>
<span id="cb3-72"><a href="#cb3-72" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; same_cov: TRUE</span></span>
<span id="cb3-73"><a href="#cb3-73" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; n_samples: 150</span></span>
<span id="cb3-74"><a href="#cb3-74" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; seed: NA</span></span></code></pre></div>
<span id="cb3-74"><a href="#cb3-74" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; seed: 373388384</span></span></code></pre></div>
<p>Note the use of <code>set_vars()</code> which specifies the names of the key variables
within the dataset and the imputation model. Additionally, note that whilst <code>vars$group</code> and <code>vars$visit</code>
are added as terms to the imputation model by default, their interaction is not,
Expand Down Expand Up @@ -699,34 +686,34 @@ <h1><span class="header-section-number">6</span> Pool</h1>
<span id="cb8-20"><a href="#cb8-20" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; trt_4 -0.092 0.683 -1.439 1.256 0.893 </span></span>
<span id="cb8-21"><a href="#cb8-21" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; lsm_ref_4 -1.616 0.486 -2.576 -0.656 0.001 </span></span>
<span id="cb8-22"><a href="#cb8-22" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; lsm_alt_4 -1.708 0.475 -2.645 -0.77 &lt;0.001 </span></span>
<span id="cb8-23"><a href="#cb8-23" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; trt_5 1.34 0.925 -0.486 3.166 0.149 </span></span>
<span id="cb8-24"><a href="#cb8-24" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; lsm_ref_5 -4.154 0.66 -5.457 -2.852 &lt;0.001 </span></span>
<span id="cb8-25"><a href="#cb8-25" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; lsm_alt_5 -2.815 0.647 -4.092 -1.538 &lt;0.001 </span></span>
<span id="cb8-26"><a href="#cb8-26" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; trt_6 1.956 0.999 -0.018 3.931 0.052 </span></span>
<span id="cb8-27"><a href="#cb8-27" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; lsm_ref_6 -6.1 0.72 -7.523 -4.678 &lt;0.001 </span></span>
<span id="cb8-28"><a href="#cb8-28" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; lsm_alt_6 -4.144 0.695 -5.516 -2.772 &lt;0.001 </span></span>
<span id="cb8-29"><a href="#cb8-29" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; trt_7 2.178 1.124 -0.043 4.399 0.055 </span></span>
<span id="cb8-30"><a href="#cb8-30" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; lsm_ref_7 -7.001 0.822 -8.628 -5.375 &lt;0.001 </span></span>
<span id="cb8-31"><a href="#cb8-31" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; lsm_alt_7 -4.823 0.789 -6.384 -3.263 &lt;0.001 </span></span>
<span id="cb8-23"><a href="#cb8-23" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; trt_5 1.317 0.931 -0.522 3.156 0.159 </span></span>
<span id="cb8-24"><a href="#cb8-24" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; lsm_ref_5 -4.141 0.661 -5.446 -2.836 &lt;0.001 </span></span>
<span id="cb8-25"><a href="#cb8-25" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; lsm_alt_5 -2.823 0.647 -4.102 -1.545 &lt;0.001 </span></span>
<span id="cb8-26"><a href="#cb8-26" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; trt_6 1.919 1.006 -0.069 3.907 0.058 </span></span>
<span id="cb8-27"><a href="#cb8-27" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; lsm_ref_6 -6.087 0.718 -7.505 -4.668 &lt;0.001 </span></span>
<span id="cb8-28"><a href="#cb8-28" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; lsm_alt_6 -4.168 0.703 -5.558 -2.778 &lt;0.001 </span></span>
<span id="cb8-29"><a href="#cb8-29" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; trt_7 2.132 1.133 -0.107 4.372 0.062 </span></span>
<span id="cb8-30"><a href="#cb8-30" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; lsm_ref_7 -6.945 0.837 -8.602 -5.288 &lt;0.001 </span></span>
<span id="cb8-31"><a href="#cb8-31" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; lsm_alt_7 -4.813 0.789 -6.373 -3.253 &lt;0.001 </span></span>
<span id="cb8-32"><a href="#cb8-32" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; --------------------------------------------------</span></span></code></pre></div>
<p>The table of values shown in the print message for <code>poolObj</code> can also be extracted using the <code>as.data.frame()</code> function:</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a><span class="fu">as.data.frame</span>(poolObj)</span>
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; parameter est se lci uci pval</span></span>
<span id="cb9-3"><a href="#cb9-3" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 1 trt_4 -0.09180645 0.6826279 -1.43949684 1.2558839 8.931772e-01</span></span>
<span id="cb9-4"><a href="#cb9-4" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 2 lsm_ref_4 -1.61581996 0.4862316 -2.57577141 -0.6558685 1.093708e-03</span></span>
<span id="cb9-5"><a href="#cb9-5" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 3 lsm_alt_4 -1.70762640 0.4749573 -2.64531931 -0.7699335 4.262148e-04</span></span>
<span id="cb9-6"><a href="#cb9-6" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 4 trt_5 1.33975256 0.9245536 -0.48627442 3.1657795 1.492897e-01</span></span>
<span id="cb9-7"><a href="#cb9-7" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 5 lsm_ref_5 -4.15439612 0.6596139 -5.45719295 -2.8515993 2.853288e-09</span></span>
<span id="cb9-8"><a href="#cb9-8" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 6 lsm_alt_5 -2.81464355 0.6465855 -4.09178487 -1.5375022 2.412198e-05</span></span>
<span id="cb9-9"><a href="#cb9-9" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 7 trt_6 1.95633775 0.9994792 -0.01823149 3.9309070 5.212617e-02</span></span>
<span id="cb9-10"><a href="#cb9-10" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 8 lsm_ref_6 -6.10037936 0.7197954 -7.52269771 -4.6780610 2.097471e-14</span></span>
<span id="cb9-11"><a href="#cb9-11" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 9 lsm_alt_6 -4.14404161 0.6946859 -5.51643551 -2.7716477 1.625003e-08</span></span>
<span id="cb9-12"><a href="#cb9-12" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 10 trt_7 2.17794477 1.1237356 -0.04320821 4.3990977 5.456450e-02</span></span>
<span id="cb9-13"><a href="#cb9-13" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 11 lsm_ref_7 -7.00142993 0.8223970 -8.62790047 -5.3749594 2.911670e-14</span></span>
<span id="cb9-14"><a href="#cb9-14" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 12 lsm_alt_7 -4.82348516 0.7893734 -6.38405217 -3.2629182 9.210812e-09</span></span></code></pre></div>
<span id="cb9-6"><a href="#cb9-6" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 4 trt_5 1.31716908 0.9309119 -0.52157744 3.1559156 1.590740e-01</span></span>
<span id="cb9-7"><a href="#cb9-7" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 5 lsm_ref_5 -4.14060567 0.6607424 -5.44563157 -2.8355798 3.357886e-09</span></span>
<span id="cb9-8"><a href="#cb9-8" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 6 lsm_alt_5 -2.82343659 0.6473700 -4.10211675 -1.5447564 2.330587e-05</span></span>
<span id="cb9-9"><a href="#cb9-9" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 7 trt_6 1.91890493 1.0061776 -0.06905222 3.9068621 5.839789e-02</span></span>
<span id="cb9-10"><a href="#cb9-10" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 8 lsm_ref_6 -6.08671772 0.7179474 -7.50524961 -4.6681858 1.941623e-14</span></span>
<span id="cb9-11"><a href="#cb9-11" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 9 lsm_alt_6 -4.16781280 0.7033519 -5.55758309 -2.7780425 2.053928e-08</span></span>
<span id="cb9-12"><a href="#cb9-12" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 10 trt_7 2.13242993 1.1328896 -0.10700795 4.3718678 6.183489e-02</span></span>
<span id="cb9-13"><a href="#cb9-13" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 11 lsm_ref_7 -6.94503554 0.8373644 -8.60163962 -5.2884315 1.191826e-13</span></span>
<span id="cb9-14"><a href="#cb9-14" aria-hidden="true" tabindex="-1"></a><span class="co">#&gt; 12 lsm_alt_7 -4.81260561 0.7891469 -6.37258775 -3.2526235 9.589554e-09</span></span></code></pre></div>
<p>These outputs gives an estimated difference of
2.178 (95% CI -0.043 to 4.399)
between the two groups at the last visit with an associated p-value of 0.055.</p>
2.132 (95% CI -0.107 to 4.372)
between the two groups at the last visit with an associated p-value of 0.062.</p>
</div>
<div id="code" class="section level1" number="7">
<h1><span class="header-section-number">7</span> Code</h1>
Expand Down
13 changes: 0 additions & 13 deletions vignettes/stat_specs.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,6 @@

<title>rbmi: Statistical Specifications</title>

<script>// Pandoc 2.9 adds attributes on both header and div. We remove the former (to
// be compatible with the behavior of Pandoc < 2.8).
document.addEventListener('DOMContentLoaded', function(e) {
var hs = document.querySelectorAll("div.section[class*='level'] > :first-child");
var i, h, a;
for (i = 0; i < hs.length; i++) {
h = hs[i];
if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6
a = h.attributes;
while (a.length > 0) h.removeAttribute(a[0].name);
}
});
</script>

<style type="text/css">
code{white-space: pre-wrap;}
Expand Down

0 comments on commit f8f6f13

Please sign in to comment.