BenjaminLind

transitivity_directed.R

Aug 10th, 2014
511
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 2.25 KB | None | 0 0
  1. library(igraph)
  2. library(compiler)
  3.  
  4. transitivity_directed <- function(g, method = "weak"){
  5.     # _g_ is an igraph graph object
  6.     # _method_ is the technique of measuring transitivity.
  7.         # "Weak" is the most common understanding of transitivity, so that's the one used here.
  8.         # "Weak census" returns the counts, not the proportion.
  9.         # Once I find a convenient table showing the weights for vacuous transitive triples and see the formula, I'll include strong transitivity.
  10.  
  11.     if (is.directed(g) == FALSE){
  12.         warning("Entered an undirected graph into transitivity_directed(). Returned transitivity(g).")
  13.         return(transitivity(g))
  14.         }    
  15.     acceptable_methods <- c("weak", "weakcensus")
  16.     if (method %in% acceptable_methods == FALSE)
  17.         stop(paste("_method_ must be in c(\"", paste(acceptable_methods, collapse = "\", \""), "\")", sep = ""))
  18.     # See Wasserman and Faust (1994:573)
  19.     weight_mat <- data.frame(row.names = c("003", "012", "102", "021D", "021U", "021C", "111D", "111U", "030T", "030C", "201", "120D", "120U", "120C", "210", "300"))
  20.     weight_mat$b_in <- c(0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 2, 1, 1, 2, 3)
  21.     weight_mat$b_out <- c(0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 2, 1, 2, 3)
  22.     weight_mat$m <- c(0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 2, 1, 1, 1, 2, 3)
  23.     weight_mat$a <- c(0, 1, 0, 2, 2, 2, 1, 1, 3, 3, 0, 2, 2, 2, 1, 0)
  24.     weight_mat$n <- c(3, 2, 2, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0)
  25.     weight_mat$c <- c(0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 6)
  26.     weight_mat$Trans <- c(0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 2, 1, 3, 6)
  27.     #weight_mat$Vac_trans <- c(6, 6, 6, 6, 6, 5, 5, 5, 5, 3, 4, 4, 4, 3, 2, 0)
  28.     weight_mat$Intrans <- c(0, 0, 0, 0, 0, 1, 1, 1, 0, 3, 2, 0, 0, 2, 1, 0)
  29.     weight_mat$Close_Friends_Disagreeing <- c(0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 1, 1, 0)
  30.  
  31.     tc <- triad.census(g)
  32.     if (method == "weakcensus"){
  33.         transcounts <- sum(weight_mat$Trans * tc)
  34.         return(transcounts)
  35.         }
  36.     else if (method == "weak"){
  37.         transcounts <- sum(weight_mat$Trans * tc)
  38.         intranscounts <- sum(weight_mat$Intrans * tc)
  39.         return(transcounts / (transcounts + intranscounts))
  40.         }
  41.     }
  42. transitivity_directed <- cmpfun(transitivity_directed)
Advertisement
Add Comment
Please, Sign In to add comment