Why betweenness centrality differs in igraph? - r

I'm not a pro programmer, but I've been learning on my own. I have a problem with betweenness centrality. I have an undirected weighted graph of 28 actors, in an adjacency matrix. When I run the code betweenness(PG_Network4, v = V(PG_Network4), directed = FALSE, nobigint = TRUE, normalized = TRUE) the results differs significantly than those I ran in UCINET, Pajek, Gephi (all of them are the same results). What is weird is that, if I do not load my network into R as "weighted" then the results are the same. And the differences are really considerable, for instance, in igraph, those nodes at the 2nd, 3rd, 4th (betweenness degree) in UCINET, Gephi or Pajek passes to the 6th, 10th, and 13th positions. What am I doing wrong here? I thought it was the weighted specification, but UCINET considers that, and still maintain the same results than Pajek and Gephi.
Thank you so much.

The issue you're having is caused by the weighted edges in the graph. UCINET and the other programs make use of it automatically, but in igraph you need to specifically define it in the betweenness function. Add edge weight to your graph: E(g)$weight <- sum(ecount(g)).
Then you can call it in the function betweenness(g, weights = E(g)$weight).
This should resolve your issue and get your values to match up with the other programs.

Related

Correct way of calculating modularity for weighted graphs

I have about 13000 genes which I am trying to cluster using igraph as follows:
g.communities <- edge.betweenness.community(as.undirected(g), weights = E(g)$weight)
which returns 97 communities with modularity 0.9773353:
modularity(as.undirected(g), membership = g.communities$membership, weights = E(g)$weight)
#0.9773353
when I tried to custom made the number of communities as below I get modularity of 0.0094:
modularity(as.undirected(g), membership = cutat(g.communities, steps = 97), weights =
E(g)$weight)
#0.0094
Shouldn't these functions return similar results? Also, is it possible to use the above
function to find the correct number of clusters? (since by just increasing the steps the modularity always increases)
Finally g.communities$modularity returns a number for each vertex.
Can these numbers be interpreted as the correlation of each vertex to its corresponding module?
You are using the steps argument of cut_at. This does not specify the number of communities, but the number of merging steps to perform on the dendrogram. If you want 97 communities, use cut_at(g.communities, no=97) or simply cut_at(g.communities, 97).
That said, I do not suggest using edge.betweenness.community on weighted graphs at this time, for the reasons I described here.

How can I calculate degree, eigenvector, bonacich power centrality using R igraph package?

I am trying to calculate degree centrality, eigenvetor centrality and bonacich power centrality using igraph package in R. My data is South Korea's commuting data.
My data looks like this1st column: orientation region code, 2nd column: destination region code, 3rd column: commuting times between the two regions
I have made igraph graph using function graph_from_data_frame() like the picture below.
od18 is the data I used. The same one mentioned at the first picture.
But here are my problems.
I can't make an adjacency matrix using this graph.
: Error in get.adjacency.sparse(graph, type = type, attr = attr, edges = edges, :
Sparse matrices must be either numeric or logical,and the edge attribute is not
this is the code I executed.
this is the error.
Actually, I don't care about the adjacency matrix if centrality calculations don't have problems. But I am worrying what if this means that I am not going to get correct results of centrality calculations.
I tried to calculate the degree centrality using function degree() but the results values are all same.
All of my nodes have the same degree values as 250.
Any help about Bonacich power centrality using -beta.
Can you help me with these problems?

How do you find betweennes centrality from igraph and what does the output number mean?

Hi so bascially lets say I have a network(A) and I want to find the betweeness centrality of it.
I used: centr_betw(graph, directed = FALSE, normalized = TRUE)
This returned every node with the value:
[1] 1.827102e+04 3.554450e+04 5.000000e-01 9.524383e+04
[5] 0.000000e+00 0.000000e+00 1.078184e+05 4.768125e+04
I really want to know what these numbers mean.
It also shows the between centralization of the whole network and a max value. Lets say the network(A) as a whole has a betweenness centrality of 0.04. What can you say about this network(A) when it is compared to a random network with a betweeness centrality of 0.001?
MUCH THANKS GUYS
Quite a bit of information can be found simply if you type ?centr_betw. In particular, centr_betw returns a list of three components: res, centralization, theoretical_max.
Each element of res is the betweenness centrality of a corresponding vertex i computed in this manner. Specifically, given a shortest path between some vertices j and k (not equal to i), i is considered to be more central if this shortest path includes i. Going over all possible pairs of j and k we can find this betweenness centrality of i.
Further, centralization and theoretical_max concern the Freeman centralization. centralization is C_x, which measures how central network's most central vertex is in relation to how central all the other vertices are. theoretical_max is the denominator of C_x providing the maximal possible value of the numerator across all networks with the same number of vertices.
So, if network A has Freeman centralization 0.04 and network B has 0.001, then we may say that the most central vertex of A is significantly more central than the most central vertex of B. If B is random (i.e., Erdos-Renyi), then that makes sense, because in a big enough network all vertices should play pretty similar role.

Replication of Erdos-Renyi graph

I'm really new to R and I have got an assignment for my classes. I have to create 1000 networks of Erdos-Renyi model. The thing is, I actually can create one model, check it parameters like degree distribution, plot it etc.. Also I can check its transitivity and so on. However, I have to compare the average clustering coefficient (local transitivity) of those 1000 networks to some network that we have been working on classes in Cytoscape. This is the code that I already know:
library(igraph)
g<-erdos.renyi.game(1000,2000, type=c("gnm"))
transitivity(g) #and other atrributes...
g2<-replicate(g,1000)
transitivity(g2[[1]])
#now I have sort of list with every graph but when I try to analyze
#I got the communicate that it is not a graph object
I have to calculate standard deviation and mean ACC from those 1000 networks, and then compare it.
I will appreciate any kind of help.
I tried a lot actually:
g<-erdos.renyi.game(1026,2222,type=c("gnm"))
g2<-1000*g
transitivity(g2[2]) # this however ends in "not a graph object"error
g2[1] #outputs the adjacency matrix but instead of 1026 vertices,
#I've got 1026000 vertices, so multiplication doesn't replicate function
#but parameters
Also, I have tried unifying the list of graphs
glist<-1000*g
acc<-union(glist, byname="auto")
transitivity(acc) #outputs the same value as first function g (only one
#erdos-renyi graph
To multiply many graphs use replication function below
g<-erdos.renyi.game(100, 20, type=c("gnm"))
g2<-replicate(1000, erdos.renyi.game(100, 20, type=c("gnm")), simplify=FALSE);
sapply(g2, transitivity)
To calculate mean of some attribute like average degree or transitivity use:
mean(sapply(g2, transitivity))

How are weights treated in the cluster_walktrap function in igraph?

I am working with character networks of plays. Nodes represent characters, edges represent speeches they address to one another. It is a directed network, and the edge weights are equal to the number of words the source character says to the target.
In iGraph, edge weight sometimes means distance, and sometimes means closeness. To get the correct results for betweenness, for instance, I need to invert the edge weights so the more words a character says to another, the 'closer' they are in the network:
edgeData <- data.frame(source, target, weight = numWords)
graph <- graph_from_data_frame(edgeData)
betweenness(graph, weights = 1/E(graph)$weight)
Now I want to study the community structure of my plays, and I don't know how to use the algorithms correctly. Should I treat edge weights as distances, and invert the weights so characters who talk more are 'closer' to one another?
cluster_walktrap(graph, weights = 1/E(graph)$weight)
Or should I treat the weights as, well, weights, and use the algorithm in its default state?
cluster_walktrap(graph)
Thanks for the help!
cluster_walktrap(graph)is OK. Community detection, weight is weight, not distance.
I am also confused when I calculate some graph indices.
but if you want to calculate shortest path (or other ), you should use
weights = 1/E(graph)$weight
you can design a demo.

Resources