Migration

In the cases of migration, co-assembly, or community coalescence, the communities can be coalesced by using community_coalesce().

com2 <- community_generate(regional_pool, I = .01, threshold = 1e-3, seed = 2)
community_coalesce(com1, com2)

Migration for 10 times.

# Regional pool
pool_build()

# Initial resource condition
resource <- setNames(rep(1, P), paste0("R", sprintf("%03d", 1:P)))

# Initial community
result_list <- rep(list(NA), 10)
community <- community_coalesce(resource, community_generate(I = 0.1, threshold = 0.01, seed = 0))

migration_period <- 10
migration_event <- 10

for (i in 0:(migration_event-1)) {
  # Migration
  community <- community_coalesce(community, community_generate(I = 0.1, threshold = 0.01, seed = i+1))

  # CR dynamics
  if (i < (migration_event-1)) {
    result_list[[i+1]] <- 
      CR_model(community, time_limit = migration_period, time_step = 1) %>%
      as.data.frame() %>%
      mutate(time = time + i * migration_period) %>% # Real time from the very first time point
      # Slice the last time point in each migration period to prevent duplicate
      slice(1:(nrow(.)-1))
  } else {
    result_list[[i+1]] <- CR_model(community, time_limit = migration_period * 10, time_step = 1) %>%
      as.data.frame() %>%
      mutate(time = time + i * migration_period) %>%
      # Slice the time at migration_period to prevent duplicated points
      slice(1:(nrow(.)-1))
  }

  # Truncate the last time point
  temp <- filter(result_list[[i+1]], time == (i + 1) * migration_period - 1) %>% dplyr::select(variable, value) 
  community_temp <- setNames(temp$value, temp$variable)

  # Threshold for extinction
  community <- community_temp[community_temp > 1e-5]  

  #
  print(i + 1)
}
# Merge the result list into one single data frame
result_merged <- rbindlist(result_list) %>%
  filter(value >= 1e-5) %>%
  mutate(type = substr(variable, 1, 1)) 
# Plot the dynamics
result_merged %>%
  ggplot(aes(x = time, y = value, color = variable)) +
  geom_line() +
  geom_vline(xintercept = seq(0, migration_period * migration_event,  migration_period)) + 
  facet_grid(type~., scale = "free_y") +
  theme_bw() +
  theme(legend.position = "none") +
  labs(y = "value")


Chang-Yu-Chang/MigrationCommunity documentation built on Aug. 13, 2019, 9:41 p.m.