【问题标题】:How to make a graph of clustered boolean variables in R?如何在 R 中制作聚集布尔变量的图?
【发布时间】:2016-06-10 15:03:44
【问题描述】:

我有一个完全由布尔变量组成的数据集。与下面转换后的动物数据集完全一样,只是有更多列。

# http://stats.stackexchange.com/questions/27323/cluster-analysis-of-boolean-vectors-in-r
library(cluster)
head(mona(animals)[[1]])

    war fly ver end gro hai
ant   0   0   0   0   1   0
bee   0   1   0   0   1   1
cat   1   0   1   0   0   1
cpl   0   0   0   0   0   1
chi   1   0   1   1   1   1
cow   1   0   1   0   1   1

我们的目标是重新排列行,以便更容易在视觉上识别相似成员模式的分组。

我认为某种聚类算法可能是可行的方法,但我不确定要使用哪些函数或如何准确地进行。

理想情况下,该表将被绘制为一种棋盘格。用阴影方块表示每个点是真还是假。

【问题讨论】:

    标签: r ggplot2 cluster-analysis data-visualization hierarchical-clustering


    【解决方案1】:

    此解决方案使用层次聚类对变量进行重新排序。值得注意的是,由于相异矩阵变大,因此无法很好地扩展大量观察结果。 this 回答中建议了许多观察的替代算法,但我没有完全理解它,也没有看到如何根据引用的章节来实现它。

    library(cluster)
    library(reshape2)
    library(ggplot2)
    
    # testing that it works using the categorical animals dataset
    adData <- mona(animals)$data
    
    # import the data, encoded with 0s and 1s for membership
    # adData  <- read.csv('adData.csv')
    
    # clustering based off this answer https://stats.stackexchange.com/a/48364
    # create a dissimilarity matrix 
    disimilarAdData <- daisy(adData)
    
    # hierarchically cluster by dissimilarity
    clusteredAdData <- agnes(disimilarAdData)
    
    # reorder the rows by dissimilarity
    orderedAdData <- adData[clusteredAdData[[1]], ]
    
    # make it logical data type for better graphing
    plotData <- sapply(as.data.frame(orderedAdData), as.logical)
    row.names(plotData) <- row.names(orderedAdData)
    
    # plot graph using shaded rows
    # http://stackoverflow.com/questions/21316363/plot-and-fill-chessboard-like-area-and-the-similars-in-r
    ggplot(melt(plotData), aes(x=Var2, y=Var1, fill=value)) + geom_tile()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-20
      • 2011-04-18
      • 1970-01-01
      • 2012-09-27
      • 2010-11-05
      相关资源
      最近更新 更多