[R] Sample function and prob argument
le Gleut, Ronan
ron@n@|eg|eut @end|ng |rom he|mho|tz-muenchen@de
Wed Jun 5 10:34:41 CEST 2019
Dear R-help mailing list,
First of all, many many thanks for your great work on the R project!
I have a very small issue regarding the sample function. Depending if we
specify values for the prob argument, we don't get the same result for a
random sampling with replacement and with equal probabilities. See the
attached R code for a minimal example with the R version 3.6.0.
With a previous R version (3.5.x), the result was just a permutation
between the possible realizations. They are now totally different with the
latest R version.
I understand that if we specify or not the prob argument, two different
internal functions are used: .Internal(sample()) or .Internal(sample2()).
Indeed, the algorithm used to draw a sample may not be the same if by
default we assume equal probabilities (without the prob argument) or if
the user defines himself the probabilities (even if they are equal).
I found this post on stackoverflow which explains the reasons of this
difference (answer by Matthew Lundberg):
I was wondering whether the solution proposed by PatrickT could solve this
issue? He proposed to have something like if(all.equal(prob, prob,
tolerance = .Machine$double.eps) prob = NULL inside the sample.int routine
in order to replicate prob=NULL with prob=rep(1, length(x)).
Thanks you in advance for your response.
Ronan Le Gleut
Helmholtz Zentrum Muenchen
Deutsches Forschungszentrum fuer Gesundheit und Umwelt (GmbH)
Ingolstaedter Landstr. 1
Stellv. Aufsichtsratsvorsitzender: MinDirig. Dr. Manfred Wolter
Geschaeftsfuehrung: Prof. Dr. med. Dr. h.c. Matthias Tschoep, Heinrich Bassler, Kerstin Guenther
Registergericht: Amtsgericht Muenchen HRB 6466
USt-IdNr: DE 129521671
More information about the R-help