# File NetStatsTest.R # Purpose: Open a file # Read contents of a NN matrix (counts) # Calculate Network statistics # Density # Bewteenness # Centralization (various) # Closeness # Degree # Date: June 29, 2006 # modified July 5th, 2006 # Author: Cuau Vital # #opens up libraries neede to run the script library(igraph) library(sna) #read all files in the given directory #change tempMyDir as needed myfiles <-list.files("c:/tempMyDir") #create table, that will display results bigMatrix <- matrix(c(0, 0), nrow=12, ncol=100) i <- 0 # loop through files for(f in myfiles[-1]) { i=i+1 #modify C:/tempMyDir for your own path c <- paste("c:/tempMyDir/", f, sep="") #read file, read it as table netMatW <- read.table( c, sep="\t" ) # change it into a matrix form netMatW <-as.matrix(netMatW) # dichotomize into 0's and 1's netMat <- event2dichot(netMatW, "absolute", 0) # create a graph to work with igraph libraries g1 <-graph.adjacency(netMat, mode="undirected") #calculate stats using sna library gden(netMat)->density centralization(netMat, degree) -> Gdegree centralization(netMat, betweenness) -> Gbetweenness centralization(netMat, mode="graph", closeness) -> Gcloseness degree(netMatW, cmode="indegree") ->deg range(deg) ->rgDegree closeness(netMat) ->close range(close) ->rgCloseness betweenness(netMat) -> bc bc <- bc/342 range(bc) ->rgBC #use Information Centrality from igraph library infocent(netMatW, gmode=graph) ->inforC centralization(netMatW, infocent) ->Ginfo var(inforC) ->varInfo geodist(netMat)->dist max(dist$gdist)->diam transitivity(g1) ->trans #write output into a table bigMatrix[1,i]<-density bigMatrix[2,i]<- trans bigMatrix[3,i]<- Gdegree bigMatrix[4,i] <- Gbetweenness bigMatrix[5,i]<- Gcloseness bigMatrix[6,i] <- Ginfo bigMatrix[7,i]<- varInfo bigMatrix[8,i]<- diam bigMatrix[9,i] <- mean(deg) bigMatrix[10,i] <- mean(bc) bigMatrix[11,i] <- mean(close) bigMatrix[12,i]<- mean(inforC) } # format table out <- file("c:/tempMyDir/NetStatsOut.xls", "w") cat("Network Measures\n", file=out) cat("\n", file =out) cat("Dn\tT\tC_D\tC_B\tC_C\tC_I\tS2_I\tDi\tµ C_D(ni)\tµ C_B(ni)\tµ C_C(ni)\tµ C_I(ni)\n", file=out) #write table into file NetStatsOut.xls write(bigMatrix, out, ncolumns=12, sep="\t") close(out)