簡體   English   中英

如何建立基於相關性的網絡?

[英]how to make a network based on correlation?

我有一個如下的矩陣

data <- replicate(30, rnorm(30)) 

我想建立一個類似此圖片的網絡http://www.forwardprogress.net/data-steps-effective-relationship-marketing-dean-delisle/

當然,變量的名稱(在這種情況下,出現了V1至V30)

有什么辦法可以在R中做到嗎?

謝謝

您的問題很不確定。 但是這樣的事情應該可以幫助您入門:

# Generate some toy data
data <- replicate(30, rnorm(30)) 

library("igraph")  # Load the igraph package

corr <- cor(data)  # Create weighted adjencency/correlation matrix

# Create a weighted complete graph from the correlation matrix
g <- graph.adjacency(corr, mode = "undirected", weighted = TRUE, diag = FALSE)

# Chose the layout function
custom.layout <- function(g, ...) {
  # layout.drl(g, weights = E(g)$weight, ...) # For bigger graphs
  layout.fruchterman.reingold(g, weights = E(g)$weight, ...)
}

看一看?layout.fruchterman.reingold和其他布局函數來調整布局。

# Format edges
E(g)$cor <- E(g)$weight
E(g)$weight <- abs(E(g)$cor)
E(g)$color <- ifelse(E(g)$cor < 0, "blue", "red")
E(g)$width <- 3*atanh(E(g)$weight)

# Format vertices
V(g)$size <- 3*abs(rowSums(corr))
V(g)$color <- "grey"
V(g)$label.color <- "black"
V(g)$label <- ""

# Do the plot
plot(g, layout = custom.layout)

Imgur

現在,它看起來與您呈現的圖形不太相似。 首先,由於我們模擬玩具數據的方式,我們不希望任何“集線器”-一切都只是噪音。 其次,布局高度依賴於布局功能。 第三,這只是為您提供有關如何自定義圖形和布局的想法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM