[R] Matrix multiplication gives different results some of the times (NaN)

Joshua Wiley jwiley.psych at gmail.com
Tue Sep 13 09:54:02 CEST 2011


I cannot replicate this on, even with 1 milllion reps on:

R version 2.13.1 (2011-07-08)
Platform: i386-pc-mingw32/i386 (32-bit)

or

R Under development (unstable) (2011-08-13 r56733)
Platform: x86_64-pc-mingw32/x64 (64-bit)

Perhaps it is OS dependent (I am away from my linux box at the moment)?

Josh

2011/9/13 Xavier Fernández i Marín <xfim.ll at gmail.com>:
> Dear all,
>
> When trying to multiply the same matrices repeatedly I get different
> results some of the times.
>
> It is not systematic, which is the fact that is giving me more trouble to
> try to isolate the problem.
>
> Basically I am doing X %*% B, where X is a Nx2 matrix and B is a vector
> with 2 values. I have the impression (by exploration) that when N<41 the
> problem appears randomly, whereas for for N<41, it has not appeared any
> time.
>
>
> When I run a loop with, say, 1000 times doing the same operation, some of
> the times the resulting matrix has some "NaN" into it.
>
>
> Here it goes some code that fails:
>
> -----8<---------------
> B <- c(0, 4.9039)
>
> X <-
> structure(c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
> 1, 1, 1, 1, 1, 1, 1, -0.171208033350785, 2.05470005817641, -2.41795651834092,
> -0.258302488807007, -2.33857441602606, 0.628992894509171, 3.17491552278606,
> -1.44233071945946, 3.15785975396147, -1.99816867735392, 2.86344423987462,
> -5.271325440284, -2.0312296185407, -3.60192700841448, 3.38189039590051,
> -3.24639622163501, 0.284327647745153, -0.262087980902988, -3.7659454179588,
> 0.684967001574538, 3.0772201853168, 5.17600286245534, 1.04626227787207,
> 3.70446276942422, -0.0396039716160263, -0.657289365686855, -1.05808995826547,
> -1.93203623162131, -0.0884561017666723, -2.93790617275895, -0.215434948253912,
> 4.98439842853693, 2.91447973684786, -1.29993065167532, -4.66018971623614,
> -6.64275130324818, -5.21531940416414, 0.479257350317855, -1.96095587405884,
> 1.08909073077384, 1.28904309721095, 1.11327650673847, -0.022977620181631,
> 3.29959801979137, -2.23876825868105), .Dim = c(45L, 2L), .Dimnames = list(
>    NULL, c("", "x")))
>
>
> for (i in 1:1000) {
>  M <- X %*% B
>  w <- which(is.nan(M))
>  l <- length(w)
>  if (l!=0) {
>    print(paste("Length", l, "error(s) on", X[w,2], sep=" "))
>  }
> }
>
> -----8<---------------
>
> It gives several errors with variable length and on similar positions.
>
>
>
>
>
> I am using R-2.13.1 under Gentoo Linux
> -----8<---------------
>> version
> platform       x86_64-pc-linux-gnu
> arch           x86_64
> os             linux-gnu
> system         x86_64, linux-gnu
> status
> major          2
> minor          13.1
> year           2011
> month          07
> day            08
> svn rev        56322
> language       R
> version.string R version 2.13.1 (2011-07-08)
> -----8<---------------
>
> -----8<---------------
>> sessionInfo()
> R version 2.13.1 (2011-07-08)
> Platform: x86_64-pc-linux-gnu (64-bit)
>
> locale:
>  [1] LC_CTYPE=ca_AD.UTF-8       LC_NUMERIC=C               LC_TIME=ca_AD.UTF-8
>  [4] LC_COLLATE=C               LC_MONETARY=C              LC_MESSAGES=ca_AD.UTF-8
>  [7] LC_PAPER=ca_AD.UTF-8       LC_NAME=C                  LC_ADDRESS=C
>  [10] LC_TELEPHONE=C            LC_MEASUREMENT=ca_AD.UTF-8 LC_IDENTIFICATION=C
>
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
>
> loaded via a namespace (and not attached):
> [1] tools_2.13.1
>
> -----8<---------------
>
> Thank you,
>
> --
> -  Xavier  -
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
Programmer Analyst II, ATS Statistical Consulting Group
University of California, Los Angeles
https://joshuawiley.com/



More information about the R-help mailing list