1

I have the following matrix

Rho <- structure(c(1, 0.466666666666667, -0.866666666666667, -0.466666666666667, 
-0.333333333333333, 0.466666666666667, 1, -0.6, -0.466666666666667, 
-0.333333333333333, -0.866666666666667, -0.6, 1, 0.333333333333333, 
0.466666666666667, -0.466666666666667, -0.466666666666667, 0.333333333333333, 
1, -0.2, -0.333333333333333, -0.333333333333333, 0.466666666666667, 
-0.2, 1), .Dim = c(5L, 5L), .Dimnames = list(c("SPX Index", "MXE2 Index", 
"USG4TR Index", "FNAR Index", "DBLCMAVL Index"), c("SPX Index", 
"MXE2 Index", "USG4TR Index", "FNAR Index", "DBLCMAVL Index")))

I thought that, in order to apply functions to single matrix arguments (like rows and/or column), I had to use mapply() or apply() in several ways.

But, if I input

mean(Rho)  
sd(Rho)

this returns me separate application of functions:

> mean(Rho)
     SPX Index     MXE2 Index   USG4TR Index     FNAR Index DBLCMAVL Index 
   -0.04000000     0.01333333     0.06666667     0.04000000     0.12000000 
> sd(Rho)
     SPX Index     MXE2 Index   USG4TR Index     FNAR Index DBLCMAVL Index 
     0.7566006      0.6902496      0.7774603      0.6282250      0.5932959 

This is not what I want: I want the mean and st. dev. of all the elements of my matrix, like

> mean(mean(Rho))
[1] 0.04

in just one command.

Is there a way to do it without coercing my matrix to vector/numeric/other?

> sessionInfo()
R version 2.15.1 (2012-06-22)
Platform: i386-pc-mingw32/i386 (32-bit)

locale:
[1] LC_COLLATE=English_Australia.1252  LC_CTYPE=English_Australia.1252   
[3] LC_MONETARY=English_Australia.1252 LC_NUMERIC=C                      
[5] LC_TIME=English_Australia.1252    

attached base packages:
[1] stats     graphics  grDevices datasets  utils     methods   base     

other attached packages:
 [1] PerformanceAnalytics_1.0.4.4 xts_0.8-6                   
 [3] zoo_1.7-7                    gogarch_0.7-2               
 [5] fastICA_1.1-16               fGarch_2150.81              
 [7] fBasics_2160.81              rmgarch_0.97                
 [9] Matrix_1.0-9                 lattice_0.20-10             
[11] Kendall_2.2                  spd_1.7                     
[13] KernSmooth_2.23-8            rugarch_1.0-11              
[15] Rsolnp_1.12                  truncnorm_1.0-6             
[17] chron_2.3-42                 numDeriv_2012.3-1           
[19] MASS_7.3-18                  RcppArmadillo_0.3.4.2       
[21] Rcpp_0.9.13                  timeSeries_2160.94          
[23] timeDate_2160.95             rcom_2.2-5                  
[25] rscproxy_2.0-5              

loaded via a namespace (and not attached):
[1] boot_1.3-5       grid_2.15.1      stabledist_0.6-4 tools_2.15.1 
Lisa Ann
  • 3,345
  • 6
  • 31
  • 42
  • 1
    `mean(Rho)` also works fine for me on the example matrix provided above but for `sd` one has to use `sd(c(Rho))`. – Maiasaura Oct 03 '12 at 16:11
  • 2
    Related question: http://stackoverflow.com/questions/9424311/how-to-get-mean-median-other-statistics-over-entire-matrix-array-or-datafram – Maiasaura Oct 03 '12 at 16:12
  • 1
    Coercing to vector (a la Ben's comment) really doesn't cost anything. Also, a warning: if you happen to have a list variable whose elements (columns) are of differing length, mean(c(all_column_means)) will give you a weighted average. – Carl Witthoft Oct 03 '12 at 17:22

1 Answers1

3

While I cannot say for sure that this is the reason you're seeing what you're seeing without seeing your sessionInfo(), my guess is that you have the PerformanceAnalytics package loaded.

in the zzz.R file of that package, they have this code:

mean.xts <- function(x,...) {
if(is.vector(x) ||is.null(ncol(x))  || ncol(x)==1){
        x<-as.numeric(x)
        mean(x,...)
    } else apply(x,2,mean.xts,...)
} 
mean.matrix <- function(x,...) {apply(x,2,mean,...)} 

sd.xts <- function(x,na.rm=FALSE) {
    if(is.vector(x) || is.null(ncol(x)) || ncol(x)==1){
        x<-as.numeric(x)
        sd(x,na.rm=na.rm)
    } else apply(x,2,sd,na.rm=na.rm)
}
sd.matrix <- function(x,na.rm=FALSE) {apply(x,2,sd,na.rm=na.rm)}

I think it is awful that they do that and I've tried repeatedly to get them to change it, but to no avail. Therefore, I boycott the package.

Anyway, start a fresh R session without loading that package and try again.

GSee
  • 48,880
  • 13
  • 125
  • 145
  • 1
    They (the authors) must have been "trained" on Matlab, which insists on doing this same dumb thing. I have repeatedly had to teach colleagues (using Matlab) that just because `mean(mean(my_matrix))` works, it doesn't follow that `stdev(stdev(my_matrix))` works. – Carl Witthoft Oct 03 '12 at 18:22
  • 1
    @CarlWitthoft, I think it's that prior to **R** 2.14 (I think), the base **R** functions used to work on each column separately. When that changed, it broke a lot of their code. Instead of creating their own class, or editing their code to use `apply`, or talking the `xts` authors into adding a `mean.xts` and `sd.xts` method, they took the lazy route and just redefined the base function to its old definition. – GSee Oct 03 '12 at 18:29
  • 1
    @JohnGay, I see you've [had this problem before](http://stackoverflow.com/a/12019358/967840) – GSee Oct 03 '12 at 18:36
  • 1
    You're right, GSee: it's like it happened in the linked question, it is a `PerformanceAnalytics` issue because I had it loaded; it's not the first time `PerformanceAnalytics` creates these issues. I think the best would be to load it just when needed and to detach it after use. I updated my question to show you `sessionInfo()` – Lisa Ann Oct 04 '12 at 07:49