Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between runif and sample in R?

In terms of probability distribution they use? I know that runif gives fractional numbers and sample gives whole numbers, but what I am interested in is if sample also use the 'uniform probability distribution'?

like image 793
Kushal Bansal Avatar asked Nov 17 '14 17:11

Kushal Bansal


2 Answers

Consider the following code and output:

> set.seed(1)
> round(runif(10,1,100))
 [1] 27 38 58 91 21 90 95 66 63  7
> set.seed(1)
> sample(1:100, 10, replace=TRUE)
 [1] 27 38 58 91 21 90 95 67 63  7

This strongly suggests that when asked to do the same thing, the 2 functions give pretty much the same output (though interestingly it is round that gives the same output rather than floor or ceiling). The main differences are in the defaults and if you don't change those defaults then both would give something called a uniform (though sample would be considered a discrete uniform and by default without replacement).

Edit

The more correct comparison is:

> ceiling(runif(10,0,100))
 [1] 27 38 58 91 21 90 95 67 63  7

instead of using round.

We can even step that up a notch:

> set.seed(1)
> tmp1 <- sample(1:100, 1000, replace=TRUE)
> set.seed(1)
> tmp2 <- ceiling(runif(1000,0,100))
> all.equal(tmp1,tmp2)
[1] TRUE

Of course if the probs argument to sample is used (with not all values equal), then it will no longer be uniform.

like image 118
Greg Snow Avatar answered Oct 13 '22 11:10

Greg Snow


sample samples from a fixed set of inputs, and if a length-1 input is passed as the first argument, returns an integer output(s).

On the other hand, runif returns a sample from a real-valued range.

 > sample(c(1,2,3), 1)
 [1] 2
 > runif(1, 1, 3)
 [1] 1.448551
like image 42
Robert Krzyzanowski Avatar answered Oct 13 '22 10:10

Robert Krzyzanowski