[R] "rbinom" : Does randomness preclude precision?

Daniel Nordlund djnordlund at verizon.net
Wed May 28 18:00:12 CEST 2008


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of Philip Twumasi-Ankrah
> Sent: Wednesday, May 28, 2008 6:52 AM
> To: ted.harding at manchester.ac.uk
> Cc: r-help at r-project.org
> Subject: Re: [R] "rbinom" : Does randomness preclude precision?
> 
> Teds reply is a bit comforting and as indicated in my post, I am resorting to using
> "sample" but as an academic issue, does randomness preclude precision?

I would say yes, at least in the manner you seem to be thinking of it.  Think about it for a minute.  Say you use the following code

status <- sample(rep(c(0,1),c(425,75)))

You will get your 75 ones randomly distributed throughout the vector status.  What would you expect the value to be for

sum(status[1:100])

I suggest that you will see the same kind of variation in the sum as you would see for 

sum(rbinom(100, 1, p=.15))

So, you can randomly arrange a certain percentage of ones in a sequence, but you should not expect to see that exact percentage in any subset of the sequence.

One other example, if you flip a fair coin 100 times, do you expect to get exactly 50 heads?

Hope this is helpful,

Dan

Daniel Nordlund
Bothell, WA USA


> 
> Randomness should be in the sequence of zeros and ones and how they are simulated
> at each iteration of the process but not in the eventual nature of the distribution.
> 
> I mean if I simulated a Normal (0, 1) and got a Normal(1.5, 2) these would be very
> different distributions. It is the same with simulating a Binomial(1, p=0.15) and getting
> Binomial(1, 0.154)
> 
> Ted.Harding at manchester.ac.uk wrote: On 28-May-08 12:53:26, Philip Twumasi-
> Ankrah wrote:
> > I am trying to simulate a series of ones and zeros (1 or 0) and I am
> > using "rbinom" but realizing that the number of successes expected is
> > not accurate. Any advice out there.
> >
> > This is the example:
> >
> > N<-500
> > status<-rbinom(N, 1, prob = 0.15)
> > count<-sum(status)
> >
> > 15 percent of 500 should be 75 but what I obtain from the "count"
> > variable is 77 that gives the probability of success to be 0.154. Not
> > very good.
> 
> The difference (77 - 75 =2) is well within the likely sampling
> variation when 500 values are sampled independently with
> P(1)=0.15:
> 
> The standard deviation of the resulting number of 1s is
> sqrt(500*0.15*0.85) =  7.98, so the difference of 2 is only 1/4 of
> a standard deviation, hence very likely to be equalled or exceeded.
> 
> Your chance of getting exactly 75 by this method is quite small:
> 
>   dbinom(75,500,0.15)
>   [1] 0.04990852
> 
> and your chance of being 2 or more off your target is
> 
>   1 - sum(dbinom((74:76),500,0.15))
>   [1] 0.8510483
> 
> > Is there another way beyond using "sample" and "rep" together?
> 
> It looks as though you are seeking to obtain exactly 75 1s,
> randomly situated, the rest being 0s, so in effect you do need
> to do something on the lines of "sample" and "rep". Hence,
> something like
> 
>   status <- rep(0,500)
>   status[sample((1:500),75,replace=FALSE)] <- 1
> 
> Hoping this helps,
> Ted.



More information about the R-help mailing list