[R] fastest R platform: follow-up and summary

Roger Bivand rsb at reclus.nhh.no
Tue Apr 17 19:33:51 CEST 2001


On Tue, 17 Apr 2001, Huntsinger, Reid wrote:

> 
> var(matrix(rnorm(4000000),ncol=4,nrow=1000000))
> 
This wasn't quite the issue here, as the computation actually averaged the
values of the 4 by 4 variance matrices - though for zero mu and unit
diagonal sigma it may be a moot point. I do find it difficult to accept
that R/S languages are slow in themselves. On a regular PC with 256Mb
(Linux), I get a result for 1 million in under two minutes:

> library(MASS)
> size <- 4
> mu <- numeric(size)
> sigma <- diag(rep(1, size))
> res <- matrix(0, size, size)
> n1 <- 10000
> n2 <- 100
> fx <- function(x){x %*% t(x)}
> system.time(for (i in 1:n2) {
+ res1 <- t(apply(mvrnorm(n=n1, mu=mu, Sigma=sigma), 1, fx))
+ res <- res + matrix(apply(res1, 2, mean), size, size)
+ })
[1] 101.65   1.41 104.28   0.00   0.00
> res <- res/n2
> res
              [,1]          [,2]          [,3]          [,4]
[1,]  1.002256e+00 -0.0003356233  0.0005537899  6.463350e-06
[2,] -3.356233e-04  1.0002180438 -0.0001356754 -7.265024e-04
[3,]  5.537899e-04 -0.0001356754  0.9973236024  2.512160e-04
[4,]  6.463350e-06 -0.0007265024  0.0002512160  9.998879e-01
> n1*n2
[1] 1e+06

mostly from the substitution given by Luke Tierney, I guess. The
attraction here is that so much functionality has already been written
taking the numerical analysis issues into account, as for example reading
mvrnorm() shows. For real speed, the advice given earlier will get you
there.

Roger


-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Breiviksveien 40, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 93 93
e-mail: Roger.Bivand at nhh.no
and: Department of Geography and Regional Development, University of
Gdansk, al. Mar. J. Pilsudskiego 46, PL-81 378 Gdynia, Poland.

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list