Skip to content

Commit

Permalink
Renaming argument prediction_zero to phi0 (#413)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinju authored Oct 22, 2024
1 parent 03a4206 commit e2caa4a
Show file tree
Hide file tree
Showing 203 changed files with 1,095 additions and 1,094 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ S3method(setup_approach,timeseries)
S3method(setup_approach,vaeac)
export(additional_regression_setup)
export(aicc_full_single_cpp)
export(append_vS_list)
export(check_convergence)
export(cli_compute_vS)
export(cli_iter)
Expand Down
6 changes: 3 additions & 3 deletions R/approach_vaeac.R
Original file line number Diff line number Diff line change
Expand Up @@ -2531,7 +2531,7 @@ Last epoch: %d. \tVLB = %.3f \tIWAE = %.3f \tIWAE_running = %.3f\n",
#' x_explain = x_explain,
#' x_train = x_train,
#' approach = approach,
#' prediction_zero = p0,
#' phi0 = p0,
#' n_MC_samples = 1, # As we are only interested in the training of the vaeac
#' vaeac.epochs = 10, # Should be higher in applications.
#' vaeac.n_vaeacs_initialize = 1,
Expand All @@ -2545,7 +2545,7 @@ Last epoch: %d. \tVLB = %.3f \tIWAE = %.3f \tIWAE_running = %.3f\n",
#' x_explain = x_explain,
#' x_train = x_train,
#' approach = approach,
#' prediction_zero = p0,
#' phi0 = p0,
#' n_MC_samples = 1, # As we are only interested in the training of the vaeac
#' vaeac.epochs = 10, # Should be higher in applications.
#' vaeac.width = 16,
Expand Down Expand Up @@ -2766,7 +2766,7 @@ vaeac_plot_eval_crit <- function(explanation_list,
#' x_explain = x_explain,
#' x_train = x_train,
#' approach = "vaeac",
#' prediction_zero = mean(y_train),
#' phi0 = mean(y_train),
#' n_MC_samples = 1,
#' vaeac.epochs = 10,
#' vaeac.n_vaeacs_initialize = 1
Expand Down
1 change: 0 additions & 1 deletion R/cli.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ cli_startup <- function(internal, model_class, verbose) {
#' @export
#' @keywords internal
cli_compute_vS <- function(internal) {

verbose <- internal$parameters$verbose
approach <- internal$parameters$approach

Expand Down
4 changes: 2 additions & 2 deletions R/compute_estimates.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ compute_estimates <- function(internal, vS_list) {
#' @keywords internal
postprocess_vS_list <- function(vS_list, internal) {
keep_samp_for_vS <- internal$parameters$output_args$keep_samp_for_vS
prediction_zero <- internal$parameters$prediction_zero
phi0 <- internal$parameters$phi0
n_explain <- internal$parameters$n_explain

# Appending the zero-prediction to the list
dt_vS0 <- as.data.table(rbind(c(1, rep(prediction_zero, n_explain))))
dt_vS0 <- as.data.table(rbind(c(1, rep(phi0, n_explain))))

# Extracting/merging the data tables from the batch running
# TODO: Need a memory and speed optimized way to transform the output form dt_vS_list to two different lists,
Expand Down
12 changes: 5 additions & 7 deletions R/compute_vS.R
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,6 @@ compute_MCint <- function(dt, pred_cols = "p_hat") {
#' @export
#' @keywords internal
append_vS_list <- function(vS_list, internal) {

iter <- length(internal$iter_list)

# Adds v_S output above to any vS_list already computed
Expand All @@ -243,17 +242,17 @@ append_vS_list <- function(vS_list, internal) {

# Creates a mapper from the last id_coalition to the new id_coalition numbering
id_coalitions_mapper <- merge(prev_coalition_map,
current_coalition_map,
by = "coalitions_str",
suffixes = c("", "_new")
current_coalition_map,
by = "coalitions_str",
suffixes = c("", "_new")
)
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"
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]
Expand All @@ -263,5 +262,4 @@ append_vS_list <- function(vS_list, internal) {
vS_list <- c(prev_vS_list_new, vS_list)
}
return(vS_list)

}
26 changes: 13 additions & 13 deletions R/explain.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#' `"categorical"`, `"timeseries"`, `"independence"`, `"regression_separate"`, or `"regression_surrogate"`.
#' The two regression approaches can not be combined with any other approach. See details for more information.
#'
#' @param prediction_zero Numeric.
#' @param phi0 Numeric.
#' The prediction value for unseen data, i.e. an estimate of the expected prediction without conditioning on any
#' features.
#' Typically we set this value equal to the mean of the response variable in our training data, but other choices
Expand Down Expand Up @@ -211,7 +211,7 @@
#' \describe{
#' \item{shapley_values_est}{data.table with the estimated Shapley values with explained observation in the rows and
#' features along the columns.
#' The column `none` is the prediction not devoted to any of the features (given by the argument `prediction_zero`)}
#' The column `none` is the prediction not devoted to any of the features (given by the argument `phi0`)}
#' \item{shapley_values_sd}{data.table with the standard deviation of the Shapley values reflecting the uncertainty.
#' Note that this only reflects the coalition sampling part of the kernelSHAP procedure, and is therefore by
#' definition 0 when all coalitions is used.
Expand Down Expand Up @@ -269,7 +269,7 @@
#' x_explain = x_explain,
#' x_train = x_train,
#' approach = "empirical",
#' prediction_zero = p,
#' phi0 = p,
#' n_MC_samples = 1e2
#' )
#'
Expand All @@ -279,7 +279,7 @@
#' x_explain = x_explain,
#' x_train = x_train,
#' approach = "gaussian",
#' prediction_zero = p,
#' phi0 = p,
#' n_MC_samples = 1e2
#' )
#'
Expand All @@ -289,7 +289,7 @@
#' x_explain = x_explain,
#' x_train = x_train,
#' approach = "copula",
#' prediction_zero = p,
#' phi0 = p,
#' n_MC_samples = 1e2
#' )
#'
Expand All @@ -299,7 +299,7 @@
#' x_explain = x_explain,
#' x_train = x_train,
#' approach = "ctree",
#' prediction_zero = p,
#' phi0 = p,
#' n_MC_samples = 1e2
#' )
#'
Expand All @@ -310,7 +310,7 @@
#' x_explain = x_explain,
#' x_train = x_train,
#' approach = approach,
#' prediction_zero = p,
#' phi0 = p,
#' n_MC_samples = 1e2
#' )
#'
Expand All @@ -332,7 +332,7 @@
#' x_train = x_train,
#' group = group_list,
#' approach = "empirical",
#' prediction_zero = p,
#' phi0 = p,
#' n_MC_samples = 1e2
#' )
#' print(explain_groups$shapley_values_est)
Expand All @@ -347,7 +347,7 @@
#' model = model,
#' x_explain = x_explain,
#' x_train = x_train,
#' prediction_zero = p,
#' phi0 = p,
#' approach = "regression_separate",
#' regression.model = parsnip::linear_reg()
#' )
Expand All @@ -356,7 +356,7 @@
#' model = model,
#' x_explain = x_explain,
#' x_train = x_train,
#' prediction_zero = p,
#' phi0 = p,
#' approach = "regression_surrogate",
#' regression.model = parsnip::linear_reg()
#' )
Expand All @@ -370,7 +370,7 @@
#' x_explain = x_explain,
#' x_train = x_train,
#' approach = "gaussian",
#' prediction_zero = p,
#' phi0 = p,
#' n_MC_samples = 1e2,
#' iterative = TRUE,
#' iterative_args = list(initial_n_coalitions = 10)
Expand All @@ -395,7 +395,7 @@ explain <- function(model,
x_explain,
x_train,
approach,
prediction_zero,
phi0,
iterative = NULL,
max_n_coalitions = NULL,
group = NULL,
Expand Down Expand Up @@ -433,7 +433,7 @@ explain <- function(model,
x_explain = x_explain,
approach = approach,
paired_shap_sampling = paired_shap_sampling,
prediction_zero = prediction_zero,
phi0 = phi0,
max_n_coalitions = max_n_coalitions,
group = group,
n_MC_samples = n_MC_samples,
Expand Down
6 changes: 3 additions & 3 deletions R/explain_forecast.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
#' explain_y_lags = 2,
#' horizon = 3,
#' approach = "empirical",
#' prediction_zero = p0_ar,
#' phi0 = p0_ar,
#' group_lags = FALSE
#' )
#'
Expand All @@ -93,7 +93,7 @@ explain_forecast <- function(model,
explain_xreg_lags = explain_y_lags,
horizon,
approach,
prediction_zero,
phi0,
max_n_coalitions = NULL,
iterative = NULL,
iterative_args = list(),
Expand Down Expand Up @@ -125,7 +125,7 @@ explain_forecast <- function(model,
# Checks data/model compatability
internal <- setup(
approach = approach,
prediction_zero = prediction_zero,
phi0 = phi0,
output_size = horizon,
max_n_coalitions = max_n_coalitions,
n_MC_samples = n_MC_samples,
Expand Down
30 changes: 15 additions & 15 deletions R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
#' x_explain = x_explain,
#' x_train = x_train,
#' approach = "empirical",
#' prediction_zero = p,
#' phi0 = p,
#' n_MC_samples = 1e2
#' )
#'
Expand Down Expand Up @@ -151,7 +151,7 @@
#' x_explain = x_explain,
#' x_train = x_train,
#' approach = "ctree",
#' prediction_zero = p,
#' phi0 = p,
#' n_MC_samples = 1e2
#' )
#'
Expand Down Expand Up @@ -299,7 +299,7 @@ plot.shapr <- function(x,
# compute start and end values for waterfall rectangles
data.table::setorder(dt_plot, rank_waterfall)
dt_plot[, end := cumsum(phi), by = id]
expected <- x$internal$parameters$prediction_zero
expected <- x$internal$parameters$phi0
dt_plot[, start := c(expected, head(end, -1)), by = id]
dt_plot[, phi_significant := format(phi, digits = digits), by = id]

Expand Down Expand Up @@ -895,15 +895,15 @@ make_waterfall_plot <- function(dt_plot,
#' )
#'
#' # Specifying the phi_0, i.e. the expected prediction without any features
#' prediction_zero <- mean(y_train)
#' phi0 <- mean(y_train)
#'
#' # Independence approach
#' explanation_independence <- explain(
#' model = model,
#' x_explain = x_explain,
#' x_train = x_train,
#' approach = "independence",
#' prediction_zero = prediction_zero,
#' phi0 = phi0,
#' n_MC_samples = 1e2
#' )
#'
Expand All @@ -913,7 +913,7 @@ make_waterfall_plot <- function(dt_plot,
#' x_explain = x_explain,
#' x_train = x_train,
#' approach = "gaussian",
#' prediction_zero = prediction_zero,
#' phi0 = phi0,
#' n_MC_samples = 1e1
#' )
#'
Expand All @@ -923,7 +923,7 @@ make_waterfall_plot <- function(dt_plot,
#' x_explain = x_explain,
#' x_train = x_train,
#' approach = "gaussian",
#' prediction_zero = prediction_zero,
#' phi0 = phi0,
#' n_MC_samples = 1e2
#' )
#'
Expand All @@ -933,7 +933,7 @@ make_waterfall_plot <- function(dt_plot,
#' x_explain = x_explain,
#' x_train = x_train,
#' approach = "ctree",
#' prediction_zero = prediction_zero,
#' phi0 = phi0,
#' n_MC_samples = 1e2
#' )
#'
Expand All @@ -943,7 +943,7 @@ make_waterfall_plot <- function(dt_plot,
#' x_explain = x_explain,
#' x_train = x_train,
#' approach = c("gaussian", "independence", "ctree"),
#' prediction_zero = prediction_zero,
#' phi0 = phi0,
#' n_MC_samples = 1e2
#' )
#'
Expand Down Expand Up @@ -1452,15 +1452,15 @@ make_MSEv_coalition_plots <- function(MSEv_coalition_dt,
#' )
#'
#' # Specifying the phi_0, i.e. the expected prediction without any features
#' prediction_zero <- mean(y_train)
#' phi0 <- mean(y_train)
#'
#' # Independence approach
#' explanation_independence <- explain(
#' model = model,
#' x_explain = x_explain,
#' x_train = x_train,
#' approach = "independence",
#' prediction_zero = prediction_zero,
#' phi0 = phi0,
#' n_MC_samples = 1e2
#' )
#'
Expand All @@ -1470,7 +1470,7 @@ make_MSEv_coalition_plots <- function(MSEv_coalition_dt,
#' x_explain = x_explain,
#' x_train = x_train,
#' approach = "empirical",
#' prediction_zero = prediction_zero,
#' phi0 = phi0,
#' n_MC_samples = 1e2
#' )
#'
Expand All @@ -1480,7 +1480,7 @@ make_MSEv_coalition_plots <- function(MSEv_coalition_dt,
#' x_explain = x_explain,
#' x_train = x_train,
#' approach = "gaussian",
#' prediction_zero = prediction_zero,
#' phi0 = phi0,
#' n_MC_samples = 1e1
#' )
#'
Expand All @@ -1490,7 +1490,7 @@ make_MSEv_coalition_plots <- function(MSEv_coalition_dt,
#' x_explain = x_explain,
#' x_train = x_train,
#' approach = "gaussian",
#' prediction_zero = prediction_zero,
#' phi0 = phi0,
#' n_MC_samples = 1e2
#' )
#'
Expand All @@ -1500,7 +1500,7 @@ make_MSEv_coalition_plots <- function(MSEv_coalition_dt,
#' x_explain = x_explain,
#' x_train = x_train,
#' approach = c("gaussian", "ctree", "empirical"),
#' prediction_zero = prediction_zero,
#' phi0 = phi0,
#' n_MC_samples = 1e2
#' )
#'
Expand Down
Loading

0 comments on commit e2caa4a

Please sign in to comment.