Home > Blockchain >  Access centrality measures attributes in list of networks
Access centrality measures attributes in list of networks

Time:07-26

I am studying the evolution of weighted and undirected networks over time. I have managed to create a list of networks and to compute different centrality measures stored as node attributes, but I don't know how to extract those to study them in more depth.

Here is a small sample of my data:

el_cc <- data.frame(from = c("BEL", "LUX", "FRN", "UKG","BEL", "LUX", "FRN", "UKG"),
                    to = c("FRN", "UKG", "DEN", "CND","FRN", "UKG", "DEN", "CND"),
                    weight = c(1,2,3,4,5,6,7,8),
                    year = c(2010,2010,2010,2010,2011,2011,2011,2011))

To prepare the data and create the graphs, I run

cc <- split(el_cc, el_cc$year)

get.graphs <- function(data){
  # Select columns
  data[(names(data) %in% c("from", "to", "weight"))]
  
  # Remove duplicate dyads
  data <- data %>% 
    rowwise() %>% 
    mutate(tmp = paste(sort(c(from,to)), collapse = ''))
  data <- data[!duplicated(data$tmp),]
  data$tmp <- NULL
  
  # Create graph
  g <- graph_from_data_frame(data, directed = FALSE)

  # Degree centrality
  g$degreecent <- degree(g, mode="total", normalized = TRUE)
  
  # Betweenness centrality
  g$betweenness <- betweenness(g, v = V(g), directed = FALSE, nobigint = TRUE, normalized = TRUE)
  
  # k-core
  g$kcore <- coreness(g, mode="all")
  
  return(g)
}

graph.cc <- lapply(cc, get.graphs)

If I try to view elements of graph.cc, I get an error

Error in adjacent_vertices(x, i, mode = if (directed) "out" else "all") : 
  At iterators.c:763 : Cannot create iterator, invalid vertex id, Invalid vertex id
Error in adjacent_vertices(x, i, mode = if (directed) "out" else "all") : 
  At iterators.c:763 : Cannot create iterator, invalid vertex id, Invalid vertex id

But if I run the line below, it seems to be fine.

graph.cc$`2011`

But then, from here, I don't know how to access the centrality measures I computed. I have seen plenty of tutorials that show how to use the attributes to plot the network, but I would like to store them in a dataframe for instance or in a list of dataframes. If I was not using a list but if I was working one year at the time, here is what I would do:

# For one year at a time

degreecent <- degree(g, mode="total", normalized = TRUE)
betweenness <- betweenness(g, v = V(g), directed = FALSE, nobigint = TRUE, normalized = TRUE)
kcore <- coreness(g, mode="all")
Centrality_Measures <- data.frame(degreecent, betweenness , kcore , stringsAsFactors=FALSE)

Ideally, I would like to build on this to obtain a list of dataframe with the centrality measures for all nodes over the years. Many thanks in advance!

CodePudding user response:

You cannot see the graphs because graph.cc is not a graph it's a list.
To create a data.frame with the centrality measures wrap the code that works in the single case as a function and lapply it to the graphs' list.

suppressPackageStartupMessages({
  library(igraph)
  library(dplyr)
})

centrality_measures <- function(g) {
  degreecent <- degree(g, mode="total", normalized = TRUE)
  betweenness <- betweenness(g, v = V(g), directed = FALSE, normalized = TRUE)
  kcore <- coreness(g, mode="all")
  Centrality_Measures <- data.frame(degreecent, betweenness, kcore, stringsAsFactors=FALSE)
  Centrality_Measures
}

lapply(graph.cc, centrality_measures)
#> $`2010`
#>     degreecent betweenness kcore
#> BEL        0.2         0.0     1
#> LUX        0.2         0.0     1
#> FRN        0.4         0.1     1
#> UKG        0.4         0.1     1
#> DEN        0.2         0.0     1
#> CND        0.2         0.0     1
#> 
#> $`2011`
#>     degreecent betweenness kcore
#> BEL        0.2         0.0     1
#> LUX        0.2         0.0     1
#> FRN        0.4         0.1     1
#> UKG        0.4         0.1     1
#> DEN        0.2         0.0     1
#> CND        0.2         0.0     1

Created on 2022-07-26 by the reprex package (v2.0.1)

  • Related