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

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?

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.

Why is cutoff parameter in igraph in r not applicable?

I want to calculate closeness centrality on a disconnected graph. closeness function in igraph in r is not well-defined for disconnected graphs since the closeness of a vertex is defined as the inverse of the average distance to all other vertices, and so if there are two vertices that have no path between them, closeness measures of these vertices can not be computed properly. To somehow cure this issue, estimate_closeness function in igraph is suggested, it works for disconnected graphs by specifying cutoffparameter as being greater than zero.
However, when I try to run V(network)$closeness <- estimate_closeness(network, mode = "all", cutoff > 0), it gives the following error:
Error in as.igraph.vs(graph, vids) : object 'cutoff' not found
How can I solve this issue?

Why betweenness centrality differs in igraph?

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.

Weighted Cluster Analysis in R — generating more clusters than requested with hclust

I'm trying to conduct a hierarchical agglomerative cluster analysis in R by using the Weighted Cluster package. Before doing so, I calculated the distances between state sequences by leveraging the TraMineR package (see pp. 4-6 here).
Following the vignette hyperlinked above, I fed my distance matrix into hclust while adding a vector of weights as follows (datadist is the distance matrix; dataframe is my data frame featuring time series data; and weight is an all-waves longitudinal survey weight):
Cluster <- hclust(as.dist(datadist), method = "ward", members = dataframe$weight)
Then, after arriving at a specific cluster solution (four subgroups), I used the cutree function to determine the relative frequency of each cluster and assign cases:
subgroups <- cutree(Cluster, k = 4)
However, I somehow generated more than four groups after executing the code above (over 30, in fact). When I removed the vector of weights, I was able to produce frequencies for four clusters, but unweighted results are sub-optimal.
If anyone out there can help me understand what's going on (and how I can address or treat the problem), it would be greatly appreciated.

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))

Resources