Re: [R] "rbinom" not using probability of success right

From: Ted Harding <Ted.Harding_at_manchester.ac.uk>
Date: Wed, 28 May 2008 14:19:28 +0100 (BST)


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.



E-Mail: (Ted Harding) <Ted.Harding_at_manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861
Date: 28-May-08                                       Time: 14:19:24
------------------------------ XFMail ------------------------------

______________________________________________
R-help_at_r-project.org 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. Received on Wed 28 May 2008 - 15:22:17 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 28 May 2008 - 16:30:42 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive