R Under development (unstable) (2024-09-02 r87090 ucrt) -- "Unsuffered Consequences" Copyright (C) 2024 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > library("matrixStats") > > fcns <- list( + weightedVar = weightedVar, + weightedSd = weightedSd, + weightedMad = weightedMad + ) > > for (name in names(fcns)) { + cat(sprintf("%s()...\n", name)) + fcn <- fcns[[name]] + + for (mode in c("integer", "double")) { + cat("mode: ", mode, "\n", sep = "") + n <- 15L + x <- runif(n, min = -5, max = 5) + storage.mode(x) <- mode + str(x) + + for (add_na in c(FALSE, TRUE)) { + cat("add_na: ", add_na, "\n", sep = "") + if (add_na) { + x[c(5, 7)] <- NA + } + str(x) + + for (na.rm in c(FALSE, TRUE)) { + cat("na.rm: ", na.rm, "\n", sep = "") + + cat("Weights are not specified (all are 1)\n") + m1 <- fcn(x, na.rm = na.rm) + str(list(m1 = m1)) + + cat("All weights are 1\n") + w <- rep(1, times = n) + m1 <- fcn(x, w, na.rm = na.rm) + str(list(m1 = m1)) + + cat("First weight is 5\n") + # Pull the mean towards zero + w[1] <- 5 + str(w) + m1 <- fcn(x, w, na.rm = na.rm) + str(list(m1 = m1)) + + cat("All weights are 0\n") + # All weights set to zero + w <- rep(0, times = n) + m1 <- fcn(x, w, na.rm = na.rm) + str(list(m1 = m1)) + + cat("First weight is 8.5\n") + # Put even more weight on the zero + w[1] <- 8.5 + m1 <- fcn(x, w, na.rm = na.rm) + str(list(m1 = m1)) + + cat("First weight is Inf\n") + # All weight on the first value + w[1] <- Inf + m1 <- fcn(x, w, na.rm = na.rm) + str(list(m1 = m1)) + + cat("Last weight is Inf\n") + # All weight on the last value + w[1] <- 1 + w[n] <- Inf + m1 <- fcn(x, w, na.rm = na.rm) + str(list(m1 = m1)) + + cat("Last weight is NA\n") + # All weight on the last value + w[1] <- 1 + w[n] <- NA_real_ + m1 <- fcn(x, w, na.rm = na.rm) + str(list(m1 = m1)) + } # for (na.rm ...) + } # for (add_na ...) + } # for (mode ...) + + cat(sprintf("%s()...DONE\n", name)) + } # for (name ...) weightedVar()... mode: integer int [1:15] 2 0 4 4 0 3 4 -2 -1 0 ... add_na: FALSE int [1:15] 2 0 4 4 0 3 4 -2 -1 0 ... na.rm: FALSE Weights are not specified (all are 1) List of 1 $ m1: num 7.55 All weights are 1 List of 1 $ m1: num 7.55 First weight is 5 num [1:15] 5 1 1 1 1 1 1 1 1 1 ... List of 1 $ m1: num 6.01 All weights are 0 List of 1 $ m1: int NA First weight is 8.5 List of 1 $ m1: int NA First weight is Inf List of 1 $ m1: int NA Last weight is Inf List of 1 $ m1: int NA Last weight is NA List of 1 $ m1: int NA na.rm: TRUE Weights are not specified (all are 1) List of 1 $ m1: num 7.55 All weights are 1 List of 1 $ m1: num 7.55 First weight is 5 num [1:15] 5 1 1 1 1 1 1 1 1 1 ... List of 1 $ m1: num 6.01 All weights are 0 List of 1 $ m1: int NA First weight is 8.5 List of 1 $ m1: int NA First weight is Inf List of 1 $ m1: int NA Last weight is Inf List of 1 $ m1: int NA Last weight is NA List of 1 $ m1: int NA add_na: TRUE int [1:15] 2 0 4 4 NA 3 NA -2 -1 0 ... na.rm: FALSE Weights are not specified (all are 1) List of 1 $ m1: int NA All weights are 1 List of 1 $ m1: int NA First weight is 5 num [1:15] 5 1 1 1 1 1 1 1 1 1 ... List of 1 $ m1: int NA All weights are 0 List of 1 $ m1: int NA First weight is 8.5 List of 1 $ m1: int NA First weight is Inf List of 1 $ m1: int NA Last weight is Inf List of 1 $ m1: int NA Last weight is NA List of 1 $ m1: int NA na.rm: TRUE Weights are not specified (all are 1) List of 1 $ m1: num 8 All weights are 1 List of 1 $ m1: num 8 First weight is 5 num [1:15] 5 1 1 1 1 1 1 1 1 1 ... List of 1 $ m1: num 6.19 All weights are 0 List of 1 $ m1: int NA First weight is 8.5 List of 1 $ m1: int NA First weight is Inf List of 1 $ m1: int NA Last weight is Inf List of 1 $ m1: int NA Last weight is NA List of 1 $ m1: int NA mode: double num [1:15] 1.6034 0.2209 0.5559 3.3981 0.0815 ... add_na: FALSE num [1:15] 1.6034 0.2209 0.5559 3.3981 0.0815 ... na.rm: FALSE Weights are not specified (all are 1) List of 1 $ m1: num 6.43 All weights are 1 List of 1 $ m1: num 6.43 First weight is 5 num [1:15] 5 1 1 1 1 1 1 1 1 1 ... List of 1 $ m1: num 5.59 All weights are 0 List of 1 $ m1: num NA First weight is 8.5 List of 1 $ m1: num NA First weight is Inf List of 1 $ m1: num NA Last weight is Inf List of 1 $ m1: num NA Last weight is NA List of 1 $ m1: num NA na.rm: TRUE Weights are not specified (all are 1) List of 1 $ m1: num 6.43 All weights are 1 List of 1 $ m1: num 6.43 First weight is 5 num [1:15] 5 1 1 1 1 1 1 1 1 1 ... List of 1 $ m1: num 5.59 All weights are 0 List of 1 $ m1: num NA First weight is 8.5 List of 1 $ m1: num NA First weight is Inf List of 1 $ m1: num NA Last weight is Inf List of 1 $ m1: num NA Last weight is NA List of 1 $ m1: num NA add_na: TRUE num [1:15] 1.603 0.221 0.556 3.398 NA ... na.rm: FALSE Weights are not specified (all are 1) List of 1 $ m1: num NA All weights are 1 List of 1 $ m1: num NA First weight is 5 num [1:15] 5 1 1 1 1 1 1 1 1 1 ... List of 1 $ m1: num NA All weights are 0 List of 1 $ m1: num NA First weight is 8.5 List of 1 $ m1: num NA First weight is Inf List of 1 $ m1: num NA Last weight is Inf List of 1 $ m1: num NA Last weight is NA List of 1 $ m1: num NA na.rm: TRUE Weights are not specified (all are 1) List of 1 $ m1: num 7.42 All weights are 1 List of 1 $ m1: num 7.42 First weight is 5 num [1:15] 5 1 1 1 1 1 1 1 1 1 ... List of 1 $ m1: num 6.27 All weights are 0 List of 1 $ m1: num NA First weight is 8.5 List of 1 $ m1: num NA First weight is Inf List of 1 $ m1: num NA Last weight is Inf List of 1 $ m1: num NA Last weight is NA List of 1 $ m1: num NA weightedVar()...DONE weightedSd()... mode: integer int [1:15] -1 3 -1 -2 -3 3 0 4 -4 -3 ... add_na: FALSE int [1:15] -1 3 -1 -2 -3 3 0 4 -4 -3 ... na.rm: FALSE Weights are not specified (all are 1) List of 1 $ m1: num 2.5 All weights are 1 List of 1 $ m1: num 2.5 First weight is 5 num [1:15] 5 1 1 1 1 1 1 1 1 1 ... List of 1 $ m1: num 2.22 All weights are 0 List of 1 $ m1: num NA First weight is 8.5 List of 1 $ m1: num NA First weight is Inf List of 1 $ m1: num NA Last weight is Inf List of 1 $ m1: num NA Last weight is NA List of 1 $ m1: num NA na.rm: TRUE Weights are not specified (all are 1) List of 1 $ m1: num 2.5 All weights are 1 List of 1 $ m1: num 2.5 First weight is 5 num [1:15] 5 1 1 1 1 1 1 1 1 1 ... List of 1 $ m1: num 2.22 All weights are 0 List of 1 $ m1: num NA First weight is 8.5 List of 1 $ m1: num NA First weight is Inf List of 1 $ m1: num NA Last weight is Inf List of 1 $ m1: num NA Last weight is NA List of 1 $ m1: num NA add_na: TRUE int [1:15] -1 3 -1 -2 NA 3 NA 4 -4 -3 ... na.rm: FALSE Weights are not specified (all are 1) List of 1 $ m1: num NA All weights are 1 List of 1 $ m1: num NA First weight is 5 num [1:15] 5 1 1 1 1 1 1 1 1 1 ... List of 1 $ m1: num NA All weights are 0 List of 1 $ m1: num NA First weight is 8.5 List of 1 $ m1: num NA First weight is Inf List of 1 $ m1: num NA Last weight is Inf List of 1 $ m1: num NA Last weight is NA List of 1 $ m1: num NA na.rm: TRUE Weights are not specified (all are 1) List of 1 $ m1: num 2.58 All weights are 1 List of 1 $ m1: num 2.58 First weight is 5 num [1:15] 5 1 1 1 1 1 1 1 1 1 ... List of 1 $ m1: num 2.26 All weights are 0 List of 1 $ m1: num NA First weight is 8.5 List of 1 $ m1: num NA First weight is Inf List of 1 $ m1: num NA Last weight is Inf List of 1 $ m1: num NA Last weight is NA List of 1 $ m1: num NA mode: double num [1:15] 3.217 -1.354 -0.627 1.599 -2.988 ... add_na: FALSE num [1:15] 3.217 -1.354 -0.627 1.599 -2.988 ... na.rm: FALSE Weights are not specified (all are 1) List of 1 $ m1: num 2.57 All weights are 1 List of 1 $ m1: num 2.57 First weight is 5 num [1:15] 5 1 1 1 1 1 1 1 1 1 ... List of 1 $ m1: num 2.56 All weights are 0 List of 1 $ m1: num NA First weight is 8.5 List of 1 $ m1: num NA First weight is Inf List of 1 $ m1: num NA Last weight is Inf List of 1 $ m1: num NA Last weight is NA List of 1 $ m1: num NA na.rm: TRUE Weights are not specified (all are 1) List of 1 $ m1: num 2.57 All weights are 1 List of 1 $ m1: num 2.57 First weight is 5 num [1:15] 5 1 1 1 1 1 1 1 1 1 ... List of 1 $ m1: num 2.56 All weights are 0 List of 1 $ m1: num NA First weight is 8.5 List of 1 $ m1: num NA First weight is Inf List of 1 $ m1: num NA Last weight is Inf List of 1 $ m1: num NA Last weight is NA List of 1 $ m1: num NA add_na: TRUE num [1:15] 3.217 -1.354 -0.627 1.599 NA ... na.rm: FALSE Weights are not specified (all are 1) List of 1 $ m1: num NA All weights are 1 List of 1 $ m1: num NA First weight is 5 num [1:15] 5 1 1 1 1 1 1 1 1 1 ... List of 1 $ m1: num NA All weights are 0 List of 1 $ m1: num NA First weight is 8.5 List of 1 $ m1: num NA First weight is Inf List of 1 $ m1: num NA Last weight is Inf List of 1 $ m1: num NA Last weight is NA List of 1 $ m1: num NA na.rm: TRUE Weights are not specified (all are 1) List of 1 $ m1: num 2.56 All weights are 1 List of 1 $ m1: num 2.56 First weight is 5 num [1:15] 5 1 1 1 1 1 1 1 1 1 ... List of 1 $ m1: num 2.47 All weights are 0 List of 1 $ m1: num NA First weight is 8.5 List of 1 $ m1: num NA First weight is Inf List of 1 $ m1: num NA Last weight is Inf List of 1 $ m1: num NA Last weight is NA List of 1 $ m1: num NA weightedSd()...DONE weightedMad()... mode: integer int [1:15] -4 2 -1 -2 2 0 2 3 0 -3 ... add_na: FALSE int [1:15] -4 2 -1 -2 2 0 2 3 0 -3 ... na.rm: FALSE Weights are not specified (all are 1) List of 1 $ m1: num 2.97 All weights are 1 List of 1 $ m1: num 2.97 First weight is 5 num [1:15] 5 1 1 1 1 1 1 1 1 1 ... List of 1 $ m1: num 4.45 All weights are 0 List of 1 $ m1: int NA First weight is 8.5 List of 1 $ m1: int 0 First weight is Inf List of 1 $ m1: int 0 Last weight is Inf List of 1 $ m1: int 0 Last weight is NA List of 1 $ m1: int NA na.rm: TRUE Weights are not specified (all are 1) List of 1 $ m1: num 2.97 All weights are 1 List of 1 $ m1: num 2.97 First weight is 5 num [1:15] 5 1 1 1 1 1 1 1 1 1 ... List of 1 $ m1: num 4.45 All weights are 0 List of 1 $ m1: int NA First weight is 8.5 List of 1 $ m1: int 0 First weight is Inf List of 1 $ m1: int 0 Last weight is Inf List of 1 $ m1: int 0 Last weight is NA List of 1 $ m1: int NA add_na: TRUE int [1:15] -4 2 -1 -2 NA 0 NA 3 0 -3 ... na.rm: FALSE Weights are not specified (all are 1) List of 1 $ m1: num NA All weights are 1 List of 1 $ m1: int NA First weight is 5 num [1:15] 5 1 1 1 1 1 1 1 1 1 ... List of 1 $ m1: int NA All weights are 0 List of 1 $ m1: int NA First weight is 8.5 List of 1 $ m1: int 0 First weight is Inf List of 1 $ m1: int 0 Last weight is Inf List of 1 $ m1: int 0 Last weight is NA List of 1 $ m1: int NA na.rm: TRUE Weights are not specified (all are 1) List of 1 $ m1: num 4.45 All weights are 1 List of 1 $ m1: num 4.45 First weight is 5 num [1:15] 5 1 1 1 1 1 1 1 1 1 ... List of 1 $ m1: num 2.97 All weights are 0 List of 1 $ m1: int NA First weight is 8.5 List of 1 $ m1: int 0 First weight is Inf List of 1 $ m1: int 0 Last weight is Inf List of 1 $ m1: int 0 Last weight is NA List of 1 $ m1: int NA mode: double num [1:15] 4.575 4.346 -0.509 4.945 2.118 ... add_na: FALSE num [1:15] 4.575 4.346 -0.509 4.945 2.118 ... na.rm: FALSE Weights are not specified (all are 1) List of 1 $ m1: num 3.3 All weights are 1 List of 1 $ m1: num 3.3 First weight is 5 num [1:15] 5 1 1 1 1 1 1 1 1 1 ... List of 1 $ m1: num 3.64 All weights are 0 List of 1 $ m1: num NA First weight is 8.5 List of 1 $ m1: num 0 First weight is Inf List of 1 $ m1: num 0 Last weight is Inf List of 1 $ m1: num 0 Last weight is NA List of 1 $ m1: num NA na.rm: TRUE Weights are not specified (all are 1) List of 1 $ m1: num 3.3 All weights are 1 List of 1 $ m1: num 3.3 First weight is 5 num [1:15] 5 1 1 1 1 1 1 1 1 1 ... List of 1 $ m1: num 3.64 All weights are 0 List of 1 $ m1: num NA First weight is 8.5 List of 1 $ m1: num 0 First weight is Inf List of 1 $ m1: num 0 Last weight is Inf List of 1 $ m1: num 0 Last weight is NA List of 1 $ m1: num NA add_na: TRUE num [1:15] 4.575 4.346 -0.509 4.945 NA ... na.rm: FALSE Weights are not specified (all are 1) List of 1 $ m1: num NA All weights are 1 List of 1 $ m1: num NA First weight is 5 num [1:15] 5 1 1 1 1 1 1 1 1 1 ... List of 1 $ m1: num NA All weights are 0 List of 1 $ m1: num NA First weight is 8.5 List of 1 $ m1: num 0 First weight is Inf List of 1 $ m1: num 0 Last weight is Inf List of 1 $ m1: num 0 Last weight is NA List of 1 $ m1: num NA na.rm: TRUE Weights are not specified (all are 1) List of 1 $ m1: num 2.29 All weights are 1 List of 1 $ m1: num 2.29 First weight is 5 num [1:15] 5 1 1 1 1 1 1 1 1 1 ... List of 1 $ m1: num 2.17 All weights are 0 List of 1 $ m1: num NA First weight is 8.5 List of 1 $ m1: num 0 First weight is Inf List of 1 $ m1: num 0 Last weight is Inf List of 1 $ m1: num 0 Last weight is NA List of 1 $ m1: num NA weightedMad()...DONE > > proc.time() user system elapsed 0.43 0.09 0.50