我有10个二元变量的数据帧,是这样的:如何计算r中二元变量之间的相关矩阵?
V1 V2 V3...
0 1 1
1 1 0
1 0 1
0 0 1
我需要得到相关矩阵,然后我可以做因素分析。
psych::corr.test
可以计算出相关矩阵,但只有person
,spearman
,kendall
方法,不用于二进制数据。
那么,如何计算这个数据帧的相关矩阵呢?
我有10个二元变量的数据帧,是这样的:如何计算r中二元变量之间的相关矩阵?
V1 V2 V3...
0 1 1
1 1 0
1 0 1
0 0 1
我需要得到相关矩阵,然后我可以做因素分析。
psych::corr.test
可以计算出相关矩阵,但只有person
,spearman
,kendall
方法,不用于二进制数据。
那么,如何计算这个数据帧的相关矩阵呢?
# create data
m <- matrix(sample(x = 0:1,size = 200,replace = T),ncol = 10)
colnames(m) <- LETTERS[1:10]
m
# create cor matrix
res <- data.frame()
for(i in seq(ncol(m))){
z <- m[,i]
z <- apply(m,2,function(x){sum(x==z)/length(z)})
res <- rbind(res,z)
}
colnames(res) <- colnames(m)
rownames(res) <- colnames(m)
res <- as.matrix(res)
res
Correl方法适用于连续数据。 https://www.quora.com/Is-it-possible-to-calculate-correlations-between-binary-variables
ü你可以尝试非参数方法试图http://www.cedar.buffalo.edu/papers/articles/CVPRIP03_propbina.pdf
你仍然可以实现因子分析,计算%的比赛,并删除可变匹配> X%。这样您可以删除数据的维度。
实际上,您也可以使用Pearson相关系数来计算二元变量。请参阅https://en.m.wikipedia.org/wiki/Phi_coefficient – sedsiv
而不是计算相关性,我宁愿使用像Jaccard这样的相似性系数/指标。 – Miha
也取决于你的变量代表什么。如果它们可能是潜在的正态分布变量的指标,那么可以使用四分相关。 – user20650