0

I'm getting weird results when I try to simulate a bunch of normal variates. I have enough memory to hold these.

N <- 2000
h1 <- 1000 * (1 - 0.9)

length(rnorm(N*h1,0,1)) == N*h1
[1] FALSE

>length(rnorm(3,0,1)) == 3
[1] TRUE

I hate R

CURIOSER AND CURIOSER:

> N<-2000
> h1 <- 100
> length(rnorm(N*h1))
[1] 200000
> h1 <- 1000 * (1 - 0.9)
> length(rnorm(N*h1))
[1] 199999


> sessionInfo()
R version 3.2.3 (2015-12-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 14.04.3 LTS

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] colorout_1.1-1

loaded via a namespace (and not attached):
[1] tools_3.2.3

nothing in my .Rprofile except:

library(colorout)

wideScreen <- function(howWide=Sys.getenv("COLUMNS")) {
    options(width=as.integer(howWide))
}
bdeonovic
  • 4,130
  • 7
  • 40
  • 70
  • it does work well for me. – SabDeM Feb 12 '16 at 23:47
  • `length(rnorm(N*h1,0,1)) == N*h1` is TRUE on my R 3.2.3 – HubertL Feb 12 '16 at 23:47
  • **sound of computer crashing through window** how do I diagnose this? – bdeonovic Feb 12 '16 at 23:48
  • What is the value of `length(rnorm(N*h1,0,1))` on your computer (assuming you haven't tossed it out the window yet)? – eipi10 Feb 12 '16 at 23:51
  • Restart R, double check syntax, try again. If problem still persists, assign it `x = rnorm(N*h1, 0, 1))` and examine `str(x)`, `str(N)` , `str(h1)` for irregularities; post `sessionInfo()`. – Gregor Thomas Feb 12 '16 at 23:51
  • i recommend doing a `rm(list=ls())` then trying again (this removes all objects from your working memory, so save work first). You have probably overwritten something somewhere and don't realize it. – rbatt Feb 12 '16 at 23:52
  • I've already tried those things. It kept giving me one value less than it should (ie 199999) I'm just restarting my computer now – bdeonovic Feb 12 '16 at 23:54
  • does this help? `\`==\` <- \`!=\`; 1 == 2` – rawr Feb 12 '16 at 23:57
  • @rawr Clever, but he said he did `rm(list=ls())` and that the length was always 1 less than expected. But along lines of what you're suggesting, maybe there's a package installed that sinisterly redefined `length()`. I wonder if using `base::length(stats::rnorm(5,0,1))` gives the right length. If so, someone may have though it funny to install a custom package on your machine and have it loaded via your .RProfile. – rbatt Feb 13 '16 at 00:02
  • 1
    thanks for the suggestions, looks like it might be related to numerical precision? – bdeonovic Feb 13 '16 at 00:09
  • There doesn't seem to be a problem with the output in your updated code – rbatt Feb 13 '16 at 00:12
  • @rbatt are you serious...reread the code. – bdeonovic Feb 13 '16 at 00:14
  • In the sense I get the same output. `length((rnorm(as.integer(N*h1))))`, then `length((rnorm(ceiling(N*h1))))` give different lengths. – rbatt Feb 13 '16 at 00:20
  • Thanks @rawr I agree this seems to be a precision problem – bdeonovic Feb 13 '16 at 00:24
  • But that doesn't explain why your initial test pops out FALSE for you and TRUE for me and others ... the numerical precision seems separate, right? – rbatt Feb 13 '16 at 00:27
  • 2
    Original post was a bit misleading. ``h1`` was actually calculated as ``h1 <- 1000 * (1 - 0.9)`` – bdeonovic Feb 13 '16 at 00:29
  • So i understand what the problem is...but how do I fix it? – bdeonovic Feb 13 '16 at 00:30
  • I ended up using ``ceil``, but will the numbers always be guaranteed to be less than the actual integer I need? – bdeonovic Feb 13 '16 at 00:31
  • Yeah, it's definitely important that the example actually reproduces the problem. Just toss in an `as.integer`, because that's probably what R is doing when converting what you supply as `n` to an integer for the rng. – rbatt Feb 13 '16 at 00:32
  • as.integer doesn't work because ``as.integer(1000 * (1 - 0.9))`` gives ``99`` – bdeonovic Feb 13 '16 at 00:33

0 Answers0