【问题标题】:Eigenvector values for different time periods of same network (igraph in R)同一网络不同时间段的特征向量值(R中的igraph)
【发布时间】:2023-03-07 03:01:01
【问题描述】:

这里是完整的R菜鸟,所以请多多包涵!

我有特定时期内国家间贸易的对年数据。我正在尝试计算 1946-2014 年期间每个单独年份中每个国家/地区的特征向量中心值。其次,我想将所有这些特征值(带有案例标签和年份)整齐地打包在一个可以导出为 CSV 的数据框中。

以边缘为例:

links <- structure(list(ccode1 = c(2L, 3L, 4L, 5L, 2L, 3L, 4L, 5L, 2L, 
3L, 4L, 5L), ccode2 = c(5L, 4L, 3L, 2L, 5L, 4L, 3L, 2L, 
5L, 4L, 3L, 2L), year = c(1960, 1960, 1960, 1960, 1961, 1961, 1961, 1961, 1962, 1962, 1962, 1962), weight = c(1347.34, 778.42999, 
866.85999, 1014.14, 895.46002, 1082.0699, 1584.7, 1193.37, 1355.3101, 
1348.75, 3653.54, 616.98999)), row.names = c(NA, 12L), class = "data.frame")

网络将按如下方式构建:

network <- graph_from_data_frame(links, directed = FALSE, vertices = NULL)

特征值的计算方式如下:

trade.eigen <- eigen_centrality(network, directed = FALSE)

1.如何自动计算每个国家/地区每年的特征值?

2。我如何将所有这些值与国家标签和年份结合在一个数据框中?

【问题讨论】:

    标签: r igraph eigenvector network-analysis


    【解决方案1】:

    感谢您提供了一个易于重现的示例。如果我正确理解您的问题,您需要做的就是:

    1. 每年迭代一次
    2. 过滤掉没有与您正在迭代的年份关联的边缘属性的边缘
    3. 计算过滤图的特征值
    4. 将输出存储在单个数据帧中

    tidyverse 系列软件包有很多实用功能可以让这一切变得简单。使用 ma​​p 进行迭代,使用 enframe 将格式从 key-value 格式更改为 data frame 格式,然后使用unnest 来清理。

    # install.packages('tidyverse')
    library(tidyverse)
    
    
    #let's get all unique values for year
    #we can do this by pulling the edge attribute
    #"year" frome the graph "network"
    years <- E(network)$year %>%
      unique
    
    
    #now we want to use purrr's map to iterate through all the years
    #the goal is to only keep edges from a year we are interested in
    #"map" returns a list, and if we use the function "setNames", then
    #each item in the list will be named after the object we are iterating
    eigen_by_year <- purrr::map(setNames(years, years), function(yr){
      #here we filter away all edges that aren't from the year we are interested
      network_filtered = network - E(network)[year != yr]
    
      #we now calculate the eigen values for the filtered network
      eigen_values <- eigen_centrality(network_filtered, directed = F)$vector
    
      #"eigen_values" is a named vector, let's convert this named vector
      #into a data frame with the name column being the vertex name
      #and the value column being the eigen value
      tibble::enframe(eigen_values)
    })
    
    #The result is a list where the item names are the years
    #and they contain a data frame of the eigen values associated
    #with their years
    eigen_by_year
    
    #let's use enframe one more time so that the name of the list items
    #are now their own "name" column and the nested data rames are
    #in the "value" column" we will need to use unnest to flatten the dataframe
    eigen_data_frame <- eigen_by_year %>%
      tibble::enframe() %>%
      tidyr::unnest()
    
    eigen_data_frame
    

    我希望这会有所帮助。

    【讨论】:

    • 就是这样!感谢您提供简单而优雅的解决方案!
    猜你喜欢
    • 2021-07-01
    • 2020-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-09
    • 2013-05-15
    • 1970-01-01
    相关资源
    最近更新 更多