繁体   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