Skip to content
Prev 396618 / 398502 Next

please help generate a square correlation matrix

If I have understood the request, I'm not sure that omitting all 0
pairs for each pair of columns makes much sense, but be that as it
may, here's another way to do it by using the 'FUN' argument of combn
to encapsulate any calculations that you do. I just use cor() as the
calculation -- you can use anything you like that takes two vectors of
0's and 1's and produces fixed length numeric results (or fromm which
you can extract such).

I encapsulated it all in a little function. Note that I first
converted the data frame to a matrix. Because of their generality,
data frames carry a lot of extra baggage that can slow purely numeric
manipulations down.

Anyway, here's the function, 'somecors' (I'm a bad name picker :(  ! )

   somecors <- function(dat, func = cor){
      dat <- as.matrix(dat)
      indx <- seq_len(ncol(dat))
         combn(indx, 2, FUN = \(z) {
            i <- z[1]; j <- z[2]
            k <- dat[, i ] | dat[, j ]
            c(z,func(dat[k,i ], dat[k,j ]))
         })
   }

Results come out as a matrix with combn(ncol(dat),2) columns, the
first 2 rows giving the pair of column numbers for each column,and
then 1 or more rows (possibly extracted) from whatever func you use.
Here's the results for your data formatted to 2 decimal places:
[,1]  [,2]  [,3]  [,4] [,5]  [,6]
[1,]  1.0  1.00  1.00  2.00    2  3.00
[2,]  2.0  3.00  4.00  3.00    4  4.00
[3,] -0.5 -0.41 -0.35 -0.41   NA -0.47
Warning message:
In func(dat[k, i], dat[k, j]) : the standard deviation is zero

The NA and warning comes in the 2,4 pair of columns because after
removing all zero rows in the pair, dat[,4] is all 1's, giving a zero
in the denominator of the cor() calculation -- again, assuming I have
correctly understood your request. If so, this might be something you
need to worry about.

Again, feel free to ignore if  I have misinterpreterd or this does not suit.

Cheers,
Bert
On Thu, Jul 25, 2024 at 2:01?PM Rui Barradas <ruipbarradas at sapo.pt> wrote: