Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lars/add MSEv eval criterion #357

Merged
merged 165 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
165 commits
Select commit Hold shift + click to select a range
9578520
Bugfix in `prepare_data()` related to vector of approaches. When usin…
LHBO Aug 24, 2023
fe0988f
Implemented function that computes the MSEv evaluation criterion for …
LHBO Aug 24, 2023
9635b07
Plot the MSEv evaluation criterion.
LHBO Aug 24, 2023
b1c8b32
Added a section about the MSEv criterion in the vignette. This is s a…
LHBO Aug 24, 2023
8da040d
# Lars have added `n_combinations` - 1 as a possibility, as the funct…
LHBO Aug 24, 2023
e9f6ae9
Samll typo.
LHBO Aug 24, 2023
7412780
Fixed bug. All messages says "n_combinations is larger than or equal …
LHBO Aug 24, 2023
83aa9e9
Changed to exclude the coalitions which are not effected by the appro…
LHBO Aug 30, 2023
8b12e85
Added example to the roxygen. This is technically not needed as it is…
LHBO Aug 30, 2023
e55bc05
Added some parameters to the plot function to make it more flexiable …
LHBO Aug 30, 2023
d598f56
Updated code as I had only changed the legend for the fill (which wor…
LHBO Aug 30, 2023
c28e202
Added the updated namespace file. Maybe that is why the tests on gith…
LHBO Aug 30, 2023
22c8e17
Added script demonstrating the bug that shapr does not enter the exac…
LHBO Aug 31, 2023
a05b82f
Added (tentative) test that checks that shapr enters exact mode when …
LHBO Aug 31, 2023
d0f278d
Added script that demonstrates the bug before the bugfix, and added t…
LHBO Aug 31, 2023
6fd2d91
Fixed lint warnings in `approach.R`.
LHBO Aug 31, 2023
36a4a18
Added `ctree` in the example in roxygen
LHBO Sep 1, 2023
c0a7926
Add manuals for the two funtions realted to MSEv criterion.
LHBO Sep 1, 2023
152d932
Updated plot function (fixed some inconsistencies) and added detailed…
LHBO Sep 1, 2023
63ed121
Started to make tests, but ran into a shapr bug.
LHBO Sep 1, 2023
4f0bdb9
Added two parameters to the `internal$parameters` list which contains…
LHBO Sep 4, 2023
2a940bf
Added test to check that `n_batches` must be larger than or equal to …
LHBO Sep 4, 2023
303df5c
Updated `get_default_n_batches` to take into consideration the number…
LHBO Sep 4, 2023
c3c7a87
Changed where seed is set such that it applies for both regular and c…
LHBO Sep 4, 2023
8e6cc9b
Typo
LHBO Sep 4, 2023
246c2cf
Added test to check that setting the seed works for combined approaches.
LHBO Sep 4, 2023
e873f1d
typo in test function
LHBO Sep 4, 2023
5a2c2eb
Added file to demonstrate the bugs (before the bugfix)
LHBO Sep 4, 2023
42c5ed1
Added new test
LHBO Sep 4, 2023
bccf6ff
Updated tests by removing n_samples
LHBO Sep 4, 2023
078c838
Added a bugfix to shapr not using the correct number of batches. Mayb…
LHBO Sep 4, 2023
703b248
Updated the demonstration script
LHBO Sep 4, 2023
c903e6b
Added last test and fixed lintr
LHBO Sep 4, 2023
801ff5f
Lint again.
LHBO Sep 4, 2023
3d216ee
Merge remote-tracking branch 'origin/master' into Lars/bugfix_n_combi…
martinju Sep 5, 2023
9de817f
styler
martinju Sep 5, 2023
d263f37
minor edits to tests
martinju Sep 5, 2023
9fbd49f
Merge branch 'Lars/bugfix_n_combinations' into Lars/bugfix_combined_a…
martinju Sep 5, 2023
14acadc
simplifies comment
martinju Sep 5, 2023
0f617a4
comb files ok
martinju Sep 5, 2023
a0753e5
Merge master into branch
LHBO Sep 5, 2023
b7d4402
Updated bug in independence approach related to categorical features …
LHBO Sep 6, 2023
c28a264
Updated bug in independence approach related to categorical features …
martinju Sep 5, 2023
0bf42bd
Merge branch 'Lars/bugfix_combined_approaches' of https://github.com/…
LHBO Sep 6, 2023
7971eec
lint warning
LHBO Sep 6, 2023
c35e0bc
Lint
LHBO Sep 7, 2023
cb15629
lint
LHBO Sep 7, 2023
6fb5350
Merge with the branch with the bugfix for the combined approaches. No…
LHBO Sep 7, 2023
8f7d1e1
Updated roxygen
LHBO Sep 7, 2023
0168e83
Merge branch 'Lars/bugfix_get_default_n_batches' into Lars/add_MSEv_e…
LHBO Sep 7, 2023
452772b
Added plot test functions for MSEv criterion
LHBO Sep 7, 2023
220d339
Ran styler
LHBO Sep 8, 2023
6faf28f
Updated most lintr. I disagree that variable names should be less tha…
LHBO Sep 8, 2023
8f406a5
Previous version would not test the output but rather that check that…
LHBO Sep 8, 2023
2ad754a
Added some extra parameters to the one test function.
LHBO Sep 8, 2023
8516e76
Updated some parameters in the MSEv test plots. Looked at all of them…
LHBO Sep 8, 2023
3266e45
SupressMessages to run `testthat::test_file` without messages, and ch…
LHBO Sep 8, 2023
40090e4
Add test images (get the same each time I run `testthat::test_file()`).
LHBO Sep 8, 2023
0c3cf83
Fixed lintr and styler
LHBO Sep 8, 2023
acf9bab
Updated vignette with MSEv. Fixed lintr and ensured that the figures …
LHBO Sep 8, 2023
d1a19b3
updated test files after accepting new values
martinju Sep 9, 2023
a3c238b
adjustments to comments and Lars' TODO-comments
martinju Sep 9, 2023
b0dd3bf
update snapshot file after weight adjustment
martinju Sep 10, 2023
0988ace
cleaned up doc
martinju Sep 10, 2023
f790c22
rerun doc
martinju Sep 10, 2023
f9116db
style
martinju Sep 10, 2023
67e5064
Merge branch 'master' into Lars/bugfix_combined_approaches
martinju Sep 10, 2023
1fa2753
Merge branch 'Lars/bugfix_combined_approaches' into Lars/bugfix_get_d…
martinju Sep 11, 2023
70f353f
Merged master into branch.
LHBO Sep 11, 2023
e0d925d
Changed to `n_batches = 10` in the combined approaches, as the previo…
LHBO Sep 12, 2023
9bbc89b
Updated some messages in plot
LHBO Sep 14, 2023
4000e7a
Merged the master into this branch
LHBO Sep 15, 2023
8f5ba2b
Minor updates to `make_MSEv_evaluation_criterion_plots`
LHBO Sep 15, 2023
b3b1375
Update the manuals
LHBO Sep 15, 2023
63815a6
Updated the MSEv text in the vignette.
LHBO Sep 18, 2023
11cf088
Merge branch 'Lars/bugfix_get_default_n_batches' of github.com:LHBO/s…
martinju Nov 16, 2023
0b146bc
accept OK test changes
martinju Nov 16, 2023
fa6a5b9
additonal Ok test files
martinju Nov 16, 2023
c2599fe
change batches in test files
martinju Nov 16, 2023
aacb474
accept new files
martinju Nov 16, 2023
4dd1a86
handle issue with a breaking change update in the testthat package
martinju Nov 17, 2023
30c202d
+ these
martinju Nov 17, 2023
a224648
removing last (unused) input of approach
martinju Nov 20, 2023
b6da078
updating tests
martinju Nov 20, 2023
c9ade53
+ update setup tests/snaps
martinju Nov 20, 2023
4851217
correcting unique length
martinju Nov 20, 2023
cdc624d
update linting and vignette
martinju Nov 20, 2023
8833b0f
update docs
martinju Nov 20, 2023
b865a65
fix example issue
martinju Nov 20, 2023
53c57eb
temporary disable tests on older R systems
martinju Nov 20, 2023
a04f127
remove unecessary if-else test
martinju Nov 20, 2023
79ddd35
data.table style on Lars's batch adjustment suggestion
martinju Nov 20, 2023
4253ef5
del comment
martinju Nov 20, 2023
2fd62b5
lint
martinju Nov 20, 2023
9562b84
Merge branch 'Lars/bugfix_get_default_n_batches' into Lars/add_MSEv_e…
martinju Nov 20, 2023
3559ffb
snaps + test adjustment
martinju Nov 20, 2023
0b29884
update plotting snaps to make tests pass
martinju Nov 20, 2023
3e6c477
update vignette
martinju Nov 20, 2023
30f4de8
.
martinju Nov 20, 2023
b7dc711
Renamed `compute_MSEv_evaluation_criterion()` to `compute_MSEv_eval_c…
LHBO Nov 20, 2023
0d55b2c
Renamed `MSEv_evaluation_criterion_for_each_coalition` to `MSEv_eval_…
LHBO Nov 20, 2023
50cd9ed
Shorted function/variable names to less than 30 char.
LHBO Nov 20, 2023
5cec958
Renamed `MSEv_eval_crit_comb` to `MSEv_eval_crit_combination` for con…
LHBO Nov 20, 2023
27e72c7
Added option for using Shapley weights in `compute_MSEv_eval_crit()`
LHBO Nov 21, 2023
34aecbf
Merge remote-tracking branch 'origin/master' into Lars/add_MSEv_eval_…
martinju Nov 21, 2023
4159b7d
update snaps after name change
martinju Nov 21, 2023
f39fbb1
update function names in tests
martinju Nov 21, 2023
e5c21b6
new figure snaps
martinju Nov 21, 2023
264489e
update function names in vignette
martinju Nov 21, 2023
a9cae1f
man + example bug
martinju Nov 21, 2023
20ea8e8
Fixed places where variable names where not updated
LHBO Nov 21, 2023
e2e543b
Manual updates
LHBO Nov 21, 2023
621e052
Added `library(shapr)` in examples.
LHBO Nov 21, 2023
69aa897
Updated combined approach in `make_MSEv_eval_crit_plots` example to s…
LHBO Nov 21, 2023
0d74c50
Export `compute_MSEv_eval_crit` to make `devtools::run_examples()` run.
LHBO Nov 21, 2023
65c4fae
Merge branch 'Lars/add_MSEv_eval_criterion' of github.com:LHBO/shapr …
martinju Nov 24, 2023
d2703c8
Added support for MSEv criterion in `explain()`.
LHBO Nov 26, 2023
62b8433
Updated MSEv criterion based on feedback.
LHBO Nov 26, 2023
1f6ee0b
Added checks for MSEv criterion
LHBO Nov 26, 2023
e505565
Refactored the plot function for MSEv based on feedback
LHBO Nov 26, 2023
0daca51
Updated vignette to reflect changes in MSEv criterion code.
LHBO Nov 26, 2023
79a6c87
Fixed MSEv_combination. Now taking mean of it gives MSEv and we see t…
LHBO Nov 27, 2023
b68b127
Removed default palette and refactored the code.
LHBO Nov 27, 2023
faeb7f4
Updated the vignette to reflect the changes in MSEv criterion.
LHBO Nov 27, 2023
8d1c670
Set default MSEv values in `setup()` as `explain_forecast()` do not s…
LHBO Nov 27, 2023
ab016b6
Merge branch 'Lars/add_MSEv_eval_criterion' of github.com:LHBO/shapr …
martinju Nov 27, 2023
392bfc4
Added tests for `MSEv_skip_empty_full_comb` and `MSEv_uniform_comb_we…
LHBO Nov 27, 2023
8a2584a
Added test for when MSEv criterion use the Shapley kernel weights
LHBO Nov 27, 2023
6b41197
Fixed bug in MSEv_plot function when specifying geom_col_width
LHBO Nov 27, 2023
ba37bf6
Fixed tests for MSEv plot function
LHBO Nov 27, 2023
4b94d0c
Added MSEv references
LHBO Nov 27, 2023
5a5164e
Added Lars as Author
LHBO Nov 27, 2023
46bbc80
Removed `MSEv_skip_empty_full_comb` form `explain` and all other func…
LHBO Nov 30, 2023
9852647
Fixed inncorrect standard deviations of the mean. Now divides by sqrt…
LHBO Nov 30, 2023
c669695
MSEv_plots: introduced approximate CI, reduced example, improved docu…
LHBO Dec 1, 2023
17a4edc
Updated MSEv_plot documentation
LHBO Dec 1, 2023
efcde43
Added example file for how to change the MSEv plots
LHBO Dec 1, 2023
b529f03
Inserted `level = 0.95` the places it where missing.
LHBO Dec 1, 2023
1ff362e
Added CI explanation in vignette.
LHBO Dec 1, 2023
bd0aa70
Fix linting and stylr.
LHBO Dec 1, 2023
e8f6477
Updated manuals (done autmatically by `devtools::run_examples()`)
LHBO Dec 1, 2023
b8d3caa
Added snaps plot svg files
LHBO Dec 1, 2023
55ced8b
Merge branch 'Lars/add_MSEv_eval_criterion' of github.com:LHBO/shapr …
martinju Dec 8, 2023
5e5729b
æ ø
martinju Dec 8, 2023
4cd6e44
update test files
martinju Dec 8, 2023
1075c65
del unused snaps
martinju Dec 8, 2023
d205664
typo
martinju Dec 8, 2023
da3e3b0
man
martinju Dec 8, 2023
1078991
styler
martinju Dec 8, 2023
cbd6ee9
bugfix name in testfile
martinju Dec 8, 2023
0c5c3c3
fixing check warning of missing ... description
martinju Dec 8, 2023
02f7467
fix check note on NSE
martinju Dec 8, 2023
70a6bee
Renamed `make_MSEv_eval_crit_plots` into `plot_MSEv_eval_crit`
LHBO Dec 8, 2023
2639fdf
Changed default confidence level to check for the number of explicands
LHBO Dec 8, 2023
bca66dd
Renamed the parameter `level` into `CI_level` in the `plot_MSEv_eval_…
LHBO Dec 8, 2023
fec0da7
Updated plot_MSEv_eval_crit.Rd and namespace
LHBO Dec 8, 2023
6fca8fc
Introduced `plot_type` parameter in `plot_MSEv_eval_crit()` and updat…
LHBO Dec 8, 2023
ca86e11
styler
LHBO Dec 8, 2023
6a391b2
Updated manual
LHBO Dec 8, 2023
b9728d7
Updated from ifelse to if else to support NULL return
LHBO Dec 8, 2023
cca8287
swap * + space with ~ in bquote to fix spacing in svg files
martinju Dec 11, 2023
cbe0baf
update svg files
martinju Dec 11, 2023
16cfdeb
man
martinju Dec 11, 2023
c70d947
bugfix vignette + remove unecessary plot arguments
martinju Dec 11, 2023
2f1ee6a
fix NSE check issues
martinju Dec 11, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .Rprofile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#' @param ... Additional arguments passed to [waldo::compare()]
#' Gives the relative path to the test files to review
#'
snapshot_review_man <- function(path, ...) {
snapshot_review_man <- function(path, tolerance = NULL, ...) {
changed <- testthat:::snapshot_meta(path)
these_rds <- (tools::file_ext(changed$name) == "rds")
if (any(these_rds)) {
Expand All @@ -16,7 +16,7 @@ snapshot_review_man <- function(path, ...) {
new <- readRDS(changed[i, "new"])

cat(paste0("Difference for check ", changed[i, "name"], " in test ", changed[i, "test"], "\n"))
print(waldo::compare(old, new, max_diffs = 50, ...))
print(waldo::compare(old, new, max_diffs = 50, tolerance = tolerance, ...))
browser()
}
}
Expand Down
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export(get_supported_approaches)
export(hat_matrix_cpp)
export(mahalanobis_distance_cpp)
export(observation_impute_cpp)
export(plot_MSEv_eval_crit)
export(predict_model)
export(prepare_data)
export(rss_cpp)
Expand Down Expand Up @@ -93,6 +94,9 @@ importFrom(stats,formula)
importFrom(stats,model.frame)
importFrom(stats,model.matrix)
importFrom(stats,predict)
importFrom(stats,pt)
importFrom(stats,qt)
importFrom(stats,sd)
importFrom(stats,setNames)
importFrom(utils,head)
importFrom(utils,methods)
Expand Down
11 changes: 10 additions & 1 deletion R/explain.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,14 @@
#' disabled for unsupported model classes.
#' Can also be used to override the default function for natively supported model classes.
#'
#' @param MSEv_uniform_comb_weights Logical. If `TRUE` (default), then the function weights the combinations
#' uniformly when computing the MSEv criterion. If `FALSE`, then the function use the Shapley kernel weights to
#' weight the combinations when computing the MSEv criterion. Note that the Shapley kernel weights are replaced by the
#' sampling frequency when not all combinations are considered.
#'
#' @param timing Logical.
#' Whether the timing of the different parts of the `explain()` should saved in the model object.
#' @param ... Further arguments passed to specific approaches
#'
#' @inheritDotParams setup_approach.empirical
#' @inheritDotParams setup_approach.independence
Expand Down Expand Up @@ -117,7 +123,8 @@
#' \describe{
#' \item{shapley_values}{data.table with the estimated Shapley values}
#' \item{internal}{List with the different parameters, data and functions used internally}
#' \item{pred_explain}{Numeric vector with the predictions for the explained observations.}
#' \item{pred_explain}{Numeric vector with the predictions for the explained observations}
#' \item{MSEv}{List with the values of the MSEv evaluation criterion for the approach.}
#' }
#'
#' `shapley_values` is a data.table where the number of rows equals
Expand Down Expand Up @@ -257,6 +264,7 @@ explain <- function(model,
keep_samp_for_vS = FALSE,
predict_model = NULL,
get_model_specs = NULL,
MSEv_uniform_comb_weights = TRUE,
timing = TRUE,
...) { # ... is further arguments passed to specific approaches

Expand Down Expand Up @@ -285,6 +293,7 @@ explain <- function(model,
seed = seed,
keep_samp_for_vS = keep_samp_for_vS,
feature_specs = feature_specs,
MSEv_uniform_comb_weights = MSEv_uniform_comb_weights,
timing = timing,
...
)
Expand Down
129 changes: 125 additions & 4 deletions R/finalize_explanation.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#' @export
finalize_explanation <- function(vS_list, internal) {
keep_samp_for_vS <- internal$parameters$keep_samp_for_vS
MSEv_uniform_comb_weights <- internal$parameters$MSEv_uniform_comb_weights

processed_vS_list <- postprocess_vS_list(
vS_list = vS_list,
Expand All @@ -24,20 +25,28 @@ finalize_explanation <- function(vS_list, internal) {

# internal$timing$shapley_computation <- Sys.time()


# Clearnig out the tmp list with model and predict_model (only added for AICc-types of empirical approach)
# Clearing out the tmp list with model and predict_model (only added for AICc-types of empirical approach)
internal$tmp <- NULL

internal$output <- processed_vS_list


output <- list(
shapley_values = dt_shapley,
internal = internal,
pred_explain = p
)
attr(output, "class") <- c("shapr", "list")

# Compute the MSEv evaluation criterion if the output of the predictive model is a scalar.
# TODO: check if it makes sense for output_size > 1.
if (internal$parameters$output_size == 1) {
output$MSEv <- compute_MSEv_eval_crit(
internal = internal,
dt_vS = processed_vS_list$dt_vS,
MSEv_uniform_comb_weights = MSEv_uniform_comb_weights
)
}

return(output)
}

Expand Down Expand Up @@ -104,7 +113,7 @@ get_p <- function(dt_vS, internal) {
#' Compute shapley values
#' @param explainer An `explain` object.
#' @param dt_vS The contribution matrix.
#' @return A `data.table` with shapley values for each test observation.
#' @return A `data.table` with Shapley values for each test observation.
#' @export
#' @keywords internal
compute_shapley_new <- function(internal, dt_vS) {
Expand Down Expand Up @@ -153,3 +162,115 @@ compute_shapley_new <- function(internal, dt_vS) {

return(dt_kshap)
}

#' Mean Squared Error of the Contribution Function `v(S)`
LHBO marked this conversation as resolved.
Show resolved Hide resolved
#'
#' @inheritParams explain
#' @inheritParams default_doc
#' @param dt_vS Data.table of dimension `n_combinations` times `n_explain + 1` containing the contribution function
#' estimates. The first column is assumed to be named `id_combination` and containing the ids of the combinations.
#' The last row is assumed to be the full combination, i.e., it contains the predicted responses for the observations
#' which are to be explained.
#' @param MSEv_skip_empty_full_comb Logical. If `TRUE` (default), we exclude the empty and grand
#' combinations/coalitions when computing the MSEv evaluation criterion. This is reasonable as they are identical
#' for all methods, i.e., their contribution function is independent of the used method as they are special cases not
#' effected by the used method. If `FALSE`, we include the empty and grand combinations/coalitions. In this situation,
#' we also recommend setting `MSEv_uniform_comb_weights = TRUE`, as otherwise the large weights for the empty and
#' grand combinations/coalitions will outweigh all other combinations and make the MSEv criterion uninformative.
#'
#' @return
#' List containing:
#' \describe{
#' \item{`MSEv`}{A \code{\link[data.table]{data.table}} with the overall MSEv evaluation criterion averaged
#' over both the combinations/coalitions and observations/explicands. The \code{\link[data.table]{data.table}}
#' also contains the standard deviation of the MSEv values for each explicand (only averaged over the combinations)
#' divided by the square root of the number of explicands.}
#' \item{`MSEv_explicand`}{A \code{\link[data.table]{data.table}} with the mean squared error for each
#' explicand, i.e., only averaged over the combinations/coalitions.}
#' \item{`MSEv_combination`}{A \code{\link[data.table]{data.table}} with the mean squared error for each
#' combination/coalition, i.e., only averaged over the explicands/observations.
#' The \code{\link[data.table]{data.table}} also contains the standard deviation of the MSEv values for
#' each combination divided by the square root of the number of explicands.}
#' }
#'
#' @description Function that computes the Mean Squared Error (MSEv) of the contribution function
#' v(s) as proposed by \href{https://arxiv.org/pdf/2006.01272.pdf}{Frye et al. (2019)} and used by
#' \href{https://www.jmlr.org/papers/volume23/21-1413/21-1413.pdf}{Olsen et al. (2022)}.
#'
#' @details
#' The MSEv evaluation criterion does not rely on access to the true contribution functions nor the
#' true Shapley values to be computed. A lower value indicates better approximations, however, the
#' scale and magnitude of the MSEv criterion is not directly interpretable in regard to the precision
#' of the final estimated Shapley values. \href{https://arxiv.org/pdf/2305.09536.pdf}{Olsen et al. (2022)}
#' illustrates in Figure 11 a fairly strong linear relationship between the MSEv criterion and the
#' MAE between the estimated and true Shapley values in a simulation study. Note that explicands
#' refer to the observations whose predictions we are to explain.
#'
#' @keywords internal
#' @author Lars Henry Berge Olsen
compute_MSEv_eval_crit <- function(internal,
dt_vS,
MSEv_uniform_comb_weights,
MSEv_skip_empty_full_comb = TRUE) {
n_explain <- internal$parameters$n_explain
n_combinations <- internal$parameters$n_combinations
id_combination_indices <- if (MSEv_skip_empty_full_comb) seq(2, n_combinations - 1) else seq(1, n_combinations)
n_combinations_used <- length(id_combination_indices)
features <- internal$objects$X$features[id_combination_indices]

# Extract the predicted responses f(x)
p <- unlist(dt_vS[id_combination == n_combinations, -"id_combination"])

# Create contribution matrix
vS <- as.matrix(dt_vS[id_combination_indices, -"id_combination"])

# Square the difference between the v(S) and f(x)
dt_squared_diff_original <- sweep(vS, 2, p)^2

# Get the weights
averaging_weights <- if (MSEv_uniform_comb_weights) rep(1, n_combinations) else internal$objects$X$shapley_weight
averaging_weights <- averaging_weights[id_combination_indices]
averaging_weights_scaled <- averaging_weights / sum(averaging_weights)

# Apply the `averaging_weights_scaled` to each column (i.e., each explicand)
dt_squared_diff <- dt_squared_diff_original * averaging_weights_scaled

# Compute the mean squared error for each observation, i.e., only averaged over the coalitions.
# We take the sum as the weights sum to 1, so denominator is 1.
MSEv_explicand <- colSums(dt_squared_diff)

# The MSEv criterion for each coalition, i.e., only averaged over the explicands.
MSEv_combination <- rowMeans(dt_squared_diff * n_combinations_used)
MSEv_combination_sd <- apply(dt_squared_diff * n_combinations_used, 1, sd) / sqrt(n_explain)

# The MSEv criterion averaged over both the coalitions and explicands.
MSEv <- mean(MSEv_explicand)
MSEv_sd <- sd(MSEv_explicand) / sqrt(n_explain)

# Set the name entries in the arrays
names(MSEv_explicand) <- paste0("id_", seq(n_explain))
names(MSEv_combination) <- paste0("id_combination_", id_combination_indices)
names(MSEv_combination_sd) <- paste0("id_combination_", id_combination_indices)

# Convert the results to data.table
MSEv <- data.table(
"MSEv" = MSEv,
"MSEv_sd" = MSEv_sd
)
MSEv_explicand <- data.table(
"id" = seq(n_explain),
"MSEv" = MSEv_explicand
)
MSEv_combination <- data.table(
"id_combination" = id_combination_indices,
"features" = features,
"MSEv" = MSEv_combination,
"MSEv_sd" = MSEv_combination_sd
)

return(list(
MSEv = MSEv,
MSEv_explicand = MSEv_explicand,
MSEv_combination = MSEv_combination
))
}
Loading
Loading