Hi, I need to create a function which generates a Binomial random number without using the rbinom function. Do I need to use the choose function or am I better just using a sample? Thanks. -- View this message in context: http://r.789695.n4.nabble.com/Binomial-tp3516778p3516778.html Sent from the R help mailing list archive at Nabble.com.
Binomial
7 messages · blutack, (Ted Harding), David Winsemius +2 more
Am 12.05.2011 10:46, schrieb blutack:
Hi, I need to create a function which generates a Binomial random number without using the rbinom function. Do I need to use the choose function or am I better just using a sample? Thanks.
I think I remember other software who generates binomial data with e.g. pi=0.7 by pi <- 0.7 x <- runif(100)>pi summary(x) -- Alex
On 12-May-11 09:02:45, Alexander Engelhardt wrote:
Am 12.05.2011 10:46, schrieb blutack:
Hi, I need to create a function which generates a Binomial random number without using the rbinom function. Do I need to use the choose function or am I better just using a sample? Thanks.
I think I remember other software who generates binomial data with e.g. pi=0.7 by pi <- 0.7 x <- runif(100)>pi summary(x) -- Alex
That needs to be the other way round (and perhaps also convert it to 0/1): x <- 1*(runif(100) < pi) since Prob(runif > pi) = (1 - pi). Comparison: pi <- 0.7 x <- runif(100)>pi x[1:10] # [1] FALSE TRUE TRUE FALSE FALSE TRUE TRUE TRUE FALSE FALSE sum(x)/100 # [1] 0.36 x <- 1*(runif(100) < pi) x[1:10] # [1] 0 0 1 1 1 1 1 0 1 0 sum(x)/100 # [1] 0.62 Ted -------------------------------------------------------------------- E-Mail: (Ted Harding) <ted.harding at wlandres.net> Fax-to-email: +44 (0)870 094 0861 Date: 12-May-11 Time: 10:21:26 ------------------------------ XFMail ------------------------------
On May 12, 2011, at 5:02 AM, Alexander Engelhardt wrote:
Am 12.05.2011 10:46, schrieb blutack:
Hi, I need to create a function which generates a Binomial random number without using the rbinom function. Do I need to use the choose function or am I better just using a sample? Thanks.
I think I remember other software who generates binomial data with e.g. pi=0.7 by pi <- 0.7
I hope Allan knows this and is just being humorous here, but for the less experienced in the audience ... Choosing a different threshold variable name might be less error prone. `pi` is one of few built-in constants in R and there may be code that depends on that fact. > pi [1] 3.141593 > pi <- 0.7 > pi [1] 0.7 > rm(pi) > pi [1] 3.141593
x <- runif(100)>pi summary(x)
Another method would be: x <- sample(c(0,1) , 100, replace=TRUE, prob=c(0.7, 0.3) )
David Winsemius, MD West Hartford, CT
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110512/3eec51a0/attachment.pl>
Am 12.05.2011 13:19, schrieb Sarah Sanchez:
Dear R helpers, I am raising one query regarding this "Binomial" thread with the sole intention of learning something more as I understand R forum is an ocean of knowledge. I was going through all the responses, but wondered that original query was about generating Binomial random numbers while what the R code produced so far generates the Bernoulli Random no.s i.e. 0 and 1. True Binomial distribution is nothing but no of Bernoulli trials. As I said I am a moron and don't understand much about Statistics. Just couldn't stop from asking my stupid question.
Oh, yes. You can generate one B(20,0.7)-distributed random varible by summing up the like this: > pie <- 0.7 > x <- runif(20) > x [1] 0.83108099 0.72843379 0.08862017 0.78477878 0.69230873 0.11229410 [7] 0.64483435 0.87748373 0.17448824 0.43549622 0.30374272 0.76274317 [13] 0.34832376 0.20876835 0.85280612 0.93810355 0.65720548 0.05557451 [19] 0.88041390 0.68938009 > x <- runif(20) < pie > x [1] FALSE FALSE TRUE FALSE TRUE TRUE TRUE TRUE FALSE TRUE FALSE TRUE [13] FALSE FALSE TRUE TRUE FALSE TRUE FALSE FALSE > sum(x) [1] 10 You could shorten this to > sum(runif(20)<0.7) [1] 12 Which would be the same as > rbinom(1,20,0.5) [1] 6 or even > qbinom(runif(1),20,0.5) [1] 12 Just play around a little, and learn from the help files: > ?rbinom Have fun!
--- On Thu, 5/12/11, David Winsemius<dwinsemius at comcast.net> wrote: From: David Winsemius<dwinsemius at comcast.net> Subject: Re: [R] Binomial To: "Alexander Engelhardt"<alex at chaotic-neutral.de> Cc: r-help at r-project.org, "blutack"<x-jess-h-x at hotmail.co.uk> Date: Thursday, May 12, 2011, 11:08 AM
I hope Allan knows this and is just being humorous here, but for the less experienced in the audience ... Choosing a different threshold variable name might be less error prone. `pi` is one of few built-in constants in R and there may be code that depends on that fact.
pi
[1] 3.141593
He didn't, or better, he forgot. Also, that Allan isn't related to me (I think) :) - Alex
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110512/6d2092b6/attachment.pl>