library(igraph)
library(foreach)
<- rep(1,6)
from <- c(2:7)
to <- data.frame(form=from, to=to)
edgelist <- graph_from_data_frame(edgelist, directed = F)
star plot(star)
Confidence clustering
Archive URL: http://web.archive.org/web/20200301160639/http://oskarkosch.com/blog/confidence-clustering/
Clustering is usually done with the objective of modularity maximization (e.g., the so-called Louvain algorithm), sometimes with additional constraints (like the Leiden algorithm). But, what is somewhat bizarre, these algorithms are very broad in terms of what they aim to partition, and therefore, are superficial in their interpretation – if one tool is used to fit all needs… then it rather should be used on exploratory stage of research, when the nature of the network and possible insights are yet to be determined.
Here I propose an early version of clustering based on neutrality centrality and few assumptions. The implementation now is a little dirty and should get better with the next versions. Also, please note, that neutrality centrality here is calculated as standard deviation of population, not sample (that explains differences in values when compared to the previous post).
The assumption is that more confident nodes are convincing less confident ones. So this algorithm tries to create clusters based on structural bias. This approach might be especially useful in social network analysis of political sympathies, or separate areas of discrimination in localization of the public utility facilities. It should be noted that in terms of modularity, it will never achieve results the modularity-based algorithms do; on the other hand, it has the potential to become lightweight.
Few things are yet to be implemented:
the exclusion of extremists from the confidence network of mainstream nodes (probably this occurs), the
extremists_populationvariable
the influence of the neighbors as a whole, rather than individuals
Also, this kind of clustering, by implementing assumptions, needs to be yet validated – and by this fact it is different from the other, more generic algorithms. While working on making this algorithm better, I will try to provide layout creating algorithm based on structural bias and rotation of nodes.
Confidence partitioning algorithm
<- function(t_prob) {
prob_bool sample(c(T,F),1,prob=c(t_prob,1-t_prob))
}
<- function(graph) {
clean.g.attributes for(e in setdiff(vertex_attr_names(graph),'name')) {
<- delete_vertex_attr(graph, e)
graph
}for(e in edge_attr_names(graph)) {
<- delete_edge_attr(graph, e)
graph
}for(e in graph_attr_names(graph)) {
<- delete_graph_attr(graph, e)
graph
}return(graph)
}
<- function(graph) {
vertices_ids if(is.named(graph)) {
<- get.vertex.attribute(graph,'name')
names else {
} <- 1:vcount(graph)
names
}return(names)
}
<- function(graph, moment=2) {
centr_neutr <- clean.g.attributes(graph)
graph <- vertices_ids(graph)
names <- length(names) - 1
global.length <- foreach(e = names, .combine = 'c') %do% {
centralities <- setdiff(names, e)
to <- distances(graph, e, to)
dists <- mean(dists)
mn <- (sum((dists-mn)^moment)/(global.length))^(1/moment)
res # res <- sd(dists)
return(res)
}names(centralities) <- names
return(centralities)
}
centr_neutr(star)
1 | 2 | 3 | 4 | 5 | 6 | 7 |
0.000000 | 0.372678 | 0.372678 | 0.372678 | 0.372678 | 0.372678 | 0.372678 |
<- function(graph, extremists_population=0, merge_same_level=.0, lonely_join_neighbours=.0, cent=NULL) {
cluster_confidence <- vertices_ids(graph)
names if(is.null(cent)) {
<- centr_neutr(graph)
cent
}<- cent %>% sort(T) %>% {.[0:extremists_population]} %>% names()
extremists <- vector('character', length(cent))
memb names(memb) <- names
<- to <- c()
from <- graph.empty(n=0, directed=F)
comps <- add_vertices(comps, length(names), name=names)
comps for(i in 1:length(names)) {
<- names[i]
nm <- as.character(neighbors(graph, nm))
nei <- cent[nei]
nei_cent if(any(cent[nm]<nei_cent)) {
<- add_edges(comps, c(nm, nei[which.max(nei_cent)]))
comps else if(merge_same_level>0&any(cent[nm]==nei_cent)&prob_bool(merge_same_level)) {
} <- add_edges(comps, c(nm, nei[which.max(nei_cent)]))
comps else if(lonely_join_neighbours>0&prob_bool(lonely_join_neighbours)) {
} <- add_edges(comps, c(nm, nei[which.max(nei_cent)]))
comps
}
}<- components(comps)
cmps return(cmps$membership)
}
<- sample_degseq(sample(c(2,3,3,4),30,T)) %>% simplify() sample_deg
par(mar=rep(0,4))
plot(sample_deg, vertex.color=cluster_confidence(sample_deg,0,1,1))