Skip to content
Prev 304589 / 398503 Next

self-defined distance function to be computed on matrix

On Thu, Aug 30, 2012 at 10:48 AM, zz <czhang at uams.edu> wrote:
If I understand it correctly, you are trying to calculate the "cosine
correlation" while excluding all rows where one of the wto columns has
a zero? There may be other ways to do it, but (shameless plug) my
package WGCNA defines a replacement for the usual correlation function
cor() that lets you specify the argument cosine  = TRUE to calculate
cosine correlation (i.e., Pearson correlation without centering). To
ignore the zeroes, turn them into NA, and specify argument
use = "pairwise.complete.obs" (or just use = "p") to the function cor.

So define a matrix (say ABC), set all zero values to NA

ABC[ABC==0] = NA

then issue

library(WGCNA)
sim = cor(ABC, cosine = TRUE, use = 'p')

Note that the correlation gives you a similarity; to turn it into a
dissimilarity or distance you have to subtract it from 1

dissim = 1-sim

HTH,

Peter