使用面板数据计算社交网络描述性统计数据

Computing social network descriptive stats with panel data

我有在不同时间点在不同医院工作的医生的信息。我想在医院月级别定义网络,以便同行是同时在同一家医院一起工作的医生。由于一名医生可以同时在多家医院工作,因此网络组部分重叠。

然后,我想在月份级别计算生成的网络的基本描述(例如平均度数、密度、聚类),以查看它们如何随时间变化。

考虑医院 x-y-w、周期 1-2 和医生 A-B-C-D 的非常简单的例子。

这在下面的数据框中表示。

mydf <- data.frame(hospital = c("x","x","x","x","x","y","y","y","w","w","w","w"), 
                period = c(1,1,1,2,2,1,2,2,1,1,2,2), 
                physician = c("A","B","C","A","B","A","A","C","C","D","A","D"))

现在,我已经编写了下面的代码。首先,我在给定的住院月中为每一对医生构建了一个数据框。其次,我过滤每个时期的货币对。第三,我为每个时期定义一个 igraph 对象。第四,我绘制图表并计算每个时期的描述性统计数据(例如密度)。有没有更简单的方法来做到这一点?如果不是,我怎么能在很多时期内自动执行此代码?

relations <- mydf %>%
  rename(from = physician) %>%
  left_join(mydf, by=c("hospital","period")) %>%
  rename(to = physician) %>%
  filter(from!=to) %>%
  relocate(from, to)

relations_1 <- relations %>%
  filter(period==1)

relations_2 <- relations %>%
  filter(period==2)

g1 <- simplify(graph_from_data_frame(relations_1, directed=FALSE, vertices=NULL))
g2 <- simplify(graph_from_data_frame(relations_2, directed=FALSE, vertices=NULL))

plot(g1)
plot(g2)

degree(g1)
degree(g2)

更新

您可以使用以下代码创建g1g2degree1degree2

rlst <- split(relations, relations$period)
for (i in seq_along(rlst)) {
  assign(paste0("g", i), simplify(graph_from_data_frame(rlst[[i]], directed = FALSE, vertices = NULL)), envir = .GlobalEnv)
  assign(paste0("degree", i), degree(get(paste0("g", i))), envir = .GlobalEnv)
}

你是这个意思吗?

relations %>%
  group_by(period) %>%
  group_map(~ degree(simplify(graph_from_data_frame(.x, directed = FALSE))))

这给出了

[[1]]
A B C D
2 2 3 1

[[2]]
A B C D
3 1 1 1