The function run_infomap_monolayer
can use shuffling
algorithms built into vegan
. To use this, we need to set
signif=T
and provide a shuffling method to
shuff_method
. The shuffling methods are the ones detailed
in ?vegan::commsim
.
network_object <- create_monolayer_network(memmott1999, bipartite = T, directed = F, group_names = c('A','P'))
## [1] "Input: a bipartite matrix"
# Run with shuffling
infomap_object <- run_infomap_monolayer(network_object, infomap_executable='Infomap',
flow_model = 'undirected',
silent=T, trials=20, two_level=T, seed=123,
signif = T, shuff_method = 'r00', nsim = 10) #nsim = 50
## [1] "Creating a link list..."
## running: ./Infomap infomap.txt . --tree --seed 123 -N 20 -f undirected --silent --two-level
## [1] "Shuffling..."
## [1] "Running Infomap on shuffled network 1/10"
## [1] "Running Infomap on shuffled network 2/10"
## [1] "Running Infomap on shuffled network 3/10"
## [1] "Running Infomap on shuffled network 4/10"
## [1] "Running Infomap on shuffled network 5/10"
## [1] "Running Infomap on shuffled network 6/10"
## [1] "Running Infomap on shuffled network 7/10"
## [1] "Running Infomap on shuffled network 8/10"
## [1] "Running Infomap on shuffled network 9/10"
## [1] "Running Infomap on shuffled network 10/10"
## Warning in run_infomap_monolayer(network_object, infomap_executable = "Infomap", : pvalue is not really 0, it is <0.1
## [1] "Removing auxilary files..."
# Plot histograms
plots <- plot_signif(infomap_object, plotit = T)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
plot_grid(
plots$L_plot+
theme_bw()+
theme(legend.position='none',
legend.title = element_text(size=16),
axis.text = element_text(size=16)),
plots$m_plot+
theme_bw()+
theme(legend.position='none',
legend.title = element_text(size=16),
axis.text = element_text(size=16))
)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
Another way is to provide shuff_method
with a list
containing already shuffled networks. You can for example use the
function shuffle_infomap
to produce shuffled networks with
vegan
. This is necessary on the shuffling algorithm from
?vegan::commsim
needs additional arguments such as
burning.
# Or can shuffle like this, if additional arguments are needed for the shuffling algorithm
shuffled <- shuffle_infomap(network_object, shuff_method='curveball', nsim=10, burnin=2000) #nsim=50
## [1] "Shuffling..."
infomap_object <- run_infomap_monolayer(network_object, infomap_executable='Infomap',
flow_model = 'undirected',
silent=T, trials=20, two_level=T, seed=123,
signif = T, shuff_method = shuffled, nsim = 10) #nsim = 50
## [1] "Creating a link list..."
## running: ./Infomap infomap.txt . --tree --seed 123 -N 20 -f undirected --silent --two-level
## [1] "Running Infomap on shuffled network 1/10"
## [1] "Running Infomap on shuffled network 2/10"
## [1] "Running Infomap on shuffled network 3/10"
## [1] "Running Infomap on shuffled network 4/10"
## [1] "Running Infomap on shuffled network 5/10"
## [1] "Running Infomap on shuffled network 6/10"
## [1] "Running Infomap on shuffled network 7/10"
## [1] "Running Infomap on shuffled network 8/10"
## [1] "Running Infomap on shuffled network 9/10"
## [1] "Running Infomap on shuffled network 10/10"
## Warning in run_infomap_monolayer(network_object, infomap_executable = "Infomap", : pvalue is not really 0, it is <0.1
## [1] "Removing auxilary files..."
plots <- plot_signif(infomap_object, plotit = F)
plot_grid(
plots$L_plot+
theme_bw()+
theme(legend.position='none',
axis.text = element_text(size=16), #20
legend.title = element_text(size=16),
axis.title = element_text(size=16)), #20
plots$m_plot+
theme_bw()+
theme(legend.position='none',
axis.text = element_text(size=16), #20
legend.title = element_text(size=16),
axis.title = element_text(size=16)) #20
)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
You can also create your own shuffling algorithm and a list of shuffled link lists. As in this example with the Tur et al. 2016 data set..
# Import data
data(tur2016)
tur2016_altitude2000 <- tur2016 %>%
filter(altitude==2000) %>%
select("donor", "receptor", "total") %>%
group_by(donor, receptor) %>%
summarise(n=mean(total)) %>%
rename(from = donor, to = receptor, weight = n) %>%
ungroup() %>%
slice(c(-10,-13,-28)) %>% # Remove singletons
filter(from!=to) # Remove self loops
## `summarise()` has grouped output by 'donor'. You can override using the `.groups` argument.
tur_network <- create_monolayer_network(tur2016_altitude2000, directed = T, bipartite = F)
## Warning: One or more rows sum to 0. This may be ok if you expect some links with only outgoing links (e.g., basal species in a food web)
## Warning: One or more columns sum to 0. This may be ok if you expect some links with only incoming links (e.g., top predators in a food web)
# A dedicated function to shuffle the networks, considering the flow.
shuffle_tur_networks <- function(x){ # x is a network object
m <- x$mat
# Assign off-diagona values
off_diagonal_lower <- m[lower.tri(m, diag = FALSE)]
off_diagonal_upper <- m[upper.tri(m, diag = FALSE)]
out <- matrix(0, nrow(m), ncol(m), dimnames = list(rownames(m), colnames(m)))
out[lower.tri(out, diag = FALSE)] <- sample(off_diagonal_lower, size = length(off_diagonal_lower), replace = F)
out[upper.tri(out, diag = FALSE)] <- sample(off_diagonal_upper, size = length(off_diagonal_upper), replace = F)
# Re-assign the diagonal (left intact)
diag(out) <- diag(m)
# Sanity checks
t1 <- setequal(out[upper.tri(out, diag = FALSE)], m[upper.tri(m, diag = FALSE)]) #Should be TRUE
t2 <- setequal(out[lower.tri(out, diag = FALSE)], m[lower.tri(m, diag = FALSE)]) #Should be TRUE
t3 <- identical(out[upper.tri(out, diag = FALSE)], m[upper.tri(m, diag = FALSE)]) #Should be FALSE
t4 <- identical(out[lower.tri(out, diag = FALSE)], m[lower.tri(m, diag = FALSE)]) #Should be FALSE
t5 <- all(table(m)==table(out))# Should be TRUE because it includes all the values, including diagonal
if (any(c(t1,t2,!t3,!t4,t5)==F)){stop('One or more sanity checks failed')}
return(out)
}
# Create a list with shuffled link lists
nsim <- 100 #nsim <- 1000
shuffled <- NULL
for (i in 1:nsim){
print(i)
x <- shuffle_tur_networks(tur_network) #Shuffle the network
x <- create_monolayer_network(x,directed = T,bipartite = F) # Create a monolayer object
shuffled[[i]] <- create_infomap_linklist(x)$edge_list_infomap #Create a link-list
}
# Use the shuffled link lists.
tur_signif <- run_infomap_monolayer(tur_network, infomap_executable='Infomap',
flow_model = 'rawdir',
silent=T,
trials=100, two_level=T, seed=200952,
signif = T, shuff_method = shuffled)
## Warning in run_infomap_monolayer(tur_network, infomap_executable = "Infomap", : pvalue is not really 0, it is <0.01
print(tur_signif$pvalue)
## [1] 0
plots <- plot_signif(tur_signif, plotit = F)
plots
## $L_plot
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
##
## $m_plot
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.