Jack Handy
Jack Handy

Reputation: 3

Calculate matrix means and compile across matrices

I've only been using R for about a week (converting long-time SAS user). I'm running a model that generates multiple identical matrices (representing different parameter scenarios) similarly to the simplified example below:

    library(matrixStats)
    len<-50
    wid<-10
    mat1 <- matrix(nrow=len, ncol=wid)
    mat2 <- matrix(nrow=len, ncol=wid)
    mat3 <- matrix(nrow=len, ncol=wid)
set.seed(123)    
for (i in 1:wid){
      for (n in 1:len){
        mat1[n,i] <- runif(1)
        mat2[n,i] <- runif(1)
        mat3[n,i] <- runif(1)
    }}

I want to calculate the mean and standard deviation of the column sums for each matrix independently, similar to:

a <- colSums(mat1)
mean(a)
sd(a)

repeated for each matrix and then compile those values into a single dataset with a matrix identifier, mean, and standard deviation, similar to:

mat1_ID    24.42858    2.198454
mat2_ID    25.67452    1.677669
mat3_ID    24.31933    1.572029

I have no idea how to do this in R. Any help would be greatly appreciated.

Upvotes: 0

Views: 78

Answers (2)

Manos Papadakis
Manos Papadakis

Reputation: 593

If you want speed you can try my way. I will use package Rfast. Since you are new in R I will show step by step my example.

install.packages("Rfast") # download package Rfast

I will use the matrices from your example.

mats <- list(mat1=mat1,mat2=mat2,mat3=mat3) # "mat=mat" store matrix mat and add name
result <- sapply(mats,function(x){
             s <- Rfast::colsums(x) # you can use also the parallel version using argument "parallel"
             c(sd=Rfast::Var(s,std=TRUE),mean=mean(s))
          })

result # this will print the matrix    
      mat1      mat2      mat3
sd    1.677669  1.677669  1.572029
mean 25.674519 25.674519 24.319328

You can transpose the matrix to get the result as you wish

t(result)
           sd     mean
mat1 1.677669 25.67452
mat2 1.677669 25.67452
mat3 1.572029 24.31933

Upvotes: 1

s_baldur
s_baldur

Reputation: 33488

mats <- paste0("mat", 1:3)
t(sapply(mats, function(x) {
  cs <- colSums(get(x))
  c(mean(cs), sd(cs))
}))

         [,1]     [,2]
mat1 24.42858 2.198454
mat2 25.67452 1.677669
mat3 24.31933 1.572029

Upvotes: 0

Related Questions