From 353c0883f09b290129a0659e7e793627a849717d Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Thu, 29 Feb 2024 21:09:41 +0800 Subject: [PATCH] Add correlation wth RAPM without resampling reasoning tasks Signed-off-by: Liang Zhang --- R/constants.R | 15 ++++++ R/tar_prepare.R | 13 +++--- _scripts/g_factor.R | 5 +- _scripts/g_factor_cor_rapm.R | 90 ++++++++++++++++++++++++++++++++++++ _targets.yaml | 3 ++ 5 files changed, 116 insertions(+), 10 deletions(-) create mode 100644 _scripts/g_factor_cor_rapm.R diff --git a/R/constants.R b/R/constants.R index 1c2514e..3b4cbe3 100644 --- a/R/constants.R +++ b/R/constants.R @@ -1,6 +1,21 @@ game_id_rapm <- bit64::as.integer64(265520726213317) # 瑞文高级推理 # data for this game should be removed because another version was used game_id_rata <- bit64::as.integer64(240052750955077) # 远距离联想A (obsolete) +game_id_reasoning <- bit64::as.integer64( + c( + 355740871500677, + 355740333933445, + 356101783560965, + 411281158706373, + 324179001000709, + 268008982659144, + 324179964613381, + 361750352892805, + 324177880433541 + ) +) +# we define here to be used in g factor resampling +num_vars_total <- 76 # used in data quality check thresh_prop_miss <- 0.25 diff --git a/R/tar_prepare.R b/R/tar_prepare.R index 9813a9c..961d6e4 100644 --- a/R/tar_prepare.R +++ b/R/tar_prepare.R @@ -1,10 +1,11 @@ -prepare_config_vars <- function(num_vars_total, n_steps) { - num_vars_base <- num_vars_total %/% n_steps - tibble::tibble( - num_vars = seq(num_vars_base, num_vars_total, num_vars_base), - use_pairs = num_vars * 2 <= num_vars_total - ) |> +prepare_config_vars <- function(num_vars_total, ..., + step = 3, from = 3, use_pairs = TRUE) { + out <- tibble::tibble(num_vars = seq(from, num_vars_total, step)) |> dplyr::filter(choose(num_vars_total, num_vars) > 1e4) + if (use_pairs) { + out$use_pairs <- out$num_vars * 2 <= num_vars_total + } + out } prepare_config_cpm <- function(...) { diff --git a/_scripts/g_factor.R b/_scripts/g_factor.R index 5a8f662..7333f2f 100644 --- a/_scripts/g_factor.R +++ b/_scripts/g_factor.R @@ -7,10 +7,7 @@ tar_option_set( ) setup_parallel_plan() -n_vars_total <- 76 -n_steps <- 20 - -config_vars <- prepare_config_vars(n_vars_total, n_steps) +config_vars <- prepare_config_vars(num_vars_total) config_cpm <- prepare_config_cpm( config == "gsr", task == "wm", diff --git a/_scripts/g_factor_cor_rapm.R b/_scripts/g_factor_cor_rapm.R new file mode 100644 index 0000000..260e61f --- /dev/null +++ b/_scripts/g_factor_cor_rapm.R @@ -0,0 +1,90 @@ +library(targets) +tar_source() +tar_option_set( + packages = c("tidyverse", "bit64", "lavaan"), + format = "qs", + controller = setup_crew_controller("gf") +) +setup_parallel_plan() + +config_vars <- prepare_config_vars( + num_vars_total - length(game_id_reasoning), + use_pairs = FALSE +) +config_cpm <- prepare_config_cpm( + config == "gsr", + task == "wm", + run == "full", + thresh_method == "alpha", + thresh_level == 0.01 +) +branches_g <- tarchetypes::tar_map( + config_vars, + tarchetypes::tar_rep( + vars_sample, + resample_vars(names(indices_cogstruct), num_vars), + batches = 10, + reps = 10, + iteration = "list", + deployment = "main" + ), + tarchetypes::tar_rep2( + fit_g, + lapply_tar_batches( + vars_sample, + fit_efa_g, + data = indices_cogstruct, + missing = "ml" + ), + vars_sample, + iteration = "list" + ), + tarchetypes::tar_rep2( + scores_g, + lapply_tar_batches( + fit_g, + extract_g_scores, + data = indices_cogstruct + ), + fit_g, + iteration = "list" + ), + tarchetypes::tar_rep2( + cor_rapm, + lapply_tar_batches( + scores_g, + \(x) { + indices_rapm |> + merge(x, by = "row.names") |> + summarise(r = cor(score, f1, use = "pairwise")) + } + ) |> + list_rbind_tar_batches(), + scores_g, + iteration = "list" + ) +) + +list( + tarchetypes::tar_file_read( + indices_cogstruct, + path_obj_from_proj("indices_cogstruct", "prepare_source_data"), + read = qs::qread(!!.x) + ), + tarchetypes::tar_file_read( + indices_rapm, + path_obj_from_proj("indices_rapm", "prepare_source_data"), + read = qs::qread(!!.x) + ), + branches_g, + tarchetypes::tar_combine( + cor_rapm, + branches_g$cor_rapm, + command = list(!!!.x) |> + lapply(bind_rows) |> + bind_rows_meta( + .names = names(config_vars), + .prefix = "cor_rapm" + ) + ) +) diff --git a/_targets.yaml b/_targets.yaml index 9d0d73c..e589aef 100644 --- a/_targets.yaml +++ b/_targets.yaml @@ -10,6 +10,9 @@ prepare_neural: g_factor: script: _scripts/g_factor.R store: _targets/g_factor +g_factor_cor_rapm: + script: _scripts/g_factor_cor_rapm.R + store: _targets/g_factor_cor_rapm explore_factors: script: _scripts/explore_factors.R store: _targets/explore_factors