R/network_randomize.R

Defines functions network_randomize

Documented in network_randomize

#' Shuffle the network while keeping the node degree
#' @description Randomize the network
#' @name network_randomize
#' @param network an `igraph` object
#' @param b shuffling times
#' @return ...

network_randomize <- function(network, keep_direction = T) {
  g <- network
  # Shuffle links
  # g <- net
  node_number <- length(V(g))

  # Exclusion network
  index_compt <- E(g)[E(g)$interaction == "exclusion"]
  g_compt <- subgraph.edges(g, eids = index_compt, delete.vertices = F)

  if (keep_direction == T) {
    node_in_degree <- degree(g_compt, mode = "in")
    node_out_degree <- degree(g_compt, mode = "out")
    g_random_compt <- sample_degseq(out.deg = node_out_degree, in.deg = node_in_degree) %>%
      set_edge_attr("interaction", value = "exclusion")
    V(g_random_compt)$name <- V(g)$name
  } else if (keep_direction == F) {
    node_degree <- degree(g_compt, mode = "all")
    g_random_compt <- sample_degseq(node_degree, method = "simple.no.multiple") %>%
      set_edge_attr("interaction", value = "exclusion") %>%
      as.directed(mode = "arbitrary")
    V(g_random_compt)$name <- V(g)$name
  }

  # Coexistence network
  if (FALSE) {
    index_coext <- E(g)[E(g)$interaction == "coexistence"]
    g_coext <- subgraph.edges(g, eids = index_coext, delete.vertices = F) %>% as.undirected()
    node_degree <- degree(g_coext, mode = "all")
    g_random_coext <- sample_degseq(node_degree) %>%
      as.directed(mode = "mutual") %>%
      set_edge_attr("interaction", value = "coexistence")
  }

  ## The rest of the edges are all coexistence
  get.edgelist(g_random_compt)
  combn(V(g_random_compt)$name, 2)
<<<<<<< HEAD
  as.data.frame(t(combn(V(g_random_compt)$name, 2)))

  if (nrow(get.edgelist(g_random_compt)) != 0) {
    edges_coext <- anti_join(as.data.frame(t(combn(V(g_random_compt)$name, 2))), as.data.frame(t(apply(get.edgelist(g_random_compt), 1, sort))), by = c("V1", "V2")) %>% as.matrix()
    edges_coext <- c(as.vector(t(as.matrix(edges_coext))), as.vector(t(as.matrix(edges_coext[, c(2, 1)])))) # Add mutual links
  } else {
    edges_coext <- as.data.frame(t(combn(node_number, 2)))
    edges_coext <- c(as.vector(t(as.matrix(edges_coext))), as.vector(t(as.matrix(edges_coext[, c(2, 1)])))) # Add mutual links
=======
  as.data.frame(t(combn(V(g_random_compt)$name, 2)), stringsAsFactors = F)
  
  if (nrow(get.edgelist(g_random_compt)) != 0) {
    edges_coext <- anti_join(as.data.frame(t(combn(V(g_random_compt)$name, 2)), stringsAsFactors = F), as.data.frame(t(apply(get.edgelist(g_random_compt), 1, sort)), stringsAsFactors = F), by = c("V1", "V2")) %>% as.matrix()
    edges_coext <- c(as.vector(t(as.matrix(edges_coext))), as.vector(t(as.matrix(edges_coext[,c(2,1)])))) # Add mutual links
  } else {
    edges_coext = as.data.frame(t(combn(node_number, 2)), stringsAsFactors = F)
    edges_coext <- c(as.vector(t(as.matrix(edges_coext))), as.vector(t(as.matrix(edges_coext[,c(2,1)])))) # Add mutual links
>>>>>>> 381875eedd7472bfbaf2cfd62d780ca03ff9b601
  }


  # Combine the networks
  g_random <- add_edges(g_random_compt, edges_coext)
  E(g_random)$interaction[is.na(E(g_random)$interaction)] <- "coexistence"
<<<<<<< HEAD

  # Put back non-growth links (NA)
  E(net)$interaction
  E(g_random)$interaction
  nets
  g_random

  g_random
  which(E(net)$interaction == "non-growth")
  "
  "



=======
  
  # Put back non-growth links (NA) 
  edges_net_nongrowth <- get.edgelist(net)[which(E(net)$interaction == "non-growth"),]
  edges_g_random <- get.edgelist(g_random) %>% apply(1, sort) %>% t()
  ii <- paste0("V1", edges_g_random[,1], "V2", edges_g_random[,2]) %in% paste0("V1", edges_net_nongrowth[,1], "V2", edges_net_nongrowth[,2])
  E(g_random)$interaction[ii] <- "non-growth"
  
  
>>>>>>> 381875eedd7472bfbaf2cfd62d780ca03ff9b601
  # Node
  V(g_random)$name <- 1:node_number

  # Edges attributes
  edge_colrs <- c("blue", "red", "grey90")
  E(g_random)$arrow.size <- .2
  E(g_random)$edge.color[E(g_random)$interaction == "exclusion"] <- 1
  E(g_random)$edge.color[E(g_random)$interaction == "coexistence"] <- 2
  E(g_random)$edge.color[E(g_random)$interaction == "non-growth"] <- 3

  return(g_random)
<<<<<<< HEAD
=======
  
>>>>>>> 381875eedd7472bfbaf2cfd62d780ca03ff9b601
}
Chang-Yu-Chang/MigrationCommunity documentation built on Aug. 13, 2019, 9:41 p.m.