s-news
[Top] [All Lists]

Re: multiplication problem - what happens at 46341?

To: henrik.parn@bio.ntnu.no
Subject: Re: multiplication problem - what happens at 46341?
From: Sundar Dorai-Raj <sundar.dorai-raj@pdf.com>
Date: Fri, 03 Mar 2006 14:03:32 -0600
Cc: s-news@lists.biostat.wustl.edu
In-reply-to: <4408890B.60807@bio.ntnu.no>
Organization: PDF Solutions, Inc.
References: <4408890B.60807@bio.ntnu.no>
User-agent: Mozilla Thunderbird 1.0.2 (Windows/20050317)


Henrik Parn wrote:
Dear all,

Why can't calculate 46341*46341 using '*'? I must have missed something very basic...

 > 46340.99999*46340.99999
[1] 2147488280
 > 46341*46341
[1] NA
 > 4.6341E4
[1] 46341
 > 4.6341E4*4.6341E4
[1] 2147488281
 > 46341^2
[1] 2147488281

My function of for unbiased skew (Sokal & Rohlf 3ed p 115) involves a multiplication, and when my samples are 'big' the answer is just 'NA' due to the problem described above:

skew.sokal <- function(x){
   m3 <- length(x) * sum((x-mean(x))^3)/((length(x)-1)*(length(x)-2))
   s3 <- sqrt(var(x))^3
   m3/s3}

 > x <-  rnorm(46342)
 > skew.sokal(x)
[1] 0.0101318
 > x <-  rnorm(46342)
 > skew.sokal(x)
[1] -0.00613662
 > x <-  rnorm(46343)
 > skew.sokal(x)
[1] NA

Thanks in advance for any suggestion! And sorry if I have overseen something basic...
S-plus 6.2 Build 6713
WinXP


Hi, Henrik,

You've overlooked how big an integer S-PLUS can store as an integer. That's why:

46342.*46342

will work, but

46342*46342

won't. This is becuase the first forces the storage to double.

As for your function, length(x) returns a integer. So, try:

skew.sokal <- function(x) {
  n <- as.numeric(length(x))
  m3 <- n * sum((x - mean(x))^3)/((n - 1) * (n - 2))
  s3 <- sqrt(var(x))^3
  m3/s3
}

set.seed(42)
x <- rnorm(46343)
skew.sokal(x)

<Prev in Thread] Current Thread [Next in Thread>