简体   繁体   中英

How to implement dist in R

I have a distance function which looks like this

sed <- function(x, y){
x <- x / sum(x)
y <- y / sum(y)

x <- x[y > 0]
y <- y[y > 0]
y <- y[x > 0]
x <- x[x > 0]

xy <- x + y
a <- x / xy
b <- y / xy

w <- xy / 2
2 * prod(a^(a*w) * b^(b*w)) - 1

}

and I have some data that looks like this:

> head(x)
             x         y
[1,] 0.5836377 0.8120142
[2,] 0.4642154 0.8857223
[3,] 0.8707579 0.4917120
[4,] 0.4688734 0.8832654
[5,] 0.8105051 0.5857316
[6,] 0.6409956 0.7675446

Where each row is a point with x and y coordinates. So sed calculates the distance between rows.

I would like to plot a heatmap using my distance function but I get the following error, how can I fix this?

> heatmap(as.matrix(x), distfun=as.dist(sed))
Error in as.vector(x, mode) : 
  cannot coerce type 'closure' to vector of type 'any'

The distfun argument expects a function that will return an object that is of class "dist" as returned by dist() . Your function computes the distance between two vectors not the entire set of dissimilarities for each pair of observations. It doesn't return the right type of object and nor can it without all the scaffolding required to iterate over all pairs of rows in the input data.

Fear ye not though as help is at hand via the proxy package available on CRAN . This allows you to provide exactly the sort of function you have written and it provides all the scaffolding required to generate the dissimilarity matrix.

You need to register your function with proxy before you can use it in anger:

## create a new distance measure
mydist <- function(x,y) x * y

## create a new entry in the registry with two aliases
pr_DB$set_entry(FUN = mydist, names = c("mydist"))

Just replace mydist with your function name and you can give it whatever name you want when entering it into the proxy database. Continuing this example, you'd then use dist() to compute the dissimilarity matrix:

dist(X, method = "mydist")

To see if this will suit your needs

install.packages("proxy")
require("proxy")

Then read ?dist and ?pr_DB .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM