Skip to content

Commit

Permalink
Imputation options switched to na.rm, na.keep and na.impute
Browse files Browse the repository at this point in the history
  • Loading branch information
fouodo committed Nov 27, 2024
1 parent 74df94e commit d4bb460
Show file tree
Hide file tree
Showing 18 changed files with 175 additions and 151 deletions.
37 changes: 30 additions & 7 deletions R/Lrner.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ Lrner <- R6Class("Lrner",
#' Learn parameters.
#' @param train_layer (`TrainLayer(1)`) \cr
#' Layer on which the learner is stored.
#' @param na_rm (`logical(1)`) \cr
#' If \code{TRUE}, the individuals with missing predictor values will be removed from the training dataset.
#' @param na_action `character(1)`\cr
#' Handling of missing values. Set to "na.keep" to keep missing values, "na.rm" to remove individuals with missing values or "na.impute" (only applicable on meta-data) to impute missing values in meta-data. Only median and mode based imputations are actually handled. With the "na.keep" option, ensure that the provided learner can handle missing values.
initialize = function (id,
package = NULL,
lrn_fct,
param_train_list,
param_pred_list = list(),
train_layer,
na_rm = TRUE) {
na_action = "na.rm") {
private$id = id
private$package = package
private$lrn_fct = lrn_fct
Expand All @@ -50,11 +50,34 @@ Lrner <- R6Class("Lrner",
if (!any(c("TrainLayer", "TrainMetaLayer") %in% class(train_layer))) {
stop("A Lrner can only belong to a TrainLayer or a TrainMetaLayer object.")
}
if (!is.logical(na_rm)) {
stop("na.rm must be a logical value\n")
# if (!is.logical(na_rm)) {
# stop("na.rm must be a logical value\n")
# } else {
# private$na_rm = na_rm
# }
impute = FALSE
if (na_action == "na.keep") {
na_rm = FALSE
} else {
private$na_rm = na_rm
if (na_action == "na.rm") {
na_rm = TRUE
} else {
if (na_action == "na.impute") {
if ("TrainLayer" %in% class(train_layer)) {
stop("Imputation is not yet handled for data modalities. Please use either the 'na.keep' or the 'na.rm' option.")
}
na_rm = FALSE
impute = TRUE
# Imputation takes place in the Training class, since it
# happens after meta-data have been generated.
train_layer$getTraining()$setImpute(impute = impute)
} else {
stop("na_action must be one of 'na.fails', 'na.rm' or 'na.impute'.")
}
}
}
private$na_rm = na_rm
# Instantiate a Lrner object
# Remove learner if already existing
if (train_layer$checkLrnerExist()) {
key_class = train_layer$getKeyClass()
Expand Down Expand Up @@ -135,7 +158,7 @@ Lrner <- R6Class("Lrner",
}
}
param_interface = data.frame(standard = c("x_name", "y_name", "object_name", "data_name"),
original = c(x, y, object, data))
original = c(x, y, object, data))
private$param_interface = param_interface
private$extract_pred_fct = extract_pred_fct
},
Expand Down
1 change: 0 additions & 1 deletion R/TrainMetaLayer.R
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ TrainMetaLayer <- R6Class("TrainMetaLayer",
#' The list of parameters to call the imputation function. Not yet implemented!
#' @return
#' A new object with the predicted values is returned.
#' @export
#'
impute = function (impute_fct = NULL,
impute_param = NULL) {
Expand Down
26 changes: 21 additions & 5 deletions R/VarSel.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ VarSel <- R6Class("VarSel",
#' Layer on which the learner is stored.
#' @param train_layer (`TrainLayer(1)`) \cr
#' The training layer where to store the learner.
#' @param na_rm (`logical(1)`) \cr
#' @param na_action `character(1)`\cr
#' Handling of missing values in meta-data. Set to "na.keep" to keep missing values, "na.rm" to remove individuals with missing values or "na.impute" (only applicable on meta-data) to impute missing values in meta-data. Only median and mode based imputations are actually handled. With the "na.keep" option, ensure that the provided learner can handle missing values.
#' If \code{TRUE}, the individuals with missing predictor values will be removed from the training dataset.
initialize = function (id,
package = NULL,
varsel_fct,
varsel_param,
train_layer,
na_rm = TRUE) {
na_action = "na.rm") {
private$id = id
private$package = package
private$varsel_fct = varsel_fct
Expand All @@ -46,11 +47,26 @@ VarSel <- R6Class("VarSel",
if (!any(c("TrainLayer") %in% class(train_layer))) {
stop("A variable selection tool can only belong to object of class TrainLayer.")
}
if (!is.logical(na_rm)) {
stop("na.rm must be a logical value\n")
# if (!is.logical(na_rm)) {
# stop("na.rm must be a logical value\n")
# } else {
# private$na_rm = na_rm
# }

if (na_action == "na.keep") {
na_rm = FALSE
} else {
private$na_rm = na_rm
if (na_action == "na.rm") {
na_rm = TRUE
} else {
if (na_action == "na.impute") {
stop("Imputation is not yet handled for data modalities. Please use either the 'na.keep' or the 'na.rm' option.")
} else {
stop("na_action must be one of 'na.fails' or 'na.rm'.")
}
}
}
private$na_rm = na_rm
# Remove VarSel if already existing
if (train_layer$checkVarSelExist()) {
key_class = train_layer$getKeyClass()
Expand Down
Loading

0 comments on commit d4bb460

Please sign in to comment.