[R] Question about random sampling in R
Francisco J. Zagmutt
gerifalte28 at hotmail.com
Thu Oct 19 20:38:47 CEST 2006
To add to the nice explanation by Marc, you can access the source directly
from the web at https://svn.r-project.org/R/trunk/src/main/random.c
If you prefer to look directly in the source tarball, notice the file is
called random.c
Francisco
Dr. Francisco J. Zagmutt
College of Veterinary Medicine and Biomedical Sciences
Colorado State University
>From: Marc Schwartz <MSchwartz at mn.rr.com>
>Reply-To: MSchwartz at mn.rr.com
>To: tom soyer <tom.soyer at gmail.com>
>CC: r-help <r-help at stat.math.ethz.ch>
>Subject: Re: [R] Question about random sampling in R
>Date: Thu, 19 Oct 2006 13:10:20 -0500
>
>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
>
>______________________________________________
>R-help at stat.math.ethz.ch 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.
More information about the R-help
mailing list