如何在NetLogo中创建图形对象
我想使用NetLogo的R扩展将一个图形对象发送到R中,然后使用iGraph包计算并返回一些度量。iGraph可以从邻接矩阵或边列表创建图形(也有其他选项)。我想发送给R的图形只是一个agentset之间的链接。有人知道怎么做吗?NW:save矩阵将邻接矩阵导出到文件。我需要这样做,然后将文件读回R,还是有更直接的方法如何在NetLogo中创建图形对象,netlogo,Netlogo,我想使用NetLogo的R扩展将一个图形对象发送到R中,然后使用iGraph包计算并返回一些度量。iGraph可以从邻接矩阵或边列表创建图形(也有其他选项)。我想发送给R的图形只是一个agentset之间的链接。有人知道怎么做吗?NW:save矩阵将邻接矩阵导出到文件。我需要这样做,然后将文件读回R,还是有更直接的方法 一如既往,谢谢你 我过去所做的是在NetLogo中构建网络,将网络导出到R,在R中计算网络度量,然后检索度量。我在其中一个项目中使用的相关代码是: to export-nw2r
一如既往,谢谢你 我过去所做的是在NetLogo中构建网络,将网络导出到R,在R中计算网络度量,然后检索度量。我在其中一个项目中使用的相关代码是:
to export-nw2r
; create file with useful graph format
nw:set-context people links
let filename (word "Networks/netlogo" behaviorspace-run-number ".gml")
export-simple-gml filename
;; reset the R-workspace
r:clearLocal
let dir pathdir:get-model
r:eval "library(igraph)"
; read network in R
set filename (word dir "/" filename)
r:put "fn" filename
r:eval "gg <- read_graph(file = fn, format = 'gml')"
r:eval "V(gg)$name <- V(gg)$id" ; gml uses 'id', but igraph uses 'name'
r:eval "if (file.exists(fn)) file.remove(fn)"
end
to calc-network-properties
r:eval "library(ineq)"
; network size
set sizeN count people
set sizeE count links
output-type "Nodes: " output-print sizeN
output-type "Edges: " output-print sizeE
; calculate degree properties
r:eval "degs <- degree(gg)"
r:eval "aveDeg <- mean(degs)"
set aveDeg r:get "aveDeg"
output-type "Mean Degree: " output-print precision aveDeg 2
r:eval "giniDeg <- ineq(degs, type = \"Gini\")"
set giniDeg r:get "giniDeg"
output-type "Gini of Degree: " output-print precision giniDeg 2
; calculate transitivity properties
r:eval "lccs <- transitivity(gg, type = \"localundirected\")"
r:eval "aveCC <- mean(lccs, na.rm = TRUE)"
set aveCC r:get "aveCC"
output-type "Mean Clustering: " output-print precision aveCC 2
r:eval "trans <- transitivity(gg, type = \"undirected\")"
set trans r:get "trans"
output-type "Transitivity: " output-print precision trans 2
; paths and betweenness
r:eval "paths <- distances(gg)"
r:eval "paths <- paths[upper.tri(paths)]"
r:eval "avePath <- mean(paths)"
set avePath r:get "avePath"
output-type "Mean Shortest Path: " output-print precision avePath 2
r:eval "diam <- max(paths)"
set diam r:get "diam"
output-type "Max Shortest Path: " output-print diam
r:eval "giniPaths <- ineq(paths, type = \"Gini\")"
set giniPaths r:get "giniPaths"
output-type "Gini of Paths: " output-print precision giniPaths 2
r:eval "btws <- betweenness(gg)"
r:eval "giniBtwn <- ineq(btws, type = \"Gini\")"
set giniBtwn r:get "giniBtwn"
output-type "Gini of Betweenness (V): " output-print precision giniBtwn 2
end
导出-nw2r
; 使用有用的图形格式创建文件
nw:设置上下文人员链接
让文件名(单词“Networks/netlogo”behaviorspace运行编号“.gml”)
导出简单gml文件名
;; 重置R工作空间
r:clearLocal
let dir pathdir:get model
r:eval“图书馆(igraph)”
; R中的读取网络
设置文件名(word目录“/”文件名)
r:放入“fn”文件名
r:eval“gg我过去所做的是在NetLogo中构建网络,将网络导出到r,计算r中的网络度量,然后检索度量。我在其中一个项目中使用的相关代码是:
to export-nw2r
; create file with useful graph format
nw:set-context people links
let filename (word "Networks/netlogo" behaviorspace-run-number ".gml")
export-simple-gml filename
;; reset the R-workspace
r:clearLocal
let dir pathdir:get-model
r:eval "library(igraph)"
; read network in R
set filename (word dir "/" filename)
r:put "fn" filename
r:eval "gg <- read_graph(file = fn, format = 'gml')"
r:eval "V(gg)$name <- V(gg)$id" ; gml uses 'id', but igraph uses 'name'
r:eval "if (file.exists(fn)) file.remove(fn)"
end
to calc-network-properties
r:eval "library(ineq)"
; network size
set sizeN count people
set sizeE count links
output-type "Nodes: " output-print sizeN
output-type "Edges: " output-print sizeE
; calculate degree properties
r:eval "degs <- degree(gg)"
r:eval "aveDeg <- mean(degs)"
set aveDeg r:get "aveDeg"
output-type "Mean Degree: " output-print precision aveDeg 2
r:eval "giniDeg <- ineq(degs, type = \"Gini\")"
set giniDeg r:get "giniDeg"
output-type "Gini of Degree: " output-print precision giniDeg 2
; calculate transitivity properties
r:eval "lccs <- transitivity(gg, type = \"localundirected\")"
r:eval "aveCC <- mean(lccs, na.rm = TRUE)"
set aveCC r:get "aveCC"
output-type "Mean Clustering: " output-print precision aveCC 2
r:eval "trans <- transitivity(gg, type = \"undirected\")"
set trans r:get "trans"
output-type "Transitivity: " output-print precision trans 2
; paths and betweenness
r:eval "paths <- distances(gg)"
r:eval "paths <- paths[upper.tri(paths)]"
r:eval "avePath <- mean(paths)"
set avePath r:get "avePath"
output-type "Mean Shortest Path: " output-print precision avePath 2
r:eval "diam <- max(paths)"
set diam r:get "diam"
output-type "Max Shortest Path: " output-print diam
r:eval "giniPaths <- ineq(paths, type = \"Gini\")"
set giniPaths r:get "giniPaths"
output-type "Gini of Paths: " output-print precision giniPaths 2
r:eval "btws <- betweenness(gg)"
r:eval "giniBtwn <- ineq(btws, type = \"Gini\")"
set giniBtwn r:get "giniBtwn"
output-type "Gini of Betweenness (V): " output-print precision giniBtwn 2
end
导出-nw2r
;使用有用的图形格式创建文件
nw:设置上下文人员链接
让文件名(单词“Networks/netlogo”behaviorspace运行编号“.gml”)
导出简单gml文件名
;重置R工作空间
r:clearLocal
let dir pathdir:get model
r:eval“图书馆(igraph)”
;在R中读取网络
设置文件名(word目录“/”文件名)
r:放入“fn”文件名
r:eval“gg