Hello! I was wanting to find out which random seed could generate the characters in the word "love", for example, when I sample with replacement from the vector of the letters. So I've written the code below: seed=0 set.seed=0 x<-sample(letters,4,replace=T) while (sum(x==c("l","o","v","e"))<4){ seed<-seed+1 set.seed=seed x<-sample(letters,4,replace=T) } When I run this code I always get that the final x vector is c("l","o","v","e"), but afterwards, when I try to set.seed iqual to the value stored in object "seed" I can never reproduce this vector c("l","o","v","e"), by doing sample(letters,4,replace=T). Why is that? Thank you very much, Isabel Natario -- Dep. Matem?tica Faculdade de Ci?ncias e Tecnologia Universidade Nova de Lisboa 2829-516 Caparica, Portugal icn at fct.unl.pt [[alternative HTML version deleted]]
Thierry Onkelinx
2015-Apr-13 10:12 UTC
[R] Finding which seed resulted in a specific sample
you need set.seed(seed) instead of set.seed = seed ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance Kliniekstraat 25 1070 Anderlecht Belgium To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher The plural of anecdote is not data. ~ Roger Brinner The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey 2015-04-13 11:12 GMT+02:00 Isabel Natario <icn at fct.unl.pt>:> Hello! > > I was wanting to find out which random seed could generate the characters > in the word "love", for example, when I sample with replacement from the > vector of the letters. So I've written the code below: > > seed=0 > set.seed=0 > x<-sample(letters,4,replace=T) > while (sum(x==c("l","o","v","e"))<4){ > seed<-seed+1 > set.seed=seed > x<-sample(letters,4,replace=T) > } > > When I run this code I always get that the final x vector is > c("l","o","v","e"), but afterwards, when I try to set.seed iqual to the > value stored in object "seed" I can never reproduce this vector > c("l","o","v","e"), by doing sample(letters,4,replace=T). Why is that? > > Thank you very much, > > Isabel Natario > -- > Dep. Matem?tica > Faculdade de Ci?ncias e Tecnologia > Universidade Nova de Lisboa > 2829-516 Caparica, Portugal > icn at fct.unl.pt > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.[[alternative HTML version deleted]]
Prof Brian Ripley
2015-Apr-13 10:25 UTC
[R] Finding which seed resulted in a specific sample
On 13/04/2015 10:12, Isabel Natario wrote:> Hello! > > I was wanting to find out which random seed could generate the characters > in the word "love", for example, when I sample with replacement from the > vector of the letters. So I've written the code below: > > seed=0 > set.seed=0 > x<-sample(letters,4,replace=T) > while (sum(x==c("l","o","v","e"))<4){ > seed<-seed+1 > set.seed=seed > x<-sample(letters,4,replace=T) > } > > When I run this code I always get that the final x vector is > c("l","o","v","e"), but afterwards, when I try to set.seed iqual to the > value stored in object "seed" I can never reproduce this vector > c("l","o","v","e"), by doing sample(letters,4,replace=T). Why is that?Because you never called the function set.seed(): assigning to a variable of that name does not help. Also, there are far more values of the underlying seed than can be set using set.seed(), so this approach will not always work. However seed <- 0L set.seed(seed) x <- sample(letters,4L,replace=T) while (sum(x == c("l","o","v","e"))<4L){ # I would have used identical() seed <- seed+1L set.seed(seed) x <- sample(letters,4L,replace=T) } seed gave me an answer (543867)> > Thank you very much, > > Isabel Natario > -- > Dep. Matem?tica > Faculdade de Ci?ncias e Tecnologia > Universidade Nova de Lisboa > 2829-516 Caparica, Portugal > icn at fct.unl.pt-- Brian D. Ripley, ripley at stats.ox.ac.uk Emeritus Professor of Applied Statistics, University of Oxford 1 South Parks Road, Oxford OX1 3TG, UK