Skip to content

ShwartzAdam/EX3_ML_CA

Repository files navigation

title author date output
EX3
AdamAndDaniel
April 28, 2016
html_document

#QUESTION 1

library('igraph')
library('sna')
ga.data <- read.csv('ga_edgelist.csv', header = T)
gg <- graph.data.frame(ga.data,directed = F)

Create new graph from data and vrtx

ga.data <- read.csv('ga_edgelist.csv', header=TRUE, stringsAsFactors=FALSE)
ga.vrtx <- read.csv('ga_actors.csv', header=TRUE, stringsAsFactors=FALSE)
gg <- graph.data.frame(ga.data, vertices=ga.vrtx, directed=FALSE)

betweenness

betweenness(gg)
which.max(betweenness(gg))

closeness

closeness(gg)
which.max(closeness(gg))

eigenvector

eig <- eigen_centrality(gg)
eig$vector
which.max(eig$vector)

modularity and plot seprated cumminuty by colors with betweenness

fc <-  edge.betweenness.community(gg)
fc$modularity
which.max(fc$modularity)
memb <- membership(fc)
max(memb)

plot(gg, vertex.size=10, vertex.label=NA,vertex.color=memb, asp=FALSE)

modularity and plot seprated cumminuty by colors with eigenvector

dc <- leading.eigenvector.community(gg)
dc$modularity 
which.max(dc$modularity)
membTwo <- membership(dc)
max(membTwo)

plot(gg, vertex.size=10, vertex.label=NA,vertex.color=membTwo, asp=FALSE)

#QUESTION 2 USING TWITTER DATA

load('termDocMatrix.rdata')
termDocMatrix[1:20,1:20]

change it to a Boolean matrix

termDocMatrix[termDocMatrix>=1] <- 1

transform into a term-term adjacency matrix

termMatrix <- termDocMatrix %*% t(termDocMatrix)

inspect terms numbered 5 to 10

termMatrix[5:10,5:10]

build a graph from the above matrix

g <- graph.adjacency(termMatrix, weighted=T, mode = "undirected")

remove loops

g <- simplify(g)

set labels and degrees of vertices

V(g)$label <- V(g)$name
V(g)$degree <- degree(g)

Set the label size of vertices based on their degrees , label.cex - The font size for vertex labels.

V(g)$label.cex <- 2.2 * V(g)$degree / max(V(g)$degree) + .2

rgb(red, green, blue, alpha) - defines a color, with an alpha transparency.

V(g)$label.color <- rgb(0, 0, .2, .8)
V(g)$frame.color <- NA

Set the width and transparency of edges based on their weights.

E(g)$weight
egam <- (log(E(g)$weight)+.4) / max(log(E(g)$weight)+.4)
E(g)$color <- rgb(.5, .5, 0, egam)
E(g)$width <- egam

plot the graph in layout1

plot(g, layout=layout1)

set seed to make the layout reproducible

set.seed(3952)
layout1 <- layout.fruchterman.reingold(g)
plot(g, layout=layout1)

Betweeness

betweenness(g)
which.max(betweenness(g))

Closeness

closeness(g)
which.max(closeness(g))

Eigenvector

eig <- eigen_centrality(g)
eig$vector

which.max(eig$vector)

modularity and plot seprated cumminuty by colors by betweenness

edge.betweenness.community(g)

fc <-  edge.betweenness.community(g)
fc$modularity
which.max(fc$modularity)
memb <- membership(fc)
max(memb)

plot(g, vertex.size=10, vertex.label=NA,vertex.color=memb, asp=FALSE)

modularity and plot seprated cumminuty by colors with eigenvector

leading.eigenvector.community(g)

dc <- leading.eigenvector.community(g)
dc$modularity 
which.max(dc$modularity)
membTwo <- membership(dc)
max(membTwo)

plot(g, vertex.size=10, vertex.label=NA,vertex.color=membTwo, asp=FALSE)

Releases

No releases published

Packages

No packages published