R: Test randomizacji sieci Igprah Stakoverflow

# NETWORK RANDOMIZATION TEST
# TEST IF FEATURES ARE UNUSUAL

# Create Network
g <- graph_from_data_frame(edges, directed=FALSE)
gd = edge_density(g)

# Network Average path length
g.apl <-mean_distance(g, directed=FALSE)

# Create one random graph with the same number of nodes and edges as g
g.random <- erdos.renyi.game(n = gorder(g), p.or.m = gd, type = "gnp")

# 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, mean_distance, directed = FALSE))

# Plot the distribution of average path lengths
hist(gl.apls, xlim = range(c(1.5, 10)))
abline(v = g.apl, col = "red", lty = 3, lwd = 2)

# Calculate the proportion of graphs with an average path length lower than our observed
mean(gl.apls < g.apl)
Andrea Perlato