<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body style='font-size: 10pt; font-family: Verdana,Geneva,sans-serif'>
<p>Sorry, I missed sharing that part comparing the two functions, vector vs. matrix without the regression.</p>
<p>// [[Rcpp::export]]<br />NumericMatrix define_e(int iter, NumericMatrix obs_mat, NumericVector draws) {<br />  Rcpp::NumericVector d = obs_mat(_, 1);<br />  Rcpp::NumericVector pr = no_init(d.length());<br />  Rcpp::NumericMatrix e(d.length(), iter);<br />  for (int i = 0; i < iter; i++) {<br />    pr = obs_mat(_, 0) * draws(i);<br />    e(_, i) = cpprbinom(d.length(), 1, pr);<br />  }<br />  return e;<br />}<br /><br />// [[Rcpp::export]]<br />NumericMatrix define_e1(int iter, NumericMatrix obs_mat, NumericVector draws) {<br />  Rcpp::NumericVector d = obs_mat(_, 1);<br />  Rcpp::NumericVector pr = no_init(d.length());<br />  Rcpp::NumericVector e = no_init(d.length());<br />  Rcpp::NumericMatrix e_mat(d.length(), iter);<br /><br />  for (int i = 0; i < iter; i++) {<br />    pr = obs_mat(_, 0) * draws(i);<br />    e = cpprbinom(d.length(), 1, pr);<br />    e_mat(_, i) = e; // save e as matrix for sake of demonstration<br />  }<br />  return e_mat;<br />}</p>
<p>So these two give the same output:</p>
<p>> draws <- rbeta(200, 100, 60)<br />> obs_mat <- cbind(c(rep(1, 40), rep(0, 60)), c(rep(1, 20), rep(0, 80)))<br />> iter <- length(draws)<br />> set.seed(1234)<br />> t0 <- define_e(iter, obs_mat, draws)<br />> set.seed(1234)<br />> t1 <- define_e1(iter, obs_mat, draws)<br />> all.equal(t0, t1)<br />[1] TRUE</p>
<p>But adding the regression to define_e1() (as define_e2() in previous email) does not give the same. The first iteration in the loop is identical in both, but not the subsequent ones. That's why I was thinking it could be linked to the seed, but (sorry for my limited knowledge of C++) it would be strange to manually "re-seed" at each iteration. Moreover, I believe the seed set in R is transferred to Rcpp and so does not need to be taken specifically care of.</p>
<p>Denis</p>
<p><br /></p>
<p><br /></p>
<p><br /></p>
<div id="signature"></div>
<p><br /></p>
<p id="reply-intro">Le 2024-10-01 11:14, Dirk Eddelbuettel a écrit :</p>
<blockquote type="cite" style="padding: 0 0.4em; border-left: #1010ff 2px solid; margin: 0">
<div class="pre" style="margin: 0; padding: 0; font-family: monospace"><br />On 1 October 2024 at 09:59, Dirk Eddelbuettel wrote:<br />| This is not reproducible. You show two functions, they both take arguments,<br />| you do not supply those. We cannot run this, so we cannot really help you.<br /><br />I missed the lines at the bottom past your signature, so I was wrong. My bad.<br /><br />I would still encourage you to _simplify_. You have a pair of moderately<br />complex functions, and they do not reproduce the same results after two runs<br />despite re-seeding. So I would trim elements from them one by one to see<br />which removal leads to identical results. Or, inversely, start from twice<br />drawing from an RNG after reseed and assert first that the draws are<br />identical. Then add your processing.<br /><br />Dirk</div>
</blockquote>
</body></html>