I have a network/graph (test_FINAL
), from which I want to remove a certain set of vertices below:
vertex <- c(1, 3, 7, 9, 10)
The idea is to run a clustering analyses
after the vertices are removed. For example, in the first iteration, vertex #1 should be removed and then, vertices #1 and #3, and the subsequent #1, #3, #7 and so on until the final iteration where all the vertices are removed and clustering performed.
My code looks as follows:
# Library
library(igraph)
# Create data
set.seed(1)
data <- matrix(sample(0:1, 100, replace=TRUE, prob=c(0.8,0.2)), nc=10)
network <- graph_from_adjacency_matrix(data , mode='undirected', diag=F )
# Default network
par(mar=c(0,0,0,0))
plot(network)
vertex <- c(1, 3, 7, 9, 10)
# Estimating cluster statistics *prior to* removing vertices
pre_cluster <- cluster_fast_greedy(network)
length(pre_cluster); sizes(pre_cluster); modularity(pre_cluster)
# removing vertices
final_graph <- delete_vertices(network, c(vertex))
cluster_graph <- cluster_fast_greedy(final_graph)
# Estimating cluster statistics *after* removing vertices
length(cluster_graph); sizes(cluster_graph); modularity(cluster_graph)
What is the best way to do this in a loop
, especially for the final iteration where all the vertices
are removed and then the cluster statistics are estimated?
CodePudding user response:
How about the following
vertex <- c(1, 3, 7, 9, 10)
cumulator <- vertex[1]
while (cumulator <= length(vertex)) {
to_be_deleted <- vertex[c(1:cumulator)]
cumulator = cumulator 1
print(to_be_deleted)
}
And in each iteration of the while
loop, you could then run your deletion function.
CodePudding user response:
Probably you can start from using Reduce
to create a cumulative list of vertices
> Reduce(c, vertex, accumulate = TRUE)
[[1]]
[1] 1
[[2]]
[1] 1 3
[[3]]
[1] 1 3 7
[[4]]
[1] 1 3 7 9
[[5]]
[1] 1 3 7 9 10
and then try
lapply(
Reduce(c, vertex, accumulate = TRUE),
function(x) {
cluster_fast_greedy(
delete_vertices(
network,
x
)
)
}
)
which gives
[[1]]
IGRAPH clustering fast greedy, groups: 3, mod: 0.32
groups:
$`1`
[1] 2 6 8 9
$`2`
[1] 1 5 7
$`3`
[1] 3 4
[[2]]
IGRAPH clustering fast greedy, groups: 3, mod: 0.29
groups:
$`1`
[1] 5 7 8
$`2`
[1] 1 4 6
$`3`
[1] 2 3
[[3]]
IGRAPH clustering fast greedy, groups: 2, mod: 0.36
groups:
$`1`
[1] 2 3 6 7
$`2`
[1] 1 4 5
[[4]]
IGRAPH clustering fast greedy, groups: 2, mod: 0.32
groups:
$`1`
[1] 2 3 6
$`2`
[1] 1 4 5
[[5]]
IGRAPH clustering fast greedy, groups: 2, mod: 0.38
groups:
$`1`
[1] 1 4 5
$`2`
[1] 2 3