美文网首页R模型
数量生态学笔记||模糊聚类

数量生态学笔记||模糊聚类

作者: 周运来就是我 | 来源:发表于2018-08-07 20:27 被阅读142次
    使用cluster程序包内Fanny()函数进行c-均值模糊聚类。

    在传统的聚类结果中,每个对象只能分配给唯一的一个组,然而,c-均值聚类结果不同,一个对象可以赋予不同的组,对象与组之间的归属度可以通过成员值(membership value.)衡量。

    rm(list = ls())
    setwd("D:\\Users\\Administrator\\Desktop\\RStudio\\数量生态学\\DATA")
    spe <- read.csv("DoubsSpe.csv", row.names=1)
    env <- read.csv("DoubsEnv.csv", row.names=1)
    spa <- read.csv("DoubsSpa.csv", row.names=1)
    # 删除无物种数据的样方8
    spe <- spe[-8,]
    env <- env[-8,]
    spa <- spa[-8,]
    # 物种多度数据:先计算样方之间的弦距离矩阵,然后进行单连接聚合聚类
    # **************************************************************
    spe.norm <- decostand(spe, "normalize")
    spe.ch <- vegdist(spe.norm, "euc")
    spe.ch.single <- hclust(spe.ch, method="single")
    # 鱼类数据的c-均值模糊聚类
    # ************************
    k <- 4  # 选择聚类分组的数量
    spe.fuz <- fanny(spe.ch, k=k, memb.exp=1.5)
    summary(spe.fuz)
    Fuzzy Clustering object of class 'fanny' :
    m.ship.expon.      1.5
    objective     6.065589
    tolerance        1e-15
    iterations         115
    converged            1
    maxit              500
    n                   29
    Membership coefficients (in %, rounded):
      [,1] [,2] [,3] [,4]
    1    42   28   13   18
    2    86    9    2    3
    3    86   10    2    2
    4    66   25    4    5
    5    17   43   20   20
    6    47   41    5    7
    7    83   13    2    3
    9    18   36   17   29
    10   36   48    7    9
    11   73   18    4    6
    12   84   11    2    3
    13   70   20    4    6
    14   60   29    5    6
    15   29   57    6    8
    16   14   70    8    8
    17   11   70   10    9
    18   10   58   18   13
    19   10   40   30   20
    20    3   10   73   13
    21    1    3   90    5
    22    1    4   90    5
    23    3    6    6   84
    24    3    4    7   86
    25    5    9   14   72
    26    2    5   80   12
    27    1    3   90    5
    28    2    4   89    6
    29    2    5   88    5
    30    3    7   81    9
    Fuzzyness coefficients:
    dunn_coeff normalized
    0.5757451  0.4343269
    Closest hard clustering:
    1  2  3  4  5  6  7  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
    1  1  1  1  2  1  1  2  2  1  1  1  1  2  2  2  2  2  3  3  3  4  4  4  3  3  3  3  3
    
    Silhouette plot information:
      cluster neighbor   sil_width
    2        1        2  0.52060562
    3        1        2  0.49263816
    12       1        2  0.48842628
    7        1        2  0.44908829
    13       1        2  0.42622825
    11       1        2  0.42572426
    4        1        2  0.34706151
    14       1        2  0.32623078
    1        1        2  0.27343990
    6        1        2  0.16117188
    17       2        3  0.22202399
    16       2        1  0.18163541
    9        2        4  0.08426515
    18       2        3  0.03401264
    5        2        3  0.02286117
    15       2        1 -0.11888413
    10       2        1 -0.13208427
    19       2        3 -0.14864026
    21       3        4  0.64316265
    22       3        4  0.64086333
    29       3        4  0.64026483
    27       3        4  0.63512401
    28       3        4  0.63102753
    30       3        4  0.59003815
    26       3        4  0.50955370
    20       3        4  0.50797154
    23       4        3  0.42148143
    24       4        3  0.38039206
    25       4        3  0.21960419
    Average silhouette width per cluster:
    [1] 0.39106149 0.01814871 0.59975072 0.34049256
    Average silhouette width of total data set:
    [1] 0.3405272
    
    Available components:
    [1] "membership"  "coeff"       "memb.exp"    "clustering"  "k.crisp"     "objective"   "convergence" "diss"
    [9] "call"        "silinfo"
    spefuz.g <- spe.fuz$clustering
    # 样方成员值
    spe.fuz$membership
            [,1]       [,2]       [,3]       [,4]
    1  0.41983293 0.27938150 0.12507006 0.17571551
    2  0.86333399 0.09160712 0.01872811 0.02633079
    3  0.86009710 0.09794009 0.01766820 0.02429460
    4  0.66104474 0.24709547 0.04069775 0.05116203
    5  0.17081624 0.42786571 0.20384018 0.19747787
    6  0.46991417 0.40943844 0.05307161 0.06757579
    7  0.82698989 0.12550961 0.01958153 0.02791898
    9  0.17845384 0.36031665 0.16941554 0.29181397
    10 0.36242730 0.47881889 0.06556495 0.09318886
    11 0.72787234 0.17931783 0.03775983 0.05505000
    12 0.83703665 0.11498506 0.01966949 0.02830879
    13 0.70047249 0.20084165 0.04108743 0.05759844
    14 0.59873829 0.28911587 0.04766088 0.06448496
    15 0.29396867 0.57042116 0.05897579 0.07663437
    16 0.14473808 0.69577978 0.07675396 0.08272817
    17 0.11117893 0.69749883 0.09890084 0.09242140
    18 0.10161983 0.57950673 0.18443183 0.13444161
    19 0.10315906 0.40097836 0.29706350 0.19879907
    20 0.03321635 0.09978590 0.73408504 0.13291271
    21 0.01305429 0.03432591 0.90451831 0.04810149
    22 0.01397900 0.03563244 0.90016255 0.05022601
    23 0.03467200 0.05789778 0.06303980 0.84439042
    24 0.02570392 0.04371767 0.07027107 0.86030734
    25 0.04932806 0.09308773 0.14017276 0.71741146
    26 0.02218219 0.04921691 0.80439767 0.12420324
    27 0.01448996 0.03317050 0.89817571 0.05416383
    28 0.01646103 0.03672029 0.88854853 0.05827015
    29 0.01920502 0.04781543 0.87803550 0.05494406
    30 0.03235038 0.07222784 0.80641410 0.08900768
    # 每个样方最接近的聚类簇
    spe.fuz$clustering
    1  2  3  4  5  6  7  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
    1  1  1  1  2  1  1  2  2  1  1  1  1  2  2  2  2  2  3  3  3  4  4  4  3  3  3  3  3
    # 轮廓图
    plot(silhouette(spe.fuz), main="鱼类数据的c-均值模糊聚类",
    +   cex.names=0.8, col=spe.fuz$silinfo$widths+1,border="white")
    
    
    # 模糊聚类簇的主坐标排序(PCoA)
    dc.pcoa <- cmdscale(spe.ch)
    dc.scores <- scores(dc.pcoa, choices=c(1,2))
    plot(scores(dc.pcoa), asp=1, type="n",
        main="模糊聚类簇的排序(PCoA)")
    abline(h=0, lty="dotted")
    abline(v=0, lty="dotted")
    for (i in 1:k) {
        gg <- dc.scores[spefuz.g==i,]
        hpts <- chull(gg)
        hpts <- c(hpts, hpts[1])
        lines(gg[hpts,], col=i+1)
        }
    stars(spe.fuz$membership, location=scores(dc.pcoa), draw.segments=TRUE,
        add=TRUE, scale=FALSE, len=0.1, col.segments=2:(k+1))
    legend(locator(1), paste("组", 1:k, sep=" "),
        pch=15, pt.cex=2, col=2:(k+1), bty="n")
    # 在图上任意点击某一处放置图例
    
    

    模糊聚类(FCM)介绍及R语言实现
    模糊c均值聚类和k-means聚类的数学原理
    FCM(Fuzzy C-Means)模糊C聚类
    对模糊聚类的小结
    模糊聚类分析

    相关文章

      网友评论

        本文标题:数量生态学笔记||模糊聚类

        本文链接:https://www.haomeiwen.com/subject/glxzvftx.html