setwd("...") #insert your work directory library ("igraph") #for network analysis library(ade4) #for jaccard or other normalizations # Load matrix from csv M = as.matrix(read.csv(file.choose(), sep = ";", header = T, row.names = 1, check.names=FALSE)) #Transform into an unweighted, directed, twomode igraph object (Jaccard works best with unweighted/ binary matrices) two_mode_network <- graph.incidence(M, directed = TRUE, mode = "out", weighted = NULL) # Delete isolates from the twomode network (optional) Isolated <- which(degree(two_mode_network)==0) two_mode_network_ni <- delete.vertices(two_mode_network, Isolated) class(two_mode_network_ni) # Transform twomode back to an incidence matrix M2 <- as_incidence_matrix(two_mode_network_ni, names = TRUE) # Make a heatmap of the incidence matrix (optional) heatmap (M2, Colv = NA, scale = "row", cexCol = 1, xlab = "Concept") #calculate jaccard distances for actors and concepts actor_jaccard <- dist.binary(M2, method=1, upper=FALSE, diag = FALSE) # Method #1 is "Jaccard Index" concept_jaccard <- dist.binary(t(M2), method=1, upper=FALSE, diag = FALSE) # create one-mode matrixes based on jaccard normalization, set diagonals zero ## Note that the Jaccard coefficient is calculated as type type d = sqrt(1 - s), so no similarity is reflected by 1, whereas 0 reflects a perfect match/ no distance at all om_actor <- as.matrix(actor_jaccard) diag(om_actor)<-0 om_concept <- as.matrix(concept_jaccard) diag(om_concept)<-0 #The Jaccard coefficients can be retained by applying the function (((x)^2)-1)*(-1)) to any value x M3 <- apply(om_actor,1:2, function(x) (((x)^2)-1)*(-1)) M4 <- apply(om_concept,1:2, function(x) (((x)^2)-1)*(-1)) #Set diagonals of Jaccard normalized one-mode networks zero diag(M3)<-0 diag(M4)<-0 #Print to csv write.table (M3, file ="om_actor_jaccard.csv", dec = ".", sep = ";", col.names = NA) write.table (M4, file = "om_concept_jaccard.csv", dec = ".", sep = ";", col.names = NA) write.table (M2, file = "tm_unweighted.csv", dec = ".", sep = ";", col.names = NA)