R语言使用灰色关联分析(Grey Relation Analysis,GRA)中国经济社会发展指标

原文链接:http://tecdat.cn/?p=16881

灰色关联分析包括两个重要功能。
第一项功能:灰色关联度,与correlation系数相似,如果要评估某些单位,在使用此功能之前转置数据。第二个功能:灰色聚类,如层次聚类。 

灰色关联度

灰色关联度有两种用法。该算法用于测量两个变量的相似性,就像`cor`一样。如果要评估某些单位,可以转置数据集。

*一种是检查两个变量的相关性,数据类型如下:

| 参考| v1 | v2 | v3 |
| ----------- |||| ---- | ---- |
| 1.2 | 1.8 | 0.9 | 8.4 |
| 0.11 | 0.3 | 0.5 | 0.2 |
| 1.3 | 0.7 | 0.12 | 0.98 |
| 1.9 | 1.09 | 2.8 | 0.99 |

reference:参考变量,reference和v1之间的灰色关联度...近似地测量reference和v1的相似度。


*另一个是评估某些单位的好坏。

| 单位| v1 | v2 | v3 |
| ----------- |||| ---- | ---- |
| 江苏| 1.8 | 0.9 | 8.4 |
| 浙江| 0.3 | 0.5 | 0.2 |
| 安徽 0.7 | 0.12 | 0.98 |
| 福建| 1.09 | 2.8 | 0.99 |

 示例

  1.  
     
  2.  
    ##生成数据
  3.  
     
  4.  
    #' economyCompare = data.frame(refer, liaoning, shandong, jiangsu, zhejiang, fujian, guangdong)
  5.  
     
  6.  
    #
  7.  
    # 异常控制 #
  8.  
    if (any(is.na(df))) stop("'df' have NA" )
  9.  
    if (distingCoeff<0 | distingCoeff>1) stop("'distingCoeff' must be in range of [0,1]" )
  10.  
     
  11.  
     
  12.  
     
  13.  
     
  14.  
    diff = X #设置差学列矩阵空间
  15.  
     
  16.  
    for (i in
  17.  
    mx = max(diff)
  18.  
     
  19.  
     
  20.  
    #计算关联系数#
  21.  
    relations = (mi+distingCoeff*mx) / (diff + distingCoeff*mx)
  22.  
     
  23.  
    #计算关联度#
  24.  
    # 暂时简单处理, 等权
  25.  
    relDegree = rep(NA, nc)
  26.  
    for (i in 1:nc) {
  27.  
    relDegree[i] = mean(relations[,i]) # 等权
  28.  
    }
  29.  
     
  30.  
     
  31.  
    #排序: 按关联度大到小#
  32.  
    X_order = X[order(relDegree,
  33.  
    relDes = rep(NA, nc) #分配空间 关联关系描述(说明谁和谁的关联度)
  34.  
    X_names = names(X_o
  35.  
    names(relationalDegree) = relDes
  36.  
     
  37.  
     
  38.  
    if (cluster) {
  39.  
     
  40.  
    greyRelDegree = GRA(economyC
  41.  
     
  42.  
     
  43.  
    # 得到差异率矩阵 #
  44.  
    grey_diff = matrix(0
  45.  
     
  46.  
    grey_diff[i,j] = abs(rel
  47.  
    #得到距离矩阵#
  48.  
    grey_dist = matrix(0, nrow
  49.  
    iff[i,j]+grey_diff[j,i]
  50.  
    }
  51.  
    }
  52.  
     
  53.  
    # 得到灰色相关系数矩阵 #
  54.  
    grey_dist_max = max(grey_dist)
  55.  
    grey_correl = matrix(0, nrow = nc, ncol = nc)
  56.  
    for (i in 1:nc) {
  57.  
    for (j in 1:nc) {
  58.  
    grey_correl[i,j] = 1 - grey_dist[i,j] / grey_dist_max
  59.  
    }
  60.  
    }
  61.  
     
  62.  
     
  63.  
     
  64.  
    d = as.dist(1-grey_correl) # 得到无对角线的下三角矩阵(数值意义反向了, 值越小表示越相关 )
  65.  
    # 主对角线其实表示了各个对象的相近程度, 画图的时候, 相近的对象放在一起
  66.  
     
  67.  
    hc = hclust(d, method = clusterMethod) # 系统聚类(分层聚类)函数, single: 单一连接(最短距离法/最近邻)
  68.  
    # hc$height, 是上面矩阵的对角元素升序
  69.  
    # hc$order, 层次树图上横轴个体序号
  70.  
    plot(hc,hang=-1) #hang: 设置标签悬挂位置
  71.  
     
  72.  
    }
  73.  
     
  74.  
    #输出#
  75.  
     
  76.  
    if (cluster) {
  77.  
    lst = list(relationalDegree=relationalDegree,
  78.  
     
  79.  
    return(lst)
  80.  
     
  81.  
    }
  82.  
    ```
  83.  
     
  84.  
     
  85.  
     
  86.  
    ```{r}
  87.  
    ## 生成数据
  88.  
    rownames(economyCompare) = c("indGV", "indVA", "profit", "incomeTax")
  89.  
    ## 灰色关联度
  90.  
    greyRelDegree = greya(economyCompare)
  91.  
    greyRelDegree
  92.  
    ```
  93.  
     
  94.  
     
  95.  
     

灰色关联度 

 

灰色聚类,如层次聚类 

  1.  
     
  2.  
    ## 灰色聚类
  3.  
     
  4.  
    greya(economyCompare, cluster = T)
  5.  
     

 


最受欢迎的见解

1.R语言k-Shape算法股票价格时间序列聚类

2.R语言中不同类型的聚类方法比较

3.R语言对用电负荷时间序列数据进行K-medoids聚类建模和GAM回归

4.r语言鸢尾花iris数据集的层次聚类

5.Python Monte Carlo K-Means聚类实战

6.用R进行网站评论文本挖掘聚类

7.用于NLP的Python:使用Keras的多标签文本LSTM神经网络

8.R语言对MNIST数据集分析 探索手写数字分类数据

9.R语言基于Keras的小数据集深度学习图像分类

原文地址:https://www.cnblogs.com/tecdat/p/13838888.html