rpvclust

Hamming distance measure for pvclust


I am trying to create a Hamming distance measure for the pvclust clustering method. (There isn't one defined for this function.) I'm based on the example given for the cosine measure:

cosine <- function(x) {
x <- as.matrix(x)
y <- t(x) %*% x
res <- 1 - y / (sqrt(diag(y)) %*% t(sqrt(diag(y))))
res <- as.dist(res)
attr(res, "method") <- "cosine"
return(res)
}

I try to do it this way:

hamming <- function(x) {
x <- as.matrix(x)
y <- t(x) %*% x
res <- sum(y != y)
res <- as.dist(res)
attr(res, "method") <- "hamming"
return(res)
}

Unfortunately it doesn't work properly. Anyone have any postings, where is the error and how to fix it?


Solution

  • Try this

    hamming <- function(x) {
    x <- as.matrix(x)
    y <- (1 - x) %*% t(x)
    res <- y + t(y)
    res <- as.dist(res)
    attr(res, "method") <- "hamming"
    return(res)
    }