Download R source file
rm(list = ls())
gc()
library(dplyr);
library(readr);
library(magrittr)
library(tidyr)
setwd("C:/SNH18wd")
AHS_Base <- read_csv ('ahs_wpvar.csv',
col_names = TRUE);
AHS_adjlist <- AHS_Base %>%
select(ego_nid, mfnid_1:mfnid_5, ffnid_1:ffnid_5, grade, sex, commcnt) %>%
filter(commcnt==1);
AHS_Edges <- AHS_adjlist %>%
rename( id = `ego_nid`,
gender = `sex`) %>%
gather(Alter_Label, Target, mfnid_1:mfnid_5, ffnid_1:ffnid_5, na.rm = TRUE)
AHS_Edges=AHS_Edges %>% filter (Target != 99999);
AHS_Edges=AHS_Edges %>%select(id, Target);
library(igraph)
gn=data.frame(NodeID=as.numeric(AHS_adjlist$ego_nid-1))
ge=data.frame(AHS_Edges-1)
gn$group <- AHS_adjlist$grade
glimpse(gn)
net<- graph_from_data_frame(ge, directed=TRUE, vertices=gn)
V(net)[[1:5]]
plot(net)
scc <- clusters(net, "strong")
plot(net, edge.arrow.size=.2,vertex.label=NA,
layout=layout_with_fr,
vertex.color=scc$membership,
vertex.size=degree(net,mode = "in")+6)
wcc <- clusters(net, "weak")
plot(net, edge.arrow.size=.2,vertex.label=NA,
layout=layout_with_fr,
vertex.color=wcc$membership,
vertex.size=degree(net,mode = "in")+5)
symnet=as.undirected(net)
GNC <- cluster_edge_betweenness(symnet, weights = NULL)
plot_dendrogram(GNC)
modularity(GNC)
V(net)$ebtwn_cluster <-membership(GNC)
V(net)[[1:5]]
plot(net, edge.arrow.size=.2,vertex.label=NA,
layout=layout_with_kk,
vertex.color=membership(GNC),
vertex.size=degree(net,mode = "in")+5)
symnet2 <- symnet %>%
set_vertex_attr("ebtwn_cluster", value = membership(GNC))
V(symnet2)[[1:5]]
plot(symnet2, edge.arrow.size=.2,
layout=layout_with_kk,
vertex.label.cex=0.5,
vertex.size=10,
vertex.color=membership(GNC),
vertex.size=degree(symnet2,mode = "in")+5)
members <- membership(GNC)
GNC_ID <- data.frame(GNC_ID = as.numeric(members), NodeID = as.numeric(names(members)))
glimpse(GNC_ID)
gn <- merge(gn, GNC_ID, by= 'NodeID', all=TRUE)
rm(members,GNC_ID)
EVC <- cluster_leading_eigen(symnet, weights = NULL)
modularity(EVC)
plot(symnet,
edge.arrow.size=.2,
vertex.label.cex=0.5,
vertex.size=10,
layout=layout_with_kk,
vertex.color=membership(EVC))
members <- membership(EVC)
EVC_ID <- data.frame(EVC_ID = as.numeric(members), NodeID = as.numeric(names(members)))
gn <- merge(gn, EVC_ID, by= 'NodeID', all=TRUE)
rm(members,EVC_ID)
WTC <- cluster_walktrap(symnet)
modularity(WTC)
plot(symnet,
edge.arrow.size=.2,
vertex.label.cex=0.5,
vertex.size=10,
layout=layout_with_kk,
vertex.color=membership(WTC))
members <- membership(WTC)
WTC_ID <- data.frame(WTC_ID = as.numeric(members), NodeID = as.numeric(names(members)))
gn <- merge(gn, WTC_ID, by= 'NodeID', all=TRUE)
rm(members,WTC_ID)
LP <- cluster_label_prop(symnet)
modularity(LP)
plot(symnet,
edge.arrow.size=.2,
vertex.label.cex=0.5,
vertex.size=10,
layout=layout_with_kk,
vertex.color=membership(LP))
members <- membership(LP)
LP_ID <- data.frame(LP_ID = as.numeric(members), NodeID = as.numeric(names(members)))
gn <- merge(gn, LP_ID, by= 'NodeID', all=TRUE)
rm(members,WTC_ID)
IMP <- cluster_infomap(symnet)
modularity(IMP)
plot(symnet,
edge.arrow.size=.2,
vertex.label.cex=0.5,
vertex.size=10,
layout=layout_with_kk,
vertex.color=membership(IMP))
members <- membership(IMP)
IMP_ID <- data.frame(IMP_ID = as.numeric(members), NodeID = as.numeric(names(members)))
gn <- merge(gn, IMP_ID, by= 'NodeID', all=TRUE)
rm(members,IMP_ID)
LC <- cluster_louvain(symnet, weights = NULL)
modularity(LC)
plot(symnet,
edge.arrow.size=.2,
vertex.label.cex=0.5,
vertex.size=10,
layout=layout_with_kk,
vertex.color=membership(LC))
library(clues)
help(cluster)
adjustedRand(gn$WTC_ID, gn$EVC_ID)
adjustedRand(gn$LP_ID, gn$GNC_ID)