[R] floor of n observations in number generators

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

From: Marcus Davy (MDavy@hortresearch.co.nz)
Date: Wed 31 Dec 2003 - 09:30:03 EST


Message-id: <sff2b35b.085@hrp3.palm.cri.nz>

I couldnt find a previous posting on this in the archives, maybe it has
already been mentioned.

If you use a calculation to generate n observations in random number
generators and you don't round to the nearest integer you may be
generating n-1 numbers not n numbers as you thought depending on the
storage precision of the calculation.

e.g.
> m <- 1000
> pi0 <- 0.9
> length(rnorm(m * (1-pi0)))
[1] 99 # Should be 100
> options(digits=16)
> m * (1-pi0)
[1] 99.99999999999997
> identical(m*(1-pi0), 100)
[1] FALSE

Random number generation generates the floor of n observations, this
feature occurs on R-1.8.1 on linux Redhat8, and winXP (also on Unix
SPlus 3.4)
for probably all of the random number generators.

e.g.
> length(rnorm(m*(1-pi0),mean=0,sd=1))
[1] 99
> length(rpois(m*(1-pi0),lambda=1))
[1] 99
> length(rbeta(m*(1-pi0),shape=1, shape2=2))
[1] 99
> length(rbinom(m*(1-pi0),size=1, prob=0.5))
[1] 99
> length(runif(m*(1-pi0),min=0, max=1))
[1] 99

marcus

Marcus Davy
Bioinformatics

______________________________________________________
The contents of this e-mail are privileged and/or confidenti...{{dropped}}

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help


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

This archive was generated by hypermail 2.1.3 : Thu 01 Jan 2004 - 09:21:04 EST