Question

I am currently working on dynamic temporal network.

Header: Time Sender Receiver

      1    1       2
      1    1       3
      2    2       1
      2    2       1
      3    1       2
      3    1       2

The above is a sample of my dataset. There are 3 time periods (sessions) and the edgelists between nodes. I want to compute centrality measures by each time period. I am thinking about writing a script that compute centrality measures within the same period of the time. However I am just wondering whether there might be R libraries that can handle this problem.

Is there anyone who knows about?

Jinie

I tried to write the code for subsetting data based on Time as follows:

uniq <-unique(unlist(df$Time))

uniq
[1] 1 2 3

for (i in 1:length(uniq)){

  t[i]<-subset(df, Time==uniq[i])

  net[i] <-as.matrix(t[i])

  netT[i]<-net[i][,-3]  #removing time column

  #### getting edgelist

  netT[i][,1]=as.character(net[i][,1])

  netT[i][,2]=as.character(net[i][,2])

  g [i]=graph.edgelist(netT [i], directed=T)

  g[i] 
}

however, I've got a error message ( Error in t[i] <- subset(df, Time == uniq[i]) : object of type 'closure' is not subsettable) Do you know why? I am kind of new to R so it is hard to figure it out. I guess t[i] is the problem. I don't know how to assign t[i] as a data frame.

Was it helpful?

Solution

The networkDynamic R library is helpful for this sort of thing (disclaimer: I'm a package maintainer)

library(networkDynamic)

# a data frame with your input data
raw<-data.frame(time=c(1,1,2,2,3,3),
              sender=c(1,1,2,2,1,1),
            receiver=c(2,3,1,1,2,2))

# add another time column to define a start and end time for each edge spell
raw2<-cbind(raw$time,raw$time+1,raw$sender,raw$receiver)

# create a networkDynamic object using this edge timing info
nd<-networkDynamic(edge.spells=raw2)

# load the sna library with static network measures
library(sna)

# apply degree measure to static networks extracted at default time points
lapply(get.networks(nd),degree)
 [[1]]
 [1] 2 1 1

 [[2]]
 [1] 1 1 0

 [[3]]
 [1] 1 1 0

OTHER TIPS

You could try the igraph library. I'm not familiar with it, but i find this question interesting enough to code up an answer, so here we go:

Because you've got a directed network (senders and receivers) you're going to need to two measures of centrality: indegree and outdegree.

Calculating this is fairly simple, the complication is splitting by time points. So for each time point we need to do the following:

  1. Create an adjacency matrix indicating for each row (sender) the number of connections to each column (receiver).
  2. From that we can simply add up the connections in the rows to get the outdegree, and the connections in the columns for the indegree.

Assuming your data is stored in a data.frame named df we can use split to split your data.frame by time point:

nodes <- unique(c(unique(df$Sender), unique(df$Receiver)))
centrality <- lapply(split(df, df$Time), function(time.df) {
  adj <- matrix(0, length(nodes), length(nodes), dimnames=list(nodes, nodes))
  for (i in 1:nrow(time.df)) {
    sender <- time.df[i, "Sender"]
    receiver <- time.df[i, "Receiver"]
    adj[sender, receiver] <- adj[sender, receiver] + 1 
  }
  list(indegree=colSums(adj), outdegree=rowSums(adj))
})
names(centrality) <- paste0("Time.Point.", 1:length(centrality))

If we run the code on your data (I've replaced the Senders and Receivers with letters for clarity):

> centrality
$Time.Point.1
$Time.Point.1$indegree
a b c 
0 1 1 

$Time.Point.1$outdegree
a b c 
2 0 0 


$Time.Point.2
$Time.Point.2$indegree
a b c 
2 0 0 

$Time.Point.2$outdegree
a b c 
0 2 0 


$Time.Point.3
$Time.Point.3$indegree
a b c 
0 2 0 

$Time.Point.3$outdegree
a b c 
2 0 0 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top