LiteratureReview.R

Peter Prevos — Mar 5, 2014, 9:06 PM

#PRELIMINARIES
setwd("~/Documents/PhD/Dissertation/Utilities") #Set working directory
par(mar=c(0,0,2,0)) #Plot margins
options(width=100) #Modify screen width
library(RQDA) #Load RQDA
Loading required package: DBI
Loading required package: RSQLite
Loading required package: gWidgets
Loading required package: gWidgetsRGtk2
Loading required package: RGtk2
Loading required package: igraph
RQDA() #Open RQDA
openProject("LiteratureReview.rqda", updateGUI=TRUE) #Open project

#VERTICES
vid <-1:as.numeric(RQDAQuery("SELECT COUNT(*) FROM freecode WHERE status==1")) #Number of used codes
cid <- RQDAQuery("SELECT id FROM freecode WHERE status==1") #Code id as in RQDA
names <- RQDAQuery("SELECT name from freecode WHERE status==1") #COde names
vertices <- data.frame(cid, vid, names) #Create data frame
#Remove coding spaces to avoid confusion
for (i in 1:nrow(vertices)) {
  vertices[i,3] <- gsub(" ", "", vertices[i,3])
}

#EDGES
edges <- data.frame(v1=numeric(0), v2=numeric(0), type=numeric(0))
edge <-1
abstractList <- RQDAQuery("SELECT id FROM source WHERE status==1;") #Number of abstracts
for (a in 1: nrow(abstractList)) { #Cycle through all abstracts
  abstract <- abstractList[a,] #Determine abstract number
  codings <- RQDAQuery(paste("SELECT cid from coding WHERE fid==",abstract,"AND status==1")) #Codes in abstract
  n <- nrow(codings) #Number of codes in abstract
  if (n>1) { #Only create edge(s) when more than one code
    for (i in 1:(n-1)) { #Create unique code combinations
      for (j in (i+1):n) {
        edges <- rbind(edges, data.frame(codings[i,],codings[j,],1)) #Add edge
        edge <- edge+1 #Number of edges in list
      }
    }
  }
}
#Replace RQDA numbers with vertex numbers
for (i in 1:2) {
  for (j in 1:nrow(edges)) {
    edges[j,i] <- vertices$vid[vertices$id==edges[j,i]] 
  }
}

#NETWORK STATISTICS
print(paste("Abstracts:", nrow(abstractList)))
[1] "Abstracts: 117"
print(paste("Vertices (codes):", max(vid))) #Total number of vertices
[1] "Vertices (codes): 39"
print(paste("Edges:", nrow(edges)))
[1] "Edges: 378"

#CREATE PAJEK FILE
pajek <- file("LitReview.net") #Open file
write(c(
  paste("*Vertices ", nrow(vertices)), 
  paste(vertices$vid,vertices$name),
  paste("*Arcs", 0),
  paste("*Edges", nrow(edges)),
  paste(edges[,1], edges[,2], 1)), 
  pajek)
close(pajek)

#NETWORK VISUALISATION
library(igraph)
g <- read.graph("LitReview.net", format="pajek")
#Walktrap Communities
wc <- walktrap.community(g) #Community detection algorithm
plot(wc, g, 
     layout=layout.fruchterman.reingold, 
     vertex.size=10,
     vertex.label=vertices$name,
     vertex.label.cex=.5,
     vertex.label.color="black",
     main = "Walktrap Communities"
)

plot of chunk unnamed-chunk-1

print(paste("Walktrap communities: ", max(wc$membership)))
[1] "Walktrap communities:  6"
for (i in 1:max(wc$membership)) {
  if (sum(wc$membership==i)>1) {
    subgraph <- induced.subgraph(g, vertices$vid[wc$membership==i])
    in.degrees <- degree(subgraph)
    out.degrees <- degree(g, i) - in.degrees
    plot(subgraph, vertex.label=vertices$name[wc$membership==i], 
         main=bquote(paste(
           "Walktrap Community ",.(i),
           " (p=",.(wilcox.test(in.degrees, out.degrees, exact=F)$p.value),")"))
    )
  }
}

plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1


#Spinglass Communities
sc <- spinglass.community(g) #Community detection algorithm
plot(sc, g, 
     layout=layout.fruchterman.reingold, 
     vertex.size=10,
     vertex.label=vertices$name,
     vertex.label.cex=.7,
     vertex.label.color="black",
     main = "Spinglass Communities"
)

plot of chunk unnamed-chunk-1

print(paste("Spinglass communities: ", max(sc$membership)))
[1] "Spinglass communities:  4"
for (i in 1:max(sc$membership)) {
  if (sum(sc$membership==i)>1) {
    subgraph <- induced.subgraph(g, vertices$vid[sc$membership==i])
    in.degrees <- degree(subgraph)
    out.degrees <- degree(g, i) - in.degrees
    plot(subgraph, vertex.label=vertices$name[sc$membership==i], 
         main=bquote(paste(
           "Spinglass Community ",.(i),
           " (p=",.(wilcox.test(in.degrees, out.degrees, exact=F)$p.value),")"))
         )
  }
}

plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1 plot of chunk unnamed-chunk-1