Jim421616

group_centrality

May 31st, 2021
937
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Set up the network objects
  2. friendship.aj <- matrix(c(NA, 0, 1, 1, 1,
  3.                           0, NA, 0, 0, 0,
  4.                           1, 0, NA, 0, 1,
  5.                           1, 1, 0, NA, 1,
  6.                           1, 0, 0, 0, NA),
  7.                         nrow = 5, byrow = TRUE)
  8. friendship.net.dir <- network(friendship.aj)
  9. friendship.net.undir <- network(friendship.aj, directed = FALSE)
  10.  
  11. # Here's the function
  12. group_centrality <- function(net) {
  13.     # Return a single centrality score for a whole network
  14.     if(!is.network(net))
  15.     {
  16.         stop("Object must be a network")
  17.     }
  18.     d_star <- (max(degree(net, cmode = 'outdegree')))
  19.     print(d_star)
  20.     for(i in 1:network.size(net))
  21.     {
  22.         if(is.directed(net))
  23.         {
  24.             print("Network is directed")
  25.             numerator <- d_star - degree(net, cmode = 'outdegree')[i]
  26.             print(numerator)
  27.             denominator <- (((network.size(net) - 1)^2))
  28.             print(denominator)
  29.             centrality <- sum((d_star - degree(net, cmode = 'outdegree')[i])/
  30.                 (((network.size(net) - 1)^2)))
  31.         } else {
  32.             print("Network is undirected")
  33.             numerator <- d_star - degree(net, cmode = 'outdegree')[i]
  34.             print(numerator)
  35.             denominator <- (((network.size(net)) - 1)*(network.size(net) - 2))
  36.             print(denominator)
  37.             centrality <- sum((d_star - degree(net, cmode = 'outdegree')[i])/
  38.                 (((network.size(net)) - 1)*(network.size(net) - 2)))
  39.         }
  40.        
  41.     }
  42.     return(centrality)
  43. }
RAW Paste Data