Skip to content

Commit 7f92829

Browse files
committed
πŸ—οΈ restructure around new topo metrics
1 parent de2c9f5 commit 7f92829

8 files changed

Lines changed: 67 additions & 15 deletions

File tree

β€Žcode/Analyse_END_simulations.Rβ€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ sim_out_long <- sim_out %>%
3030
mutate(metric = factor(metric, levels = metrics)) %>%
3131
# set niche model as reference model
3232
glow_up(model = factor(model)) %>%
33-
glow_up(model = relevel(model, ref = "ADBM"))
33+
glow_up(model = relevel(model, ref = "Niche"))
3434

3535
boxplot_sim <- ggplot(sim_out_long, aes(x = model, y = value, colour = model)) +
3636
geom_boxplot(outlier.alpha = 0.5, width = 0.6) +
@@ -66,7 +66,7 @@ lda_scores$model <- predict(lda_fit)$class
6666

6767
# Compute niche centroid
6868
niche_centroid_lda <- lda_scores %>%
69-
yeet(model == "ADBM") %>%
69+
yeet(model == "Niche") %>%
7070
no_cap(across(starts_with("LD"), mean))
7171

7272
# Centre

β€Žcode/Topology.Rβ€Ž

Lines changed: 65 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ df <- read_csv("data/outputs/topology.csv") %>%
2727
na.omit() %>%
2828
# set niche model as reference model
2929
glow_up(model = factor(model)) %>%
30-
glow_up(model = relevel(model, ref = "ADBM"))
30+
glow_up(model = relevel(model, ref = "Niche"))
3131

3232
# Dependent variable matrix for multivariate tests
3333
# Assuming columns 2 onwards are your topological metrics
@@ -64,7 +64,7 @@ scores$model <- df$model
6464

6565
# Compute niche centroid
6666
niche_centroid <- scores %>%
67-
yeet(model == "ADBM") %>%
67+
yeet(model == "Niche") %>%
6868
no_cap(across(starts_with("Can"), mean))
6969

7070
# Center all scores on niche
@@ -79,9 +79,11 @@ loadings_df <- as.data.frame(cda$structure[, 1:2]) %>%
7979
rownames_to_column("Metric") %>%
8080
rename(CV1 = Can1, CV2 = Can2) %>%
8181
glow_up(Level = case_when(
82-
Metric %in% c("complexity", "connectance", "trophic_level") ~ "Macro",
83-
Metric %in% c("generality", "vulnerability") ~ "Micro",
84-
TRUE ~ "Meso"
82+
Metric %in% c("complexity", "connectance", "trophic_level", "ChLen") ~ "Macro",
83+
Metric %in% c("generality") ~ "Role",
84+
Metric %in% c("vulnerability", "top") ~ "Heterogeneity",
85+
Metric %in% c("distance") ~ "Path",
86+
TRUE ~ "Scaling"
8587
))
8688

8789
# CDA Loadings Plot
@@ -94,7 +96,7 @@ ggplot(loadings_df, aes(x = CV1, y = CV2)) +
9496
arrow = arrow(length = unit(0.2, "cm")), linewidth = 1) +
9597
geom_text_repel(aes(label = Metric)) +
9698
coord_equal(xlim = c(-1, 1), ylim = c(-1, 1)) +
97-
scale_colour_manual(values = c("#006D75", "#2F2F2F", "#EA7200")) +
99+
scale_colour_manual(values = c("#006D75", "#2F2F2F", "#EA7200", "#B2B4B2", "#FFB81C")) +
98100
figure_theme
99101

100102
ggsave("../figures/lda_corr.png",
@@ -144,7 +146,7 @@ lda_scores$model <- df$model
144146

145147
# Compute niche centroid
146148
niche_centroid_lda <- lda_scores %>%
147-
yeet(model == "ADBM") %>%
149+
yeet(model == "Niche") %>%
148150
no_cap(across(starts_with("LD"), mean))
149151

150152
# Centre
@@ -216,13 +218,16 @@ emm_df <- emm_df %>%
216218
TRUE ~ as.character(metric)
217219
)) %>%
218220
mutate(level = case_when(
219-
metric %in% c("complexity", "connectance", "trophic_level") ~ "Macro",
220-
metric %in% c("generality", "vulnerability") ~ "Micro",
221-
TRUE ~ "Meso"
221+
metric %in% c("complexity", "connectance", "trophic_level", "S2", "S1", "ChLen") ~ "Macro",
222+
metric %in% c("generality") ~ "Role",
223+
metric %in% c("vulnerability", "top") ~ "Heterogeneity",
224+
metric %in% c("distance") ~ "Path",
225+
metric %in% c("centrality") ~ "Hubs",
226+
TRUE ~ "Scaling"
222227
))
223228

224229
# --- 2. Create Categorized Plot List ---
225-
levs <- c("Macro", "Meso", "Micro")
230+
levs <- c("Macro", "Role", "Heterogeneity", "Path", "Scaling")
226231
plot_list_emm <- vector("list", length = 3)
227232

228233
for (i in seq_along(levs)) {
@@ -256,8 +261,55 @@ for (i in seq_along(levs)) {
256261

257262
# --- 3. Patchwork Assembly ---
258263
# Adjusting heights to accommodate the number of facets in each level
259-
(plot_list_emm[[1]] / plot_list_emm[[2]] / plot_list_emm[[3]]) +
260-
plot_layout(heights = c(2, 2, 1), guides = "collect")
264+
(plot_list_emm[[1]] / (plot_list_emm[[2]] + plot_list_emm[[4]]) /
265+
plot_list_emm[[3]] / plot_list_emm[[5]])+
266+
plot_layout(heights = c(3, 1, 1, 1), guides = "collect")
261267

262268
# Save
263269
ggsave("../figures/emm_summary.png", width = 9, height = 12, dpi = 400)
270+
271+
# =========================
272+
# 5. Standard Errors
273+
# =========================
274+
275+
sims <-
276+
read_csv("data/outputs/topology.csv") %>%
277+
na.omit() %>%
278+
pivot_longer(-c(model),
279+
names_to = "metric") %>%
280+
squad_up(model, metric)
281+
282+
# use Niche model as reference point
283+
ref_stats <- sims %>%
284+
yeet(model == "Niche") %>%
285+
squad_up(metric) %>%
286+
no_cap(mu_ref = mean(value),
287+
sd_ref = sd(value))
288+
289+
comparison <- sims %>%
290+
squad_up(model, metric) %>%
291+
no_cap(mu_model = mean(value, na.rm = TRUE)) %>%
292+
left_join(ref_stats, by = "metric") %>%
293+
glow_up(z = (mu_model - mu_ref) / sd_ref)
294+
295+
ggplot(comparison %>%
296+
yeet(metric != "richness")) +
297+
geom_hline(yintercept = 2,
298+
linetype = "dashed",
299+
colour = shark_silver) +
300+
geom_hline(yintercept = -2,
301+
linetype = "dashed",
302+
colour = shark_silver) +
303+
geom_point(aes(x = metric,
304+
y = z,
305+
colour = model),
306+
alpha = 0.7) +
307+
scale_colour_manual(values = model_colours) +
308+
labs(x = "Model",
309+
y = "Normalised Error (niche as reference)") +
310+
figure_theme +
311+
theme(panel.grid.major = element_blank(),
312+
strip.text = ggtext::element_markdown())
313+
314+
315+
-740 Bytes
Loading

β€Žfigures/cv.pngβ€Ž

206 KB
Loading

β€Žfigures/emm_summary.pngβ€Ž

116 KB
Loading

β€Žfigures/lda.pngβ€Ž

189 KB
Loading

β€Žfigures/lda_compare.pngβ€Ž

-76.6 KB
Loading

β€Žfigures/lda_corr.pngβ€Ž

-10.9 KB
Loading

0 commit comments

Comments
Β (0)