Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate degree, closeness and betweenness in R

I have a data table which consists of names of users who post in the same thread in a forum, it looks like that: X1 X2 1. g79 kian 2. g79 greyracer 3. g79 oldskoo1 ...

I need to calculate degree, closeness and betweenness. I'm using the following code:

library(igraph)
setwd("/Volumes/NATASHKA/api/R files")
load("edgelist_one_mode.rda")
load("map.rda")
load ("result.rda")
el <- as.matrix(whatwewant)
el[,1] <- as.character(el[,1])
el[,2] <- as.character(el[,2])
g <- graph.data.frame(el, directed=FALSE)
plot(g, edge.arrow.size=.5)
indegreeG <- degree(g, mode="in")
outdegreeG <- degree(g, mode="out")
totaldegreeG <- degree(g)
inclosenessG <- closeness(g, mode='in')
outclosenessG <- closeness(g, mode='out')
totalclosenessG <- closeness(g)
betweennessG <- betweenness(g)
forumG <- data.frame(V(g)$name, indegreeG, outdegreeG, totaldegreeG, inclosenessG,    outclosenessG, totalclosenessG, betweennessG)
write.table(forumG,file="forumG.csv",sep=";")

The question is why do I get the same values for in-degree, out-degree and total-degree, the same for closeness? Besides, at the beginning I have 41213 users, but after analysis (when I calculate degree, etc..) I only have 37874. How could I lose so many observations? Please tell me if I have a mistake in the code.

Thanks

like image 595
Tash Avatar asked Mar 24 '23 16:03

Tash


1 Answers

The reason you get the same value for in-degree, out-degree and total degree is because you are creating an undirected network with the graph.data.frame(el, directed=FALSE). In an undirected network, the number of links from a node and to a node are the same and they are both equal to the global degree.

If you want a directed network, you will need to do graph.data.frame(el, directed=TRUE). It will create a directed network in which the id in the first column of your dataframe is the id of the node sending the tie and the id in the second column indicates the node receiving that tie.

As for loosing nodes, my guess would be that you have some individuals who never interact with anyone and therefore are lost when you transform your two-mode network into one-mode (I assume you do this but don't show us how you do it because of your line:load("edgelist_one_mode.rda"))

Short of a reproducible example, I think that is all I can deduce from your code.

like image 169
Antoine Vernet Avatar answered Apr 06 '23 01:04

Antoine Vernet