Re: [R] simulating binary variables

About this list Date view Thread view Subject view Author view Attachment view

From: Adaikalavan Ramasamy (ramasamy@stats.ox.ac.uk)
Date: Sat 10 Aug 2002 - 01:16:23 EST


Message-id: <007501c23fb7$be3aba00$a61401a3@stats.ox.ac.uk>

As the previous mail, rbinom and rpois are your best bets for binomial and
discrete.
If you use runif or rnorm you will get continuos variables which you can
convert to discrete by using round(a*runif(100) +b) mod m [but you run the
risk of getting a cycle of numbers if you're not careful].

If you got a non-standard distribution you could use sample. For eg
sample(0:1, 5000, replace=T) produces the similiar result as binomial with
probability 0.5. More interestingly, say if you want to simulate n obs from
a distributon that place a an equal mass on the first 100 prime numbers, the

x <- c( 2, 3,5,7,11, 13, 17, 19 , ... ......... ,521, 523, 541 )
y <- sample( x, n, replace=T) and you can turn the replace =F is you want
sampling without replacement

> On 08/09/02 13:28, laura@bayesian-bay.freeserve.co.uk wrote:
>
> >I am wanting to simulate a data set consisting of a Y variable
> and several X variables, all either binary or discrete. I am
> wondering how to go about doing this and have failed to find
> anything about this in the R -help.

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._


About this list Date view Thread view Subject view Author view Attachment view

This archive was generated by hypermail 2.1.3 : Tue 01 Jun 2004 - 09:31:05 EST