[R] Question about random sampling in R

Marc Schwartz MSchwartz at mn.rr.com
Thu Oct 19 20:10:20 CEST 2006


On Thu, 2006-10-19 at 12:07 -0500, tom soyer wrote:
> Hi,
> 
> I looked up the help file on sample(), but didn't find the info I was
> looking for.
> 
> When sample() is used to resample from a distribution, e.g., bootstrap, how
> does it do it? Does it use an uniform distribution, e.g., runif(), or
> something else? And, when the help file says:"sample(x) generates a random
> permutation of the elements of x (or 1:x)", would I be correct if I
> translate the statement as follows: it means that the order of
> sequence, which was generated from a uniform distribution, would look like a
> random normal distribution.
> 
> Thanks,
> 
> Tom

In the simplest case, where you have not specified a set of probability
weights, sample() uses a uniform distribution, such that each element
has an equal probability of being selected.

In the case of sampling WITHOUT replacement (the default), each element
in the vector has an equal probability of being selected. Once selected,
that element is removed from the sampling space and the process is
repeated with the remaining elements until all elements have been
selected.

So:

> sample(10)
 [1]  3  8  5  9  7  1  4  2 10  6

yields a random permutation of 1:10.

In the case of 'replace = TRUE', which is sampling WITH replacement,
after an element is selected it is retained in the sampling space, thus
can be selected multiple times.

So:

> sample(10, replace = TRUE)
 [1] 1 4 1 8 7 8 6 7 5 9


If you specify a set of probability weights from the sampling vector,
then the probability for each element in being selected is affected
accordingly.

In the case of bootstrapping, sampling WITH replacement is used. You
might find the following post helpful in this scenario:

  http://finzi.psych.upenn.edu/R/Rhelp02a/archive/67421.html


If you want to investigate further, you can review the C source code for
the relevant R functions in random.c in the R source tarball. The file
will be in ../src/main.

HTH,

Marc Schwartz



More information about the R-help mailing list