SCM

SCM Repository

Expires: Tue, 30 Apr 2024 10:16:59 -0000 Cache-Control: max-age=600 ETag: W/"220" [matrixstats] Contents of /pkg/matrixStats/tests/rowMedians.R
ViewVC logotype

Contents of /pkg/matrixStats/tests/rowMedians.R

Parent Directory Parent Directory | Revision Log Revision Log


Revision 220 - (show annotations)
Thu Jun 5 07:33:37 2014 UTC (9 years, 10 months ago) by henrikb
File size: 1316 byte(s)
CLEANUP: Package tests no longer do benchmarking.
BENCHMARK: Made list of alternative methods in reports more explicit.
1 library("matrixStats")
2
3 # - - - - - - - - - - - - - - - - - - - - - - - - - - - -
4 # Consistency checks
5 # - - - - - - - - - - - - - - - - - - - - - - - - - - - -
6 set.seed(1)
7
8 cat("Consistency checks:\n")
9 K <- if (Sys.getenv("_R_CHECK_FULL_") == "") 4 else 20
10 for (kk in 1:K) {
11 cat("Random test #", kk, "\n", sep="")
12
13 # Simulate data in a matrix of any shape
14 dim <- sample(500:2000, size=2)
15 n <- prod(dim)
16 x <- rnorm(n, sd=100)
17 dim(x) <- dim
18
19 # Add NAs?
20 if ((kk %% 4) %in% c(3,0)) {
21 cat("Adding NAs\n")
22 nna <- sample(n, size=1)
23 naValues <- c(as.double(NA), NaN)
24 x[sample(length(x), size=nna)] <- sample(naValues, size=nna, replace=TRUE)
25 }
26
27 # Integer or double?
28 if ((kk %% 4) %in% c(2,0)) {
29 cat("Coercing to integers\n")
30 storage.mode(x) <- "integer"
31 }
32
33 na.rm <- sample(c(TRUE,FALSE), size=1)
34
35 # rowMedians():
36 y1 <- matrixStats::rowMedians(x, na.rm=na.rm)
37 y2 <- apply(x, MARGIN=1, FUN=median, na.rm=na.rm)
38 stopifnot(all.equal(y1,y2))
39 y3 <- matrixStats::colMedians(t(x), na.rm=na.rm)
40 stopifnot(all.equal(y1,y3))
41
42 # colMedians():
43 y1 <- matrixStats::colMedians(x, na.rm=na.rm)
44 y2 <- apply(x, MARGIN=2, FUN=median, na.rm=na.rm)
45 stopifnot(all.equal(y1,y2))
46 y3 <- matrixStats::rowMedians(t(x), na.rm=na.rm)
47 stopifnot(all.equal(y1,y3))
48 } # for (kk ...)

root@r-forge.r-project.org
ViewVC Help
Powered by ViewVC 1.3.0-dev  
Thanks to:
Vienna University of Economics and Business Powered By FusionForge