R語言使用灰色關(guān)聯(lián)分析(Grey Relation Analysis,GRA)中國(guó)經(jīng)濟(jì)社會(huì)發(fā)展指標(biāo)
原文鏈接:http://tecdat.cn/?p=16881
原文出處:拓端數(shù)據(jù)部落公眾號(hào)
?
灰色關(guān)聯(lián)分析包括兩個(gè)重要功能。
第一項(xiàng)功能:灰色關(guān)聯(lián)度,與correlation系數(shù)相似,如果要評(píng)估某些單位,在使用此功能之前轉(zhuǎn)置數(shù)據(jù)。第二個(gè)功能:灰色聚類,如層次聚類。?
灰色關(guān)聯(lián)度
灰色關(guān)聯(lián)度有兩種用法。該算法用于測(cè)量?jī)蓚€(gè)變量的相似性,就像`cor`一樣。如果要評(píng)估某些單位,可以轉(zhuǎn)置數(shù)據(jù)集。
*一種是檢查兩個(gè)變量的相關(guān)性,數(shù)據(jù)類型如下:
| 參考| 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之間的灰色關(guān)聯(lián)度...近似地測(cè)量reference和v1的相似度。
*另一個(gè)是評(píng)估某些單位的好壞。
| 單位| 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 |
?示例
##生成數(shù)據(jù)
#' economyCompare = data.frame(refer, liaoning, shandong, jiangsu, zhejiang, fujian, guangdong)
#
# 異常控制 #
if (any(is.na(df))) stop("'df' have NA" )
if (distingCoeff<0 | distingCoeff>1) stop("'distingCoeff' must be in range of [0,1]" )
diff = X ?#設(shè)置差學(xué)列矩陣空間
for (i in
mx = max(diff)
#計(jì)算關(guān)聯(lián)系數(shù)#
relations = (mi+distingCoeff*mx) / (diff + distingCoeff*mx)
#計(jì)算關(guān)聯(lián)度#
# 暫時(shí)簡(jiǎn)單處理, 等權(quán)
relDegree = rep(NA, nc)
for (i in 1:nc) {
relDegree[i] = mean(relations[,i]) ?# 等權(quán)
}
#排序: 按關(guān)聯(lián)度大到小#
X_order = X[order(relDegree,
relDes = rep(NA, nc) #分配空間 ?關(guān)聯(lián)關(guān)系描述(說明誰和誰的關(guān)聯(lián)度)
X_names = names(X_o
names(relationalDegree) = relDes
if (cluster) {
greyRelDegree = GRA(economyC
# 得到差異率矩陣 #
grey_diff = matrix(0
grey_diff[i,j] = abs(rel
#得到距離矩陣#
grey_dist = matrix(0, nrow
iff[i,j]+grey_diff[j,i]
}
}
# 得到灰色相關(guān)系數(shù)矩陣 #
grey_dist_max = max(grey_dist)
grey_correl = matrix(0, nrow = nc, ncol = nc)
for (i in 1:nc) {
for (j in 1:nc) {
grey_correl[i,j] = 1 - grey_dist[i,j] / grey_dist_max
}
}
d = as.dist(1-grey_correl) ?# 得到無對(duì)角線的下三角矩陣(數(shù)值意義反向了, 值越小表示越相關(guān) )
# 主對(duì)角線其實(shí)表示了各個(gè)對(duì)象的相近程度, 畫圖的時(shí)候, 相近的對(duì)象放在一起
hc = hclust(d, method = clusterMethod) ?# 系統(tǒng)聚類(分層聚類)函數(shù), single: 單一連接(最短距離法/最近鄰)
# hc$height, 是上面矩陣的對(duì)角元素升序
# hc$order, 層次樹圖上橫軸個(gè)體序號(hào)
plot(hc,hang=-1) ?#hang: 設(shè)置標(biāo)簽懸掛位置
}
#輸出#
if (cluster) ?{
lst = list(relationalDegree=relationalDegree,
return(lst)
}
```
```{r}
## 生成數(shù)據(jù)
rownames(economyCompare) = c("indGV", "indVA", "profit", "incomeTax")
## 灰色關(guān)聯(lián)度
greyRelDegree = greya(economyCompare)
greyRelDegree
```
灰色關(guān)聯(lián)度?

?

灰色聚類,如層次聚類?
## 灰色聚類
greya(economyCompare, cluster = T)
?
?


最受歡迎的見解
1.R語言k-Shape算法股票價(jià)格時(shí)間序列聚類
2.R語言中不同類型的聚類方法比較
3.R語言對(duì)用電負(fù)荷時(shí)間序列數(shù)據(jù)進(jìn)行K-medoids聚類建模和GAM回歸
4.r語言鳶尾花iris數(shù)據(jù)集的層次聚類
5.Python Monte Carlo K-Means聚類實(shí)戰(zhàn)
6.用R進(jìn)行網(wǎng)站評(píng)論文本挖掘聚類
7.用于NLP的Python:使用Keras的多標(biāo)簽文本LSTM神經(jīng)網(wǎng)絡(luò)
8.R語言對(duì)MNIST數(shù)據(jù)集分析 探索手寫數(shù)字分類數(shù)據(jù)
9.R語言基于Keras的小數(shù)據(jù)集深度學(xué)習(xí)圖像分類
?