不知道这是你做了什么,但是......
比方说,你有5个变量,例如数据框或矩阵中有5列。然后weights
将是包含相应列的权重的length=5
的向量。
在文档中的符号weights=rep.int(1,p)
只是意味着权重的默认值是长度为p的向量,其具有全部为1,例如。权重都等于1.在文档的其他地方,它解释了p是列数。
此外,请注意daisy(...)
产生相异矩阵。这是你在hclust(...)
中使用的。所以,如果x
是数据帧或矩阵五列的变量,那么:
d <- daisy(x, metric="gower", weights=c(1,2,3,4,5))
hc <- hclust(d, method="complete")
编辑(应答到OP的评论)
下面的代码显示集群如何依赖于权重。
clust.anal <- function(df,w,h) {
require(cluster)
d <- daisy(df, metric="gower", weights=w)
hc <- hclust(d, method="complete")
clust <- cutree(hc,h=h)
plot(hc, sub=paste("weights=",paste(wts,collapse=",")))
rect.hclust(hc,h=0.8,border="red")
}
df <- read.table("ExampleClusterData.csv", sep=";",header=T)
df[1] <- factor(df[[1]])
df[2] <- factor(df[[2]])
# weights increase with col number...
wts=c(1,2,3,4,5,6,7)
clust.anal(df,wts,h=0.8)
# weights decrease with col number...
wts=c(7,6,5,4,3,2,1)
clust.anal(df,wts,h=0.8)
不要忘记提及您正在使用'库(集群)'工作。 – tonytonov
正确,谢谢。 – user3231946