[R] How does the r-distribution function work

Peter Dalgaard p.dalgaard at biostat.ku.dk
Fri Jul 6 09:54:19 CEST 2007


pieter claassen wrote:
> I am trying to understand what rbinom function does.
>
> Here is some sample code. Are both the invocations of bfunc effectively
> doing the same or I am missing the point?
>
>   
There are some "newbie" issues with your code (you are extending a on 
every iteration, and your bfunc is just rbinom with the parameters in a 
different order), but basically, yes: They are conceptually the same. 
Both give 10000 independent binomial samples.

In fact, if you reset the random number generator in between, they also 
give the same results (this is an implementation issue and not obviously 
guaranteed for any distribution) . Here's an example with smaller values 
than 10000 and 30.

 > set.seed(123)
 > rbinom(10,1,.5)
 [1] 0 1 0 1 1 0 1 1 1 0

 > set.seed(123)
 > for (i in 1:10) print(rbinom(1,1,.5))
[1] 0
[1] 1
[1] 0
[1] 1
[1] 1
[1] 0
[1] 1
[1] 1
[1] 1
[1] 0

 > set.seed(123)
 > replicate(10, rbinom(1,1,.5))
 [1] 0 1 0 1 1 0 1 1 1 0


> Thanks,
> Pieter
>
> bfunc <- function(n1,p1,sims) {
> c<-rbinom(sims,n1,p1)
> c
> }
>
> a=c()
> b=c()
> p1=.5
> for (i in 1:10000){
> a[i]=bfunc(30,p1,1)
> }
> b=bfunc(30,p1,10000)
>
> ______________________________________________
> 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