मैं एक नेटवर्क के लिए एक डिग्री सहसंबंध मैट्रिक्स बनाना चाहता हूं, जहां कॉलम और पंक्तियां नेटवर्क की डिग्री को कैप्चर करती हैं। मैं एक वैश्विक माप की तलाश नहीं कर रहा हूं - जैसे assortativity_degree(), लेकिन एक वास्तविक सहसंबंध मैट्रिक्स, जहां मैट्रिक्स में प्रत्येक तत्व किनारों की संख्या है जो कि डिग्री के साथ नोड्स के लिए ग्राफ में मौजूद हैं = जो भी और डिग्री = जो भी हो। मैंने igraph प्रलेखन में चारों ओर खोदा है और गुगल किया है लेकिन मुझे जो चाहिए वह काफी कुछ नहीं मिलता है। मैंने निम्नलिखित को एक साथ जोड़ दिया है, जो काम करने लगता है, लेकिन मैं सोच रहा हूं कि क्या कोई और सरल तरीका है जिससे मैं अनजान हूं। मुझे नहीं लगता कि मैं जो चाहता हूं वह इतना गूढ़ है कि किसी और ने इसके बारे में नहीं सोचा है - और शायद इग्राफ या इसी तरह का कोई फ़ंक्शन है और मुझे नहीं पता कि इसे क्या कहा जाता है।
library(igraph)
#>
#> Attaching package: 'igraph'
#> The following objects are masked from 'package:stats':
#>
#> decompose, spectrum
#> The following object is masked from 'package:base':
#>
#> union
g <- make_graph("Zachary")
x <- sort(unique(degree(g))) # vector of all degrees in the network
y <- sort(unique(degree(g))) # vector for all degrees in the network
datalist = list()
# this loop creates a vector that identifies the number of
# edges that occur between nodes of degree whatever and degree whatever
for(i in y) {
row <- mapply(function(x)
{length(E(g)[V(g)[degree(g) == i] %--% V(g)[degree(g) == x]])},
x)
datalist[[i]] <- row
}
# takes the data list created in the previous for loop and row bind it into a
# matrix
m = do.call(rbind, datalist)
# label rows and columns with the relevatn degree
rownames(m) <- unique(sort(degree(g)))
colnames(m) <- unique(sort(degree(g)))
m
#> 1 2 3 4 5 6 9 10 12 16 17
#> 1 0 0 0 0 0 0 0 0 0 1 0
#> 2 0 0 0 3 0 1 2 1 5 3 7
#> 3 0 0 2 3 1 3 1 1 0 3 2
#> 4 0 3 3 1 3 1 2 2 2 3 3
#> 5 0 0 1 3 0 1 1 2 2 2 3
#> 6 0 1 3 1 1 0 1 1 1 2 1
#> 9 0 2 1 2 1 1 0 1 0 1 0
#> 10 0 1 1 2 2 1 1 0 1 1 0
#> 12 0 5 0 2 2 1 0 1 0 0 1
#> 16 1 3 3 3 2 2 1 1 0 0 0
#> 17 0 7 2 3 3 1 0 0 1 0 0
2021-06-19 को reprex पैकेज द्वारा बनाया गया (v2.0.0)
1 उत्तर
हम डिग्री का ग्राफ बनाकर नीचे जैसा कुछ कर सकते हैं, यानी g.dg
dg <- degree(g)
g.dg <- graph_from_data_frame(
with(
get.data.frame(g),
data.frame(dg[from], dg[to])
),
directed = FALSE
)
mat <- get.adjacency(g.dg, sparse = FALSE)
ord <- order(as.numeric(row.names(mat)))
out <- mat[ord, ord]
जो देता है
1 2 3 4 5 6 9 10 12 16 17
1 0 0 0 0 0 0 0 0 0 1 0
2 0 0 0 3 0 1 2 1 5 3 7
3 0 0 2 3 1 3 1 1 0 3 2
4 0 3 3 1 3 1 2 2 2 3 3
5 0 0 1 3 0 1 1 2 2 2 3
6 0 1 3 1 1 0 1 1 1 2 1
9 0 2 1 2 1 1 0 1 0 1 0
10 0 1 1 2 2 1 1 0 1 1 0
12 0 5 0 2 2 1 0 1 0 0 1
16 1 3 3 3 2 2 1 1 0 0 0
17 0 7 2 3 3 1 0 0 1 0 0
get.data.frame(g)
में दो कॉलम हैं,from
औरto
।with
का उपयोग करके, हमget.data.frame(g)
के वातावरण में प्रवेश करते हैं औरfrom
औरto
को डेटा के नाम के रूप में उपयोग कर सकते हैं (get.data.frame(g)$from
याget.data.frame(g)$to
के बजाय) . यदि आप?with
टाइप करते हैं, तो आप अधिक उदाहरण और स्पष्टीकरण देखेंगे।