[R] Question about random sampling in R

Alberto Monteiro albmont at centroin.com.br
Thu Oct 19 21:53:35 CEST 2006


Tom Soyer wrote:
> 
> 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.
>
I think it's clear that sample (without repetition) simulates
what you would get if you wrote every element in a card, shuffled
the card, and extracted a sample.

In other words, take some number n, another m <= n, 
let x <- 1:n and then simulate y <- sample(x, m). If you
do it many times, y[1] (or y[2], or y[m]) will have the
discrete distribution given by Probability(y[1] = 1) = 1/n,
Prob(y[1] = 2) = 1/n, ..., Prob(y[1] = n) = 1/n. The same,
of course, is valid for y[2], etc.

Ok, too much talking, let's run an example:

  x <- 1:10
  y3.hist <- NULL
  for (i in 1:10000) {
    y <- sample(x, 5)
    y3.hist[i] <- y[3]
  }
  hist(y3.hist)

Alberto Monteiro



More information about the R-help mailing list