From 46e0b06eec063978212ec559897643a9119e5c0a Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Tue, 24 Sep 2024 02:49:47 +0800 Subject: [PATCH] Add gss dynamic --- R/mantel.R | 27 +++++++++++++++++++++++++++ R/sync.R | 7 ------- R/utils.R | 8 -------- _targets.R | 53 +++++++++++++++++++++++++++++++---------------------- 4 files changed, 58 insertions(+), 37 deletions(-) create mode 100644 R/mantel.R delete mode 100644 R/sync.R diff --git a/R/mantel.R b/R/mantel.R new file mode 100644 index 0000000..a92b7b8 --- /dev/null +++ b/R/mantel.R @@ -0,0 +1,27 @@ +calc_mantel <- function(data, ydis, col_xdis = last_col()) { + data |> + mutate( + mantel = map( + pick({{ col_xdis }})[[1]], + \(xdis) vegan::mantel(xdis, ydis) + ), + .keep = "unused" + ) +} + +extract_stats_mantel <- function(data_mantel) { + data_mantel |> + mutate( + map(mantel, tidy_mantel) |> + list_rbind(), + .keep = "unused" + ) +} + +tidy_mantel <- function(mantel) { + tibble( + statistic = mantel$statistic, + p.value = mantel$signif, + method = mantel$method + ) +} diff --git a/R/sync.R b/R/sync.R deleted file mode 100644 index e5561b0..0000000 --- a/R/sync.R +++ /dev/null @@ -1,7 +0,0 @@ -calc_sync_smc <- function(sync_whole_trials, smc) { - sync_whole_trials |> - mutate( - mantel = map(neu_sync, ~ vegan::mantel(.x, smc)), - .keep = "unused" - ) -} diff --git a/R/utils.R b/R/utils.R index 43ffe84..2a2ba5e 100644 --- a/R/utils.R +++ b/R/utils.R @@ -8,14 +8,6 @@ convert_p2_p1 <- function(statistic, p.value, ) } -tidy_mantel <- function(mantel) { - tibble( - statistic = mantel$statistic, - p.value = mantel$signif, - method = mantel$method - ) -} - get_resid <- function(y, x) { resid(lm(y ~ x, na.action = na.exclude)) } diff --git a/_targets.R b/_targets.R index d716b4d..b6f1629 100644 --- a/_targets.R +++ b/_targets.R @@ -174,6 +174,31 @@ list( collect() |> calc_group_pattern() ), + tar_target( + patterns_group_dynamic, + arrow::read_parquet(file_cca_y) |> + calc_group_pattern_dynamic() + ), + tar_target(data_gss_dynamic, calc_mantel(patterns_group_dynamic, pattern_semantics)), + tar_target(stats_gss_dynamic, extract_stats_mantel(data_gss_dynamic)), + tarchetypes::tar_rep( + data_gss_dynamic_permuted, + calc_mantel( + patterns_group_dynamic, + seriation::permute(pattern_semantics, sample.int(150L)) + ), + reps = 10, + batches = 100 + ), + tarchetypes::tar_rep2( + stats_gss_dynamic_permuted, + extract_stats_mantel(data_gss_dynamic_permuted), + data_gss_dynamic_permuted + ), + tar_target( + clusters_stats_gss_dynamic, + calc_clusters_stats(stats_gss_dynamic, stats_gss_dynamic_permuted) + ), tar_target( # leave one out patterns_group_whole_loo, @@ -349,10 +374,7 @@ list( .by = cca_id ) ), - tar_target( - sync_smc, - calc_sync_smc(sync_whole_trials, smc) - ), + tar_target(sync_smc, calc_mantel(sync_whole_trials, smc)), tar_target( sync_dynamic, whole_erps |> @@ -367,13 +389,14 @@ list( .by = cca_id ) ), + tar_target(sync_smc_dynamic, calc_mantel(sync_dynamic, smc)), tar_target( - sync_smc_dynamic, - calc_sync_smc(sync_dynamic, smc) + stats_sync_smc_dynamic, + extract_stats_mantel(sync_smc_dynamic) ), tarchetypes::tar_rep( sync_smc_dynamic_permuted, - calc_sync_smc( + calc_mantel( sync_dynamic, seriation::permute(smc, sample.int(206L)) ), @@ -382,23 +405,9 @@ list( ), tarchetypes::tar_rep2( stats_sync_smc_dynamic_permuted, - sync_smc_dynamic_permuted |> - mutate( - map(mantel, tidy_mantel) |> - list_rbind(), - .keep = "unused" - ), + extract_stats_mantel(sync_smc_dynamic_permuted), sync_smc_dynamic_permuted ), - tar_target( - stats_sync_smc_dynamic, - sync_smc_dynamic |> - mutate( - map(mantel, tidy_mantel) |> - list_rbind(), - .keep = "unused" - ) - ), tar_target( clusters_stats_sync_smc_dynamic, stats_sync_smc_dynamic |>