利用R轧制期间情节网络图

问题描述 投票:0回答:1

我试图进行网络图,并通过滚动的三年期的一些统计数据,但我不知道如何设置滚动功能。下面是我没有滚动编码。

> library(igraph)
> em <- read.csv(file.choose(), header = TRUE )
> network <- graph.data.frame(em, directed = TRUE )
> em
   sender receiver weights year
1       a        d       2 2001
2       b        a       3 2001
3       c        a       1 2001
4       d        h       1 2001
5       e        d       3 2001
6       a        b       1 2002
7       b        c       2 2002
8       c        d       1 2002
9       d        a       1 2002
10      e        h       2 2002
11      a        d       1 2003
12      b        d       1 2003
13      c        a       2 2003
14      d        h       2 2003
15      e        a       1 2003
16      a        d       1 2004
17      b        c       1 2004
18      c        d       2 2004
19      d        c       1 2004
20      e        a       2 2004
> E(network)$weight <- as.numeric(network[,3])
Warning message:
In eattrs[[name]][index] <- value :
  number of items to replace is not a multiple of replacement length
> p <- plot (network,edge.width=E(network)$weight)

因此,在这个例子中,最终会拿出一个称重网络图。我想在2001 - 2003年和2002- 2004年子样本一些SNA统计进行使用数据的图表,更进一步。一些网上资源建议-rollappy() - 或包-tidyquant-能做到的功能,但我没能弄清楚我们如何认识的第4列的一年,如何设置滚动周期。将非常感激,如果任何人都可以帮忙,因为我是一个新手,R.

非常感谢!!

r igraph rolling-computation
1个回答
0
投票

感谢@ emilliman5了进一步的问题。

我的真实数据集是一个不平衡的面板与15年的时间跨度。我的目的是要进行使用完整数据的一部分网络图。减去的规则是3年滚动周期(其实与其他一些条件,但我刚才问滚动这里)。所以我打算先请滚动子样本,并进行图表。我希望现在是有点清晰。

以上数据只是一个模拟样品。 4年的范围应该产生两个图(2001- 2003年,2002- 2004年),15年应该拿出13个图。真正的加权变量不叫重量,但我同意行“as.numeric(网络[1,3])”是多余的。 (我知道我现在做的例子并不好...对不起...)

我找人帮我,现在,所以我只是张贴一些代码。希望它可以帮助其他人。

方法1:通过功能调用子样本。这救我从图形构建嵌套循环在一起。

# Function: conditions for substracting; here rolling-year only
f <- function(j){                 
df <- subset(em, year>=j & year <= j+2)
}

print (f(2001))                  # Test function output, print-out

# Rolling by location and year, graph-plotting
for (j in 2001:2002){
sdf = f(j)                              
nw <- graph.data.frame(sdf, directed = TRUE)
plot(nw, edge.width = E(sdf[[j]])$weight)
}

方法2:使用循环 - 罚款一两个减去条件,但将是更有点笨拙。

c <- 2001                       # a number for year count
sdf <- {}                       # Set an empty set to save subsets
for (j in 2001:2002){
df_temp <- subset(em, year>=j & year<=j+2)
print(nrow(df_temp))            # To check the subset, not necessary
sdf[[c]] <- cbind(df_temp)
nw <- graph.data.frame(sdf[[c]], directed = TRUE)
plot(nw, edge.width = E(sdf[[c]])$weight)
c <- c + 1
}
© www.soinside.com 2019 - 2024. All rights reserved.