Skip to content
Prev 22534 / 398502 Next

simulating binary variables

On 08/09/02 13:28, laura at bayesian-bay.freeserve.co.uk wrote:

            
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.

Try runif() in the base package.  It generates random numbers
from a uniform distribution.  So, for example, if you want a
binary variable with an expectation of .75, and 1000
observations, say:

runif(1000)<=.75

Or, if you want to see the numbers right away:

(runif(1000)<=.75)+0

To generate a factor with several levels, you can apply cut() to
runif().  That may be sufficient, but note that factors are
"categorical variables."

Of course, you assign these to variables, e.g.,

x1 <- runif(1000)<=.75

and then use these in your model.

Jon
Message-ID: <20020809102547.C4546@cattell.psych.upenn.edu>
In-Reply-To: <20020809132857.MOGG23487.fep08-svc.ttyl.com@localhost>; from laura@bayesian-bay.freeserve.co.uk on Fri, Aug 09, 2002 at 01:28:57PM +0000