这更像是一个功能问题。所以我有二进制向量的Sokal和Sneath距离:
d(xi,xj)=a/(a+2*(b+c))
其中,a =#(xik = xjk = 1),b =#(xik = 0且xjk = 1)并且c =#(xik = 1且xjk = 0)。 k是矩阵的列数,#表示事件的次数。
你能给我一些提示和技巧吗?谢谢。
我写了一个,b,c为
a=sum(x[i]==x[j]==1)
b=sum((x[i]==0)&&(x[j]==0]))
c=sum((x[i]==1)&&(x[j]==1))
答案 0 :(得分:0)
我找到了解决问题的方法:
SSD<- function(x) {
x<-as.matrix(x) #convert to matrix
dist.matrix<- matrix(0,nrow(x),nrow(x)) #creates empty matrix
for (i in 1:nrow(x)) { #iterate the data rows over i
for (j in 1:nrow(x)) { #iterate the data rows over j
a<- 0 #empty|
b<- 0 #empty|=> to populate later on
c<- 0 #empty|
for (k in 1:ncol(x)) { #iterate the data over columns
if (((x)[i,k]==1)&((x)[j,k]==1)) { #first assumption to hold
a<-a+1 #summing the ones that hold the case
}else if (((x)[i,k]==0)&((x)[j,k]==1)) { #second assumption
b<-b+1 #summing the ones that hold the case
} else if (((x)[i,k]==1)&((x)[j,k]==0)) { #third assumption
c<-c+1 #summing the ones that hold the case
}
}
dist.matrix[i,j]<-a/(a+2*(b+c)) #populate the matrix by the formula given
}
}
library(xtable)
return(xtable(dist.matrix))
}