Network randomizations
In the previous exercise you may have noticed that the average path length of the Forrest Gump network was smaller than the average path length of the random network. If you ran the code a few times you will have noticed that it is nearly always lower in the Forrest Gump network than the random network. What this suggests is that the Forrest Gump network is more highly interconnected than each random network even though the random networks have the same number of vertices and approximately identical graph densities. Rather than re-running this code many times, you can more formally address this by creating 1000 random graphs based on the number of vertices and density of the original Forrest Gump graph. Then, you can see how many times the average path length of the random graphs is less than the original Forrest Gump network. This is called a randomization test.
The graph g
, and its average path length (that you calculated in the previous exercise), g.apl
are in your workspace.
Cet exercice fait partie du cours
Network Analysis in R
Instructions
- Generate 1000 random graphs of the original graph
g
by executing the code that creates the list objectgl
and the for loop. - Calculate the average path length of the 1000 random graphs using
lapply()
. Create a vectorgl.apls
of these 1000 values by executing the code that usesunlist()
. - Plot a histogram of the average path lengths of the 1000 random graphs using
hist()
on the vectorgl.apls
. Add a red dashed vertical line to the plot usingabline()
with the x-intercept being the value of the average path length of the original graph,g.apl
. - Calculate the proportion of times that the values of the average path length of random graphs
gl.apls
are lower than the value of the original graphg.apl
. This is essentially the probability that we would expect our observed average path length by chance given the original density and number of vertices of the original graph.
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de code.
library(igraph)
# Generate 1000 random graphs
gl <- vector('list', 1000)
for(i in 1:1000){
gl[[i]] <- erdos.renyi.game(n = gorder(g), p.or.m = gd, type = "gnp")
}
# Calculate average path length of 1000 random graphs
gl.apls <- unlist(lapply(gl, ___, directed = FALSE))
# Plot the distribution of average path lengths
___(gl.apls, xlim = range(c(1.5, 6)))
abline(v = ___, col = "red", lty = 3, lwd = 2)
# Calculate the proportion of graphs with an average path length lower than our observed
mean(___ < ___)