Skip to content

Commit

Permalink
rerun the simulations
Browse files Browse the repository at this point in the history
  • Loading branch information
haganjam committed Jun 30, 2023
1 parent 1859b78 commit dbb6e33
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
15 changes: 10 additions & 5 deletions scripts/02_simulation/02_generate_environments.R
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,16 @@ bind_rows(env_com, .id = "rep") %>%
# comparing the variances confirms the simulations

# make a plot for the supplementary
ggpubr::ggarrange(p1, p2, p3,
labels = c("a", "b", "c"),
font.label = list(size = 11, face = "plain"),
common.legend = TRUE,
nrow = 1)
p123 <-
ggpubr::ggarrange(p1, p2, p3,
labels = c("a", "b", "c"),
font.label = list(size = 11, face = "plain"),
common.legend = TRUE,
nrow = 1
)

ggsave("figures/SI2_fig_S3.svg", p123, units = "cm",
width = 20, height = 8)

# export these data for use in the simulations
saveRDS(env_SI, "scripts/02_simulation/02_generated_environment_SI.rds")
Expand Down
2 changes: 1 addition & 1 deletion scripts/02_simulation/03_simulate_metacommunities.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ env_list <- list(env_SI, env_TI, env_com)
set.seed(54258748)

# number of replicate simulations
N_REP <- 10
N_REP <- 500

assertthat::assert_that(
N_REP == length(env_SI) && N_REP == length(env_SI) && N_REP == length(env_com)
Expand Down
26 changes: 16 additions & 10 deletions scripts/02_simulation/05_analyse_accuracy.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ output_rye <-
filter( !(Beff %in% c( "LS", "LC", "TC", "NO" )) )

# add a variable describing the different levels of variation
output_rye$env_var <- rep(c("Spatial env. variation", "Temporal env. variation", "Combination"), each = 10*7)
output_rye$env_var <- rep(c("Spatial env. variation", "Temporal env. variation", "Combination"), each = 500*7)

# get a table of the range of the different BEF effects
output_rye %>%
Expand Down Expand Up @@ -61,16 +61,17 @@ output_rye <-
error_df <-
output_rye %>%
group_by(Beff) %>%
summarise(APE_m = median(APE, na.rm = TRUE),
PPE_med = median(PPE, na.rm = TRUE))
summarise(APE_med = median(APE, na.rm = TRUE),
APE_PI_low = quantile(APE, 0.025),
APE_PI_high = quantile(APE, 0.975))
print(error_df)

# bind the prop and absolute deviation dfs
RYE_df <- full_join(prop_df, abs_dev_df, by = "Beff")
names(RYE_df) <- c("BEF effect", "Prop. within", "Mean absolute deviation", "PI95 low", "PI95 high")
RYE_df <- full_join(prop_df, error_df, by = "Beff")
names(RYE_df) <- c("BEF effect", "Prop. within", "Med. abs. prediction error", "PI_low", "PI_high")

# export the table
write_csv(x = RYE_df, file = "figures/SI2_table1.csv")
write_csv(x = RYE_df, file = "figures/SI2_table_S2.csv")

# check the correlations
cor_obs <-
Expand All @@ -87,16 +88,21 @@ plot_rye <-
levels(plot_rye$`Within PI95%`) <- c("Yes", "No")

# visualisation of the accuracy of the method
p1<-
ggplot(data = plot_rye) +
plot_rye_x <- plot_rye %>% filter(sim_rep %in% sample(1:500, 10))
p1 <-
ggplot() +
geom_abline(intercept = 0, slope = 1, linetype = "dashed") +
geom_errorbar(mapping = aes(x = BEF_obs,
geom_errorbar(data = plot_rye_x,
mapping = aes(x = BEF_obs,
ymin = PI95_low, ymax = PI95_high,
colour = `Within PI95%`),
width = 0, alpha = 0.2, show.legend = FALSE) +
geom_point(mapping = aes(x = BEF_obs, y = mean_BEF, colour = `Within PI95%`), shape = 1,
geom_point(data = plot_rye_x,
mapping = aes(x = BEF_obs, y = mean_BEF, colour = `Within PI95%`), shape = 1,
alpha = 0.7) +
facet_wrap(~Beff, scales = "free") +
geom_text(data = cor_obs %>% mutate(cor_x = paste0("r = ", round(cor_x, 2) )),
mapping = aes(x = -Inf, y = Inf, hjust = -0.25, vjust = 1.5, label = cor_x )) +
scale_colour_manual(values = wesanderson::wes_palette(name = "Darjeeling1", n = 2)) +
ylab("Estimated biodiversity effect (with 95% PI)") +
xlab("True, simulated biodiversity effect") +
Expand Down

0 comments on commit dbb6e33

Please sign in to comment.