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

preserve task attributes when creating reduced_task by modifying data using mlr3pipelines #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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 DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ Description: A general test for conditional independence in supervised learning
License: GPL (>= 3)
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.1.2
RoxygenNote: 7.2.2
URL: https://github.com/bips-hb/cpi,
https://bips-hb.github.io/cpi/
BugReports: https://github.com/bips-hb/cpi/issues
Imports: foreach, mlr3, lgr, knockoff
Imports: foreach, mlr3, lgr, knockoff, mlr3pipelines
Suggests:
BEST,
mlr3learners,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ import(mlr3)
import(stats)
importFrom(knockoff,create.second_order)
importFrom(lgr,get_logger)
importFrom(mlr3pipelines,PipeOpMutate)
28 changes: 17 additions & 11 deletions R/cpi.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#' @import stats mlr3 foreach
#' @importFrom knockoff create.second_order
#' @importFrom lgr get_logger
#' @importFrom mlr3pipelines PipeOpMutate
#'
#' @details
#' This function computes the conditional predictive impact (CPI) of one or
Expand Down Expand Up @@ -243,20 +244,25 @@ cpi <- function(task, learner,
} else {
stop("Argument 'x_tilde' must be a matrix, data.frame or NULL.")
}

# For each feature, fit reduced model and return difference in error
cpi_fun <- function(i) {
if (is.null(test_data)) {
reduced_test_data <- NULL
reduced_data <- as.data.frame(task$data())
reduced_data[, task$feature_names[i]] <- x_tilde[, task$feature_names[i]]
if (task$task_type == "regr") {
reduced_task <- as_task_regr(reduced_data, target = task$target_names)
} else if (task$task_type == "classif") {
reduced_task <- as_task_classif(reduced_data, target = task$target_names)
} else {
stop("Unknown task type.")
}

mutation <-
lapply(X = i, FUN = function(k) {
return(as.formula(paste0("~ x_tilde[, '", task$feature_names[k], "']")))
})

names(mutation) <- task$feature_names[i]

pom <-
mlr3pipelines::PipeOpMutate$new(
param_vals = list(mutation = mutation)
)

reduced_task <- pom$train(list(task))$output
} else {
reduced_task <- NULL
reduced_test_data <- test_data
Expand All @@ -273,7 +279,7 @@ cpi <- function(task, learner,
}
cpi <- mean(dif)
se <- sd(dif) / sqrt(length(dif))

if (is.null(groups)) {
res <- data.frame(Variable = task$feature_names[i],
CPI = unname(cpi),
Expand Down