Philip Twumasi-Ankrah
2008-May-28 12:53 UTC
[R] "rbinom" not using probability of success right
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. Is there another way beyond using "sample" and "rep" together? A Smile costs Nothing But Rewards Everything Happiness is not perfected until it is shared -Jane Porter [[alternative HTML version deleted]]
Charles Annis, P.E.
2008-May-28 13:06 UTC
[R] "rbinom" not using probability of success right
Do it again. What did you get this time? Then do it another time. Do you see what is happening? Charles Annis, P.E. Charles.Annis at StatisticalEngineering.com phone: 561-352-9699 eFax: 614-455-3265 http://www.StatisticalEngineering.com -----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 8:53 AM To: r-help at r-project.org Subject: [R] "rbinom" not using probability of success right 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. Is there another way beyond using "sample" and "rep" together? A Smile costs Nothing But Rewards Everything Happiness is not perfected until it is shared -Jane Porter [[alternative HTML version deleted]] ______________________________________________ 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.
Prof Brian Ripley
2008-May-28 13:18 UTC
[R] "rbinom" not using probability of success right
You asked for each of 500 to be included with probability 0.15, not for 15% of 500. If you want the latter, use sample, e.g. sample(c(rep(1,75), rep(0,425))) And to see if your 77 is reasonable for binomial sampling:> binom.test(77, 500, 0.15)Exact binomial test data: 77 and 500 number of successes = 77, number of trials = 500, p-value = 0.8022 alternative hypothesis: true probability of success is not equal to 0.15 95 percent confidence interval: 0.1234860 0.1886725 sample estimates: probability of success 0.154 so it certainly is. On Wed, 28 May 2008, 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. > > Is there another way beyond using "sample" and "rep" together?[...] -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
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 ------------------------------
Charles Annis, P.E.
2008-May-28 14:48 UTC
[R] "rbinom" : Does randomness preclude precision?
I think I see the rub: You would like to see the distribution of a sample be identical to the distribution from which it was sampled. But if it is random then that can happen only in the long run, not on every sample. That is why samples from a normal density are *not* themselves normal - they're "t." When the sample size is large enough the differences between a random sample's density and its parent density become vanishingly small. Thus the differences you observe from repeated random samples from the binomial. Repeated sampling produces slightly different numbers of successes. How could it be otherwise? Charles Annis, P.E. Charles.Annis at StatisticalEngineering.com phone: 561-352-9699 eFax:? 614-455-3265 http://www.StatisticalEngineering.com ? ________________________________________ From: Philip Twumasi-Ankrah [mailto:nana_kwadwo_derkyi at yahoo.com] Sent: Wednesday, May 28, 2008 10:36 AM To: Charles.Annis at StatisticalEngineering.com Subject: RE: [R] "rbinom" : Does randomness preclude precision? Charles, When you simulate data from a distribution, what you effect are doing is generating a sequence of values that would correspond to that distribution. So you can generate 1000 values from a normal distribution and expect that when you check on the distribution of your sample (what you do with your qqnorm or Q-Q plot), it should be a close fit with the theoretical distribution with the assigned parameter values. It will be difficult to explain why a simulated data may be different from the distribution it is was generated from . I think you can not blame it on randomness. I hope you understand what I am trying to determine. "Charles Annis, P.E." <Charles.Annis at StatisticalEngineering.com> wrote: What do you mean by "... *eventual* nature of the distribution?" If you simulated 100 samples, would you expect to see 1.5 successes? Or 1? Or 2? How many, in your thinking, is "eventual?" Charles Annis, P.E. Charles.Annis at StatisticalEngineering.com phone: 561-352-9699 eFax: 614-455-3265 http://www.StatisticalEngineering.com -----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 9: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? 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. -------------------------------------------------------------------- E-Mail: (Ted Harding) Fax-to-email: +44 (0)870 094 0861 Date: 28-May-08 Time: 14:19:24 ------------------------------ XFMail ------------------------------ A Smile costs Nothing But Rewards Everything Happiness is not perfected until it is shared -Jane Porter [[alternative HTML version deleted]] ______________________________________________ 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. A Smile costs Nothing? ????But Rewards Everything Happiness is not perfected until it is shared ????????????????????????????????????????????????????????????? -Jane Porter ?
Message: 24 Date: Wed, 28 May 2008 05:53:26 -0700 (PDT) From: Philip Twumasi-Ankrah <nana_kwadwo_derkyi at yahoo.com> Subject: [R] "rbinom" not using probability of success right To: r-help at r-project.org Message-ID: <374543.31047.qm at web39505.mail.mud.yahoo.com> Content-Type: text/plain 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. Is there another way beyond using "sample" and "rep" together? I understand you correctly you want there to be exactly 75 ones. If this is what you are trying to do then using pseudorandom variables is the incorrect way of going about it. Your suggestion of sample(c(rep (0,545),rep(1,75))) seems to me to be the best way of going about it since conceptually this is what you are doing: taking permutation of a fixed set of numbers. Best, Kyle