-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_polblogs.R
More file actions
executable file
·69 lines (57 loc) · 2.55 KB
/
Copy pathexample_polblogs.R
File metadata and controls
executable file
·69 lines (57 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#################################################
## Spectral Clustering with Sample Splitting:
## An example on the political blog data.
##
## Copyright Lingxue Zhu (lzhu@cmu.edu)
## All Rights Reserved.
##
## Reference:
## 1. Lei and Zhu (2017),
## "Generic Sample Splitting for Refined Community Recovery in Degree Corrected Stochastic Block Models",
## Statistica Sinica
## 2. Adamic and Glance (2005),
## "The political blogosphere and the 2004 US election: Divided they blog.",
## Proceedings of the 3rd International Workshop on Link Discovery, ACM, 36–43.
##
###################################################
rm(list=ls())
source("splitSpectral.R")
###################
## load data
###################
## political blog data
PolblogsEdge <- read.csv("data/clean_PolblogsEdges.csv", stringsAsFactors = FALSE)
PolblogsNode <- read.csv("data/clean_PolblogsNodeLabels.csv", stringsAsFactors = FALSE)
## construct the adjacency matrix
n.node <- nrow(PolblogsNode)
BlogAdj <- matrix(0, nrow=n.node, ncol=n.node)
BlogAdj[cbind(PolblogsEdge[,"node1"], PolblogsEdge[,"node2"])] <- 1
BlogAdj[cbind(PolblogsEdge[,"node2"], PolblogsEdge[,"node1"])] <- 1
## 1222 Nodes: 586 liberal and 636 conservative
table(PolblogsNode[, "truelabel"])
## 1 2
## 586 636
## 16714 Edges
sum(BlogAdj[upper.tri(BlogAdj)])
## 16714
######################
## Community detection
######################
## spectral clustering
spectral.clust.est = SpectralClust(Adj=BlogAdj, K=2, isSphere=TRUE)
spectral.accur = Accuracy(spectral.clust.est, PolblogsNode[, "truelabel"], K=2)
print(paste("Sphere spectral clustering: accuracy =", spectral.accur))
## 2-fold cross clustering
cross.2v.clust.est = CrossClust.vFold(Adj=BlogAdj, fold=2, K=2, isSphere=TRUE)
cross.2v.accur = Accuracy(cross.2v.clust.est, PolblogsNode[, "truelabel"], K=2)
print(paste("2-fold sphere cross clustering: accuracy =", cross.2v.accur))
## 10-fold cross clustering
cross.10v.clust.est = CrossClust.vFold(Adj=BlogAdj, fold=10, K=2, isSphere=TRUE)
cross.10v.accur = Accuracy(cross.10v.clust.est, PolblogsNode[, "truelabel"], K=2)
print(paste("10-fold sphere cross clustering: accuracy =", cross.10v.accur))
## self-cross clustering based on spectral clustering on the full graph
cross.self.clust.est = CrossClust(Adj=BlogAdj,
i.G1=c(1:n.node), G1.clust.est=spectral.clust.est,
i.G2=c(1:n.node), K=2, isSphere=TRUE)
cross.self.accur = Accuracy(cross.self.clust.est, PolblogsNode[, "truelabel"], K=2)
print(paste("Sphere self-cross clustering: accuracy =", cross.self.accur))