diff --git a/R/compute_vS.R b/R/compute_vS.R index 321a391d3..6c3b26f19 100644 --- a/R/compute_vS.R +++ b/R/compute_vS.R @@ -231,6 +231,7 @@ compute_MCint <- function(dt, pred_cols = "p_hat") { #' @keywords internal append_vS_list <- function(vS_list, internal) { iter <- length(internal$iter_list) + keep_samp_for_vS <- internal$parameters$output_args$keep_samp_for_vS # Adds v_S output above to any vS_list already computed if (iter > 1) { @@ -249,17 +250,53 @@ append_vS_list <- function(vS_list, internal) { prev_vS_list_new <- list() # Applies the mapper to update the prev_vS_list ot the new id_coalition numbering - for (k in seq_along(prev_vS_list)) { - prev_vS_list_new[[k]] <- merge(prev_vS_list[[k]], - id_coalitions_mapper[, .(id_coalition, id_coalition_new)], - by = "id_coalition" - ) - prev_vS_list_new[[k]][, id_coalition := id_coalition_new] - prev_vS_list_new[[k]][, id_coalition_new := NULL] + if (isFALSE(keep_samp_for_vS)) { + for (k in seq_along(prev_vS_list)) { + this_vS <- prev_vS_list[[k]] + + this_vS_new <- merge(this_vS, + id_coalitions_mapper[, .(id_coalition, id_coalition_new)], + by = "id_coalition" + ) + + this_vS_new[, id_coalition := id_coalition_new] + this_vS_new[, id_coalition_new := NULL] + + + prev_vS_list_new[[k]] <- this_vS_new + } + } else { + for (k in seq_along(prev_vS_list)) { + this_vS <- prev_vS_list[[k]]$dt_vS + this_samp_for_vS <- prev_vS_list[[k]]$dt_samp_for_vS + + + this_vS_new <- merge(this_vS, + id_coalitions_mapper[, .(id_coalition, id_coalition_new)], + by = "id_coalition" + ) + + this_vS_new[, id_coalition := id_coalition_new] + this_vS_new[, id_coalition_new := NULL] + + this_samp_for_vS_new <- merge(this_samp_for_vS, + id_coalitions_mapper[, .(id_coalition, id_coalition_new)], + by = "id_coalition" + ) + + this_samp_for_vS_new[, id_coalition := id_coalition_new] + this_samp_for_vS_new[, id_coalition_new := NULL] + + + prev_vS_list_new[[k]] <- list(dt_vS = this_vS_new, dt_samp_for_vS = this_samp_for_vS_new) + } } + names(prev_vS_list_new) <- names(prev_vS_list) # Merge the new vS_list with the old vS_list vS_list <- c(prev_vS_list_new, vS_list) } + + return(vS_list) } diff --git a/tests/testthat/_snaps/adaptive-output/output_lm_numeric_gaussian_group_converges_tol.rds b/tests/testthat/_snaps/adaptive-output/output_lm_numeric_gaussian_group_converges_tol.rds deleted file mode 100644 index ed6d05c26..000000000 Binary files a/tests/testthat/_snaps/adaptive-output/output_lm_numeric_gaussian_group_converges_tol.rds and /dev/null differ diff --git a/tests/testthat/_snaps/adaptive-output/output_lm_numeric_indep_conv_max_n_coalitions.rds b/tests/testthat/_snaps/adaptive-output/output_lm_numeric_indep_conv_max_n_coalitions.rds deleted file mode 100644 index 0a0f7379e..000000000 Binary files a/tests/testthat/_snaps/adaptive-output/output_lm_numeric_indep_conv_max_n_coalitions.rds and /dev/null differ diff --git a/tests/testthat/_snaps/adaptive-output/output_lm_numeric_independence_cont_est_object.rds b/tests/testthat/_snaps/adaptive-output/output_lm_numeric_independence_cont_est_object.rds deleted file mode 100644 index 0507b05cd..000000000 Binary files a/tests/testthat/_snaps/adaptive-output/output_lm_numeric_independence_cont_est_object.rds and /dev/null differ diff --git a/tests/testthat/_snaps/adaptive-output/output_lm_numeric_independence_cont_est_path.rds b/tests/testthat/_snaps/adaptive-output/output_lm_numeric_independence_cont_est_path.rds deleted file mode 100644 index 0507b05cd..000000000 Binary files a/tests/testthat/_snaps/adaptive-output/output_lm_numeric_independence_cont_est_path.rds and /dev/null differ diff --git a/tests/testthat/_snaps/adaptive-output/output_lm_numeric_independence_converges_tol.rds b/tests/testthat/_snaps/adaptive-output/output_lm_numeric_independence_converges_tol.rds deleted file mode 100644 index 02f2a785c..000000000 Binary files a/tests/testthat/_snaps/adaptive-output/output_lm_numeric_independence_converges_tol.rds and /dev/null differ diff --git a/tests/testthat/_snaps/adaptive-output/output_lm_numeric_independence_converges_tol_paired.rds b/tests/testthat/_snaps/adaptive-output/output_lm_numeric_independence_converges_tol_paired.rds deleted file mode 100644 index 02f2a785c..000000000 Binary files a/tests/testthat/_snaps/adaptive-output/output_lm_numeric_independence_converges_tol_paired.rds and /dev/null differ diff --git a/tests/testthat/_snaps/adaptive-output/output_lm_numeric_independence_reach_exact.rds b/tests/testthat/_snaps/adaptive-output/output_lm_numeric_independence_reach_exact.rds deleted file mode 100644 index 4c5089cbb..000000000 Binary files a/tests/testthat/_snaps/adaptive-output/output_lm_numeric_independence_reach_exact.rds and /dev/null differ diff --git a/tests/testthat/_snaps/adaptive-output/output_verbose_1.rds b/tests/testthat/_snaps/adaptive-output/output_verbose_1.rds deleted file mode 100644 index 876bd1a66..000000000 Binary files a/tests/testthat/_snaps/adaptive-output/output_verbose_1.rds and /dev/null differ diff --git a/tests/testthat/_snaps/adaptive-output/output_verbose_1_3.rds b/tests/testthat/_snaps/adaptive-output/output_verbose_1_3.rds deleted file mode 100644 index fd125a547..000000000 Binary files a/tests/testthat/_snaps/adaptive-output/output_verbose_1_3.rds and /dev/null differ diff --git a/tests/testthat/_snaps/adaptive-output/output_verbose_1_3_4.rds b/tests/testthat/_snaps/adaptive-output/output_verbose_1_3_4.rds deleted file mode 100644 index 583867cac..000000000 Binary files a/tests/testthat/_snaps/adaptive-output/output_verbose_1_3_4.rds and /dev/null differ diff --git a/tests/testthat/_snaps/adaptive-output/output_verbose_1_3_4_5.rds b/tests/testthat/_snaps/adaptive-output/output_verbose_1_3_4_5.rds deleted file mode 100644 index f0b55a8cf..000000000 Binary files a/tests/testthat/_snaps/adaptive-output/output_verbose_1_3_4_5.rds and /dev/null differ diff --git a/tests/testthat/_snaps/asymmetric-causal-output/output_asym_cond_reg_iterative.rds b/tests/testthat/_snaps/asymmetric-causal-output/output_asym_cond_reg_iterative.rds index 0feb8fa18..d83b9f0cc 100644 Binary files a/tests/testthat/_snaps/asymmetric-causal-output/output_asym_cond_reg_iterative.rds and b/tests/testthat/_snaps/asymmetric-causal-output/output_asym_cond_reg_iterative.rds differ diff --git a/tests/testthat/_snaps/asymmetric-causal-output/output_cat_asym_causal_mixed_cat_ad.rds b/tests/testthat/_snaps/asymmetric-causal-output/output_cat_asym_causal_mixed_cat_ad.rds index a1f893d63..0430f2d08 100644 Binary files a/tests/testthat/_snaps/asymmetric-causal-output/output_cat_asym_causal_mixed_cat_ad.rds and b/tests/testthat/_snaps/asymmetric-causal-output/output_cat_asym_causal_mixed_cat_ad.rds differ diff --git a/tests/testthat/_snaps/asymmetric-causal-output/output_mixed_asym_cond_reg.rds b/tests/testthat/_snaps/asymmetric-causal-output/output_mixed_asym_cond_reg.rds index 43e42e350..19f4a5439 100644 Binary files a/tests/testthat/_snaps/asymmetric-causal-output/output_mixed_asym_cond_reg.rds and b/tests/testthat/_snaps/asymmetric-causal-output/output_mixed_asym_cond_reg.rds differ diff --git a/tests/testthat/_snaps/asymmetric-causal-output/output_mixed_sym_caus_conf_TRUE_iterative.rds b/tests/testthat/_snaps/asymmetric-causal-output/output_mixed_sym_caus_conf_TRUE_iterative.rds index dca60b54b..f57d561a5 100644 Binary files a/tests/testthat/_snaps/asymmetric-causal-output/output_mixed_sym_caus_conf_TRUE_iterative.rds and b/tests/testthat/_snaps/asymmetric-causal-output/output_mixed_sym_caus_conf_TRUE_iterative.rds differ diff --git a/tests/testthat/_snaps/adaptive-output.md b/tests/testthat/_snaps/iterative-output.md similarity index 96% rename from tests/testthat/_snaps/adaptive-output.md rename to tests/testthat/_snaps/iterative-output.md index 72239e8d0..c3c2d8622 100644 --- a/tests/testthat/_snaps/adaptive-output.md +++ b/tests/testthat/_snaps/iterative-output.md @@ -982,3 +982,38 @@ 2: 2 42.44 4.919 -4.878 -11.9086 -0.8405 -1.1714 3: 3 42.44 7.447 -25.748 0.0324 -0.1976 0.8978 +# output_lm_numeric_independence_keep_samp_for_vS + + Code + (out <- code) + Message + Success with message: + max_n_coalitions is NULL or larger than or 2^n_features = 32, + and is therefore set to 2^n_features = 32. + + * Model class: + * Approach: independence + * Iterative estimation: TRUE + * Number of feature-wise Shapley values: 5 + * Number of observations to explain: 3 + + -- iterative computation started -- + + -- Iteration 1 ----------------------------------------------------------------- + i Using 5 of 32 coalitions, 5 new. + + -- Iteration 2 ----------------------------------------------------------------- + i Using 10 of 32 coalitions, 4 new. + + -- Iteration 3 ----------------------------------------------------------------- + i Using 12 of 32 coalitions, 2 new. + + -- Iteration 4 ----------------------------------------------------------------- + i Using 16 of 32 coalitions, 4 new. + Output + explain_id none Solar.R Wind Temp Month Day + + 1: 1 42.44 -4.541 8.330 17.491 -5.585 -3.093 + 2: 2 42.44 2.246 -3.285 -5.258 -5.585 -1.997 + 3: 3 42.44 3.704 -18.549 -1.467 -2.545 1.289 + diff --git a/tests/testthat/_snaps/iterative-output/output_lm_numeric_gaussian_group_converges_tol.rds b/tests/testthat/_snaps/iterative-output/output_lm_numeric_gaussian_group_converges_tol.rds new file mode 100644 index 000000000..07003dc0c Binary files /dev/null and b/tests/testthat/_snaps/iterative-output/output_lm_numeric_gaussian_group_converges_tol.rds differ diff --git a/tests/testthat/_snaps/iterative-output/output_lm_numeric_indep_conv_max_n_coalitions.rds b/tests/testthat/_snaps/iterative-output/output_lm_numeric_indep_conv_max_n_coalitions.rds new file mode 100644 index 000000000..e7ba91106 Binary files /dev/null and b/tests/testthat/_snaps/iterative-output/output_lm_numeric_indep_conv_max_n_coalitions.rds differ diff --git a/tests/testthat/_snaps/iterative-output/output_lm_numeric_independence_cont_est_object.rds b/tests/testthat/_snaps/iterative-output/output_lm_numeric_independence_cont_est_object.rds new file mode 100644 index 000000000..446552c2c Binary files /dev/null and b/tests/testthat/_snaps/iterative-output/output_lm_numeric_independence_cont_est_object.rds differ diff --git a/tests/testthat/_snaps/iterative-output/output_lm_numeric_independence_cont_est_path.rds b/tests/testthat/_snaps/iterative-output/output_lm_numeric_independence_cont_est_path.rds new file mode 100644 index 000000000..446552c2c Binary files /dev/null and b/tests/testthat/_snaps/iterative-output/output_lm_numeric_independence_cont_est_path.rds differ diff --git a/tests/testthat/_snaps/adaptive-output/output_lm_numeric_independence_converges_maxit.rds b/tests/testthat/_snaps/iterative-output/output_lm_numeric_independence_converges_maxit.rds similarity index 56% rename from tests/testthat/_snaps/adaptive-output/output_lm_numeric_independence_converges_maxit.rds rename to tests/testthat/_snaps/iterative-output/output_lm_numeric_independence_converges_maxit.rds index 752207bf6..6f0d11cd9 100644 Binary files a/tests/testthat/_snaps/adaptive-output/output_lm_numeric_independence_converges_maxit.rds and b/tests/testthat/_snaps/iterative-output/output_lm_numeric_independence_converges_maxit.rds differ diff --git a/tests/testthat/_snaps/iterative-output/output_lm_numeric_independence_converges_tol.rds b/tests/testthat/_snaps/iterative-output/output_lm_numeric_independence_converges_tol.rds new file mode 100644 index 000000000..63ad73065 Binary files /dev/null and b/tests/testthat/_snaps/iterative-output/output_lm_numeric_independence_converges_tol.rds differ diff --git a/tests/testthat/_snaps/iterative-output/output_lm_numeric_independence_converges_tol_paired.rds b/tests/testthat/_snaps/iterative-output/output_lm_numeric_independence_converges_tol_paired.rds new file mode 100644 index 000000000..63ad73065 Binary files /dev/null and b/tests/testthat/_snaps/iterative-output/output_lm_numeric_independence_converges_tol_paired.rds differ diff --git a/tests/testthat/_snaps/iterative-output/output_lm_numeric_independence_keep_samp_for_vS.rds b/tests/testthat/_snaps/iterative-output/output_lm_numeric_independence_keep_samp_for_vS.rds new file mode 100644 index 000000000..6d8f2e912 Binary files /dev/null and b/tests/testthat/_snaps/iterative-output/output_lm_numeric_independence_keep_samp_for_vS.rds differ diff --git a/tests/testthat/_snaps/iterative-output/output_lm_numeric_independence_reach_exact.rds b/tests/testthat/_snaps/iterative-output/output_lm_numeric_independence_reach_exact.rds new file mode 100644 index 000000000..56ef76740 Binary files /dev/null and b/tests/testthat/_snaps/iterative-output/output_lm_numeric_independence_reach_exact.rds differ diff --git a/tests/testthat/_snaps/iterative-output/output_verbose_1.rds b/tests/testthat/_snaps/iterative-output/output_verbose_1.rds new file mode 100644 index 000000000..ed049c2a0 Binary files /dev/null and b/tests/testthat/_snaps/iterative-output/output_verbose_1.rds differ diff --git a/tests/testthat/_snaps/iterative-output/output_verbose_1_3.rds b/tests/testthat/_snaps/iterative-output/output_verbose_1_3.rds new file mode 100644 index 000000000..964fcc670 Binary files /dev/null and b/tests/testthat/_snaps/iterative-output/output_verbose_1_3.rds differ diff --git a/tests/testthat/_snaps/iterative-output/output_verbose_1_3_4.rds b/tests/testthat/_snaps/iterative-output/output_verbose_1_3_4.rds new file mode 100644 index 000000000..b6fef86c3 Binary files /dev/null and b/tests/testthat/_snaps/iterative-output/output_verbose_1_3_4.rds differ diff --git a/tests/testthat/_snaps/iterative-output/output_verbose_1_3_4_5.rds b/tests/testthat/_snaps/iterative-output/output_verbose_1_3_4_5.rds new file mode 100644 index 000000000..ef7ad93c4 Binary files /dev/null and b/tests/testthat/_snaps/iterative-output/output_verbose_1_3_4_5.rds differ diff --git a/tests/testthat/_snaps/adaptive-setup.md b/tests/testthat/_snaps/iterative-setup.md similarity index 100% rename from tests/testthat/_snaps/adaptive-setup.md rename to tests/testthat/_snaps/iterative-setup.md diff --git a/tests/testthat/_snaps/regression-output/output_lm_numeric_lm_separate_iterative.rds b/tests/testthat/_snaps/regression-output/output_lm_numeric_lm_separate_iterative.rds index c2ff52301..abe999492 100644 Binary files a/tests/testthat/_snaps/regression-output/output_lm_numeric_lm_separate_iterative.rds and b/tests/testthat/_snaps/regression-output/output_lm_numeric_lm_separate_iterative.rds differ diff --git a/tests/testthat/test-adaptive-output.R b/tests/testthat/test-iterative-output.R similarity index 93% rename from tests/testthat/test-adaptive-output.R rename to tests/testthat/test-iterative-output.R index 0be8006c0..1974ecb64 100644 --- a/tests/testthat/test-adaptive-output.R +++ b/tests/testthat/test-iterative-output.R @@ -310,3 +310,23 @@ test_that("output_verbose_1_3_4_5", { "output_verbose_1_3_4_5" ) }) + + +# Just checking that internal$output$dt_samp_for_vS works for iterative +test_that("output_lm_numeric_independence_keep_samp_for_vS", { + expect_snapshot_rds( + (out <- explain( + testing = TRUE, + model = model_lm_numeric, + x_explain = x_explain_numeric, + x_train = x_train_numeric, + approach = "independence", + phi0 = p0, + output_args = list(keep_samp_for_vS = TRUE), + iterative = TRUE + )), + "output_lm_numeric_independence_keep_samp_for_vS" + ) + + expect_false(is.null(out$internal$output$dt_samp_for_vS)) +}) diff --git a/tests/testthat/test-adaptive-setup.R b/tests/testthat/test-iterative-setup.R similarity index 100% rename from tests/testthat/test-adaptive-setup.R rename to tests/testthat/test-iterative-setup.R