Chee Chen
2014-Jan-01 22:12 UTC
[R] Question: reproducibility of random sampling with replacement
Dear All, I would like to ask for your help on "reproducibility of random sampling with replacement". For example, one re-samples the rows with replacement of a residual matrix and uses the new residual matrix thus obtained to produce a statistic ; repeat this for a certain number of times. My questions: will the above produce ever be reproducible by setting a seed? Namely, Given the same residual matrix, Ted applies the above process and so does Jack, will they get the same results by setting a seed? My attempt: setting seed does not freeze the command "sample" from getting different samples, as from the codes: ===x= 1:20 S = matrix(0,5,20) for (i in 1:5) { S[i,] = sample(x, replace=FALSE) } set.seed(123) T = matrix(0,5,20) for (i in 1:5) { T[i,] = sample(x, replace=FALSE) } sum(S==T) == I would appreciate any comments and/or suggestions on this. Regards, Chee [[alternative HTML version deleted]]
Rolf Turner
2014-Jan-01 22:59 UTC
[R] Question: reproducibility of random sampling with replacement
Why on earth would you expect S and T to be the same given what you have done. "I am unable to rightly apprehend the confusion of ideas that could provoke such a question", (Charles Babbage). You have to set the *same* seed before each construction. I.e. do set.seed(123) before creating S; then do set.seed(123) again before creating T. If you do so, S and T will be identical. cheers, Rolf Turner P. S. "T" is not a good name for an object; too easy to confuse with "TRUE". Not an egregious sin, but to be avoided. R. T. On 02/01/14 11:12, Chee Chen wrote:> Dear All, > > I would like to ask for your help on "reproducibility of random sampling with replacement". For example, one re-samples the rows with replacement of a residual matrix and uses the new residual matrix thus obtained to produce a statistic ; repeat this for a certain number of times. > > My questions: will the above produce ever be reproducible by setting a seed? Namely, Given the same residual matrix, Ted applies the above process and so does Jack, will they get the same results by setting a seed? > > My attempt: setting seed does not freeze the command "sample" from getting different samples, as from the codes: > ===> x= 1:20 > S = matrix(0,5,20) > for (i in 1:5) { > S[i,] = sample(x, replace=FALSE) > } > > set.seed(123) > > T = matrix(0,5,20) > for (i in 1:5) { > T[i,] = sample(x, replace=FALSE) > } > sum(S==T) > ==> > I would appreciate any comments and/or suggestions on this.
Rui Barradas
2014-Jan-01 22:59 UTC
[R] Question: reproducibility of random sampling with replacement
Hello, Inline. Em 01-01-2014 22:12, Chee Chen escreveu:> Dear All, > > I would like to ask for your help on "reproducibility of random sampling with replacement". For example, one re-samples the rows with replacement of a residual matrix and uses the new residual matrix thus obtained to produce a statistic ; repeat this for a certain number of times. > > My questions: will the above produce ever be reproducible by setting a seed?Yes. Namely, Given the same residual matrix, Ted applies the above process and so does Jack, will they get the same results by setting a seed?> > My attempt: setting seed does not freeze the command "sample" from getting different samples,Yes it does, you are simply not setting the seed before the first for loop. Rui Barradas as from the codes:> ===> x= 1:20 > S = matrix(0,5,20) > for (i in 1:5) { > S[i,] = sample(x, replace=FALSE) > } > > set.seed(123) > > T = matrix(0,5,20) > for (i in 1:5) { > T[i,] = sample(x, replace=FALSE) > } > sum(S==T) > ==> > I would appreciate any comments and/or suggestions on this. > Regards, > Chee > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Jeff Newmiller
2014-Jan-01 23:01 UTC
[R] Question: reproducibility of random sampling with replacement
If you want to reproduce the same sequence twice, then you need to set the seed at the beginning of each calculation. You are only doing it for the second calculation below. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. Chee Chen <chee.chen at yahoo.com> wrote:>Dear All, > >I would like to ask for your help on "reproducibility of random >sampling with replacement". For example, one re-samples the rows with >replacement of a residual matrix and uses the new residual matrix thus >obtained to produce a statistic ; repeat this for a certain number of >times. > >My questions: will the above produce ever be reproducible by setting a >seed? Namely, Given the same residual matrix, Ted applies the above >process and so does Jack, will they get the same results by setting a >seed? > >My attempt: setting seed does not freeze the command "sample" from >getting different samples, as from the codes: >===>x= 1:20 >S = matrix(0,5,20) >for (i in 1:5) { > S[i,] = sample(x, replace=FALSE) >} > >set.seed(123) > >T = matrix(0,5,20) >for (i in 1:5) { > T[i,] = sample(x, replace=FALSE) >} >sum(S==T) >==> >I would appreciate any comments and/or suggestions on this. >Regards, >Chee > > [[alternative HTML version deleted]] > >______________________________________________ >R-help at r-project.org mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide >http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.
Bert Gunter
2014-Jan-01 23:08 UTC
[R] Question: reproducibility of random sampling with replacement
You have to set the same seed before each random number generation! You did not do this. Bert Gunter Genentech Nonclinical Biostatistics (650) 467-7374 "Data is not information. Information is not knowledge. And knowledge is certainly not wisdom." H. Gilbert Welch On Wed, Jan 1, 2014 at 2:12 PM, Chee Chen <chee.chen at yahoo.com> wrote:> Dear All, > > I would like to ask for your help on "reproducibility of random sampling with replacement". For example, one re-samples the rows with replacement of a residual matrix and uses the new residual matrix thus obtained to produce a statistic ; repeat this for a certain number of times. > > My questions: will the above produce ever be reproducible by setting a seed? Namely, Given the same residual matrix, Ted applies the above process and so does Jack, will they get the same results by setting a seed? > > My attempt: setting seed does not freeze the command "sample" from getting different samples, as from the codes: > ===> x= 1:20 > S = matrix(0,5,20) > for (i in 1:5) { > S[i,] = sample(x, replace=FALSE) > } > > set.seed(123) > > T = matrix(0,5,20) > for (i in 1:5) { > T[i,] = sample(x, replace=FALSE) > } > sum(S==T) > ==> > I would appreciate any comments and/or suggestions on this. > Regards, > Chee > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.