我已经定义了一个距离函数如下K-手段与我自己的距离函数的聚类
jaccard.rules.dist <- function(x,y) ({
# implements feature distance. Feature "Airline" gets a different treatment, the rest
# are booleans coded as 1/0. Airline column distance = 0 if same airline, 1 otherwise
# the rest of the atributes' distance is cero iff both are 1, 1 otherwise
airline.column <- which(colnames(x)=="Aerolinea")
xmod <- x
ymod <-y
xmod[airline.column] <-ifelse(x[airline.column]==y[airline.column],1,0)
ymod[airline.column] <-1 # if they are the same, they are both ones, else they are different
andval <- sum(xmod&ymod)
orval <- sum(xmod|ymod)
return (1-andval/orval)
})
这改变一点点的Jaccard距离为形式的dataframes现在
t <- data.frame(Aerolinea=c("A","B","C","A"),atr2=c(1,1,0,0),atr3=c(0,0,0,1))
,我会喜欢用我刚刚定义的距离在我的数据集上执行一些k-均值聚类。如果我尝试使用函数kmeans,则无法指定我的距离函数。我试过用hclust,它接受一个distanca矩阵,这是我计算如下
distmat <- matrix(nrow=nrow(t),ncol=nrow(t))
for (i in 1:nrow(t))
for (j in i:nrow(t))
distmat[j,i] <- jaccard.rules.dist(t[j,],t[i,])
distmat <- as.dist(distmat)
,然后调用hclust
hclust(distmat)
Error in if (is.na(n) || n > 65536L) stop("size cannot be NA nor exceed 65536") :
missing value where TRUE/FALSE needed
我到底做错了什么?有没有另一种方法可以接受任意距离函数作为其输入?
在此先感谢。
您的距离矩阵中是否缺少值? – 2013-05-06 20:41:45
或者你的矩阵大小大于65536? – 2013-05-06 21:04:23
不,没有缺失的值,矩阵是(在上面的例子中)4x4 – user2345448 2013-05-06 23:39:27