Skip to content

Commit

Permalink
Remove Param class
Browse files Browse the repository at this point in the history
  • Loading branch information
fouodo committed Nov 5, 2024
1 parent 2e56455 commit e1d6c20
Show file tree
Hide file tree
Showing 42 changed files with 205 additions and 578 deletions.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
^README.Rmd
^README.files
^\.covrignore
^doc$
^Meta$
3 changes: 0 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ Collate:
'HashTable.R'
'Lrner.R'
'Model.R'
'Param.R'
'ParamLrner.R'
'ParamVarSel.R'
'PredictData.R'
'PredictLayer.R'
'PredictMetaLayer.R'
Expand Down
3 changes: 0 additions & 3 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ export(Data)
export(HashTable)
export(Lrner)
export(Model)
export(Param)
export(ParamLrner)
export(ParamVarSel)
export(PredictData)
export(PredictLayer)
export(PredictMetaLayer)
Expand Down
6 changes: 3 additions & 3 deletions R/Model.R
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,15 @@ Model <- R6Class("Model",
#' @param ind_subset `vector(1)` \cr
#' Subset of individual IDs to be predicted.
#' @param ...
#' Further parameters.
#' Further parameters to be passed to the basic predict function.
#'
#' @return
#' The predicted object are returned. The predicted object must be either a vector or a list
#' containing a field predictions with predictions.
#'
#' @export
#'
predict = function (testing_data, ind_subset = NULL, ...) {
predict = function (testing_data, ind_subset = NULL) {
tmp_lrner = self$getLrner()
if(tmp_lrner$getTrainLayer()$getId() != testing_data$getTestLayer()$getId()) {
stop("Learner and data must belong to the same layer.")
Expand Down Expand Up @@ -165,7 +165,7 @@ Model <- R6Class("Model",
var_name = testing_data$getIndCol(),
value = ind_subset)
}
pred_param <- list(...)
pred_param <- private$lrner$getParamPred()
pred_param$object = self$getBaseModel()
# Predict using the subset of variables utilized for training
training_var = colnames(private$train_data$getData())
Expand Down
3 changes: 1 addition & 2 deletions R/PredictData.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ PredictData <- R6Class("PredictData",
#' @export
#'
getPredictData = function () {
tmp_data <- private$data_frame
return(tmp_data)
return(private$data_frame)
},
#' @description
#' Getter of the current layer.
Expand Down
1 change: 0 additions & 1 deletion R/PredictMetaLayer.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ PredictMetaLayer <- R6Class("PredictMetaLayer",
#' constructor
#'
#' @param id (`character(1)`)\cr
#' See class Param
#' @param predicting (`Predicting(1)`)\cr
#'
initialize = function (id, predicting) {
Expand Down
15 changes: 6 additions & 9 deletions R/Predicting.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#' @title Predicting Class
#'
#' @description
#' This class is the basic class of the present package. An object from this class
#' is designed to contain multiple layers, but only one meta layer.
#' This class is designed for predictions.
#'
#' The Predicting is structured as followed:
#' * [PredictLayer]: Can be clinical, gene expression, etc.
#' - [PredictData]: Specific to each layer, it must be set up by the user.
#' * [PredictMetaLayer]: Basically a [PredictLayer], but with some specific properties.
#' * [PredictLayer]: Exists for each modality.
#' - [PredictData]: Related class for modality-specific predictions.
#' * [PredictMetaLayer]: Related class for meta predictions.
#' - [PredictData]: Specific to the meta layer, it is set up internally after cross-validation.
#'
#' Use the function \code{train} for training and \code{predict} for predicting.
Expand All @@ -25,7 +24,7 @@ Predicting <- R6Class("Predicting",
#' constructor
#'
#' @param id (`character(1)`)\cr
#' See class Param
#' Predicting id.
#' @param ind_col (`character(1L)`)
#' Name of column of individuals IDS
initialize = function (id, ind_col) {
Expand Down Expand Up @@ -55,15 +54,13 @@ Predicting <- R6Class("Predicting",
key_class_predicting = self$getKeyClass()
predicted_values = NULL
for (k in key_class_predicting[ , "key"]) {
# FIXME: Maybe define a class Prediction instead of
# using Hashtable?
pred_layer = self$getFromHashTable(key = k)
pred_data = pred_layer$getPredictData()
pred_values = pred_data$getPredictData()
predicted_values = data.frame(rbind(predicted_values,
pred_values))
}
# Will transform meta data.frame into wide format
# Will transform meta data.frame into wide format. In case of data.frame, only the first column is considered.
predicted_values_wide = reshape(predicted_values,
idvar = colnames(predicted_values)[2L],
timevar = colnames(predicted_values)[1L],
Expand Down
2 changes: 1 addition & 1 deletion R/Target.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#' @export
#'
#' @importFrom R6 R6Class
#' @seealso [TrainLayer], [Lrner], [Model], [ParamLrner], [TestData]
#' @seealso [TrainLayer], [Lrner], [Model], [TestData]
Target <- R6Class("Target",
inherit = Data,
public = list(
Expand Down
2 changes: 1 addition & 1 deletion R/TestLayer.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ TestLayer <- R6Class("TestLayer",
#' constructor
#'
#' @param id (`character(1)`)\cr
#' See class Param
#' Testing layer id.
#' @param testing (`Testing(1)`)\cr
#'
initialize = function (id, testing) {
Expand Down
2 changes: 1 addition & 1 deletion R/TestMetaLayer.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ TestMetaLayer <- R6Class("TestMetaLayer",
#' constructor
#'
#' @param id (`character(1)`)\cr
#' See class Param
#' Testing meta-layer id.
#' @param testing (`Testing(1)`)\cr
#'
initialize = function (id, testing) {
Expand Down
2 changes: 1 addition & 1 deletion R/Testing.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Testing <- R6Class("Testing",
#' constructor
#'
#' @param id (`character(1)`)\cr
#' See class Param
#' Testing id.
#' @param ind_col (`character(1)`)
#' Name of column of individuals IDS
initialize = function (id, ind_col) {
Expand Down
2 changes: 1 addition & 1 deletion R/TrainData.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#' @export
#'
#' @importFrom R6 R6Class
#' @seealso [TrainLayer], [Lrner], [Model], [ParamLrner], [TestData]
#' @seealso [TrainLayer], [Lrner], [Model], [TestData]
TrainData <- R6Class("TrainData",
inherit = Data,
public = list(
Expand Down
2 changes: 1 addition & 1 deletion R/TrainLayer.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ TrainLayer <- R6Class("TrainLayer",
#' constructor
#'
#' @param id (`character(1)`)\cr
#' See class Param
#' Training layer id.
#' @param training (`Training(1)`)\cr
#'
initialize = function (id, training) {
Expand Down
2 changes: 1 addition & 1 deletion R/TrainMetaLayer.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ TrainMetaLayer <- R6Class("TrainMetaLayer",
#' constructor
#'
#' @param id (`character(1)`)\cr
#' See class Param
#' Id of training meta-layer.
#' @param training (`Training(1)`)\cr
#'
initialize = function (id, training) {
Expand Down
2 changes: 1 addition & 1 deletion R/Training.R
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ Training <- R6Class("Training",
#' The predicted object. All layers and the meta layer are predicted. This is the final predicted object.
#' @export
#'
# TODO: Mention that our predictions based on cross-validation are different from that coming from the original learning method; e.g. that coming from ranger.
# Our predictions based on cross-validation are different from that coming from the original learning method; e.g. that coming from ranger.
predict = function (testing,
ind_subset = NULL) {
# 0) Check consistency between training and testing layers
Expand Down
10 changes: 5 additions & 5 deletions R/VarSel.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ VarSel <- R6Class("VarSel",
#' Variable selection function name. Note: Variable selection functions, except \code{Boruta}, must return a vector of selected variables.
#' @param varsel_fct (`character(1)`) \cr
#' Variable selection parameters.
#' @param param (`ParamVarSel(1)`) \cr
#' @param varsel_param (`list(1)`) \cr
#' Layer on which the learner is stored.
#' @param train_layer (`TrainLayer(1)`) \cr
#' The training layer where to store the learner.
initialize = function (id,
package = NULL,
varsel_fct,
param,
varsel_param,
train_layer) {
private$id = id
private$package = package
private$varsel_fct = varsel_fct
private$param = param
private$param = varsel_param
if (!any(c("TrainLayer") %in% class(train_layer))) {
stop("A variable selection tool can only belong to object of class TrainLayer.")
}
Expand Down Expand Up @@ -92,7 +92,7 @@ VarSel <- R6Class("VarSel",
varsel = sprintf('%s::%s', private$package,
private$varsel_fct)
}
varsel_param = private$param$getParamVarSel()
varsel_param = private$param
# Prepare training dataset
if (!is.null(ind_subset)) {
train_data = train_data$getIndSubset(
Expand Down Expand Up @@ -188,7 +188,7 @@ VarSel <- R6Class("VarSel",
package = NULL,
# Learn function name (like \code{ranger}).
varsel_fct = NULL,
# Parameters (from class [Param]) of the learn function.
# Parameters of the variable selection function.
param = NULL,
# Training layer (from class [TainLayer] or [TrainMetaLayer]) of the current learner.
train_layer = NULL,
Expand Down
47 changes: 18 additions & 29 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -124,34 +124,30 @@ training$upset(order.by = "freq")

We need to set up variable selection methods to our training resources. Note that this can be the same method or different layer-specific methods. For simplicity, we will set up the same method on all layers.

- Preparation parameters of the variable selection method.

```{r varsel_param, include=TRUE, eval=TRUE}
same_param_varsel <- ParamVarSel$new(id = "ParamVarSel",
param_list = list(num.trees = 1000L,
mtry = 3L,
probability = TRUE))
print(same_param_varsel)
```

- Instantiate the variable selection method and assign training layers.

```{r varsel_object, include=TRUE, eval=TRUE}
varsel_ge <- VarSel$new(id = "varsel_geneexpr",
package = "Boruta",
varsel_fct = "Boruta",
param = same_param_varsel,
varsel_param = list(num.trees = 1000L,
mtry = 3L,
probability = TRUE),
train_layer = tl_ge)
varsel_pr <- VarSel$new(id = "varsel_proteinexpr",
package = "Boruta",
varsel_fct = "Boruta",
param = same_param_varsel,
varsel_param = list(num.trees = 1000L,
mtry = 3L,
probability = TRUE),
train_layer = tl_pr)
varsel_me <- VarSel$new(id = "varsel_methylation",
package = "Boruta",
varsel_fct = "Boruta",
param = same_param_varsel,
varsel_param = list(num.trees = 1000L,
mtry = 3L,
probability = TRUE),
train_layer = tl_me)
```

Expand All @@ -169,38 +165,30 @@ For each layer, the variable selection results show the chosen variables. In thi

We can now train our models using the subset of selected variables. Users can choose to set up layer-specific learners, but for illustration, we will use the same learner for all layers.

- Set up the same leaner parameters.

```{r lrner_param, include=TRUE, eval=TRUE}
same_param <- ParamLrner$new(id = "ParamRanger",
param_list = list(probability = TRUE,
mtry = 1L),
hyperparam_list = list(num.trees = 1000L))
```

- Set up learners for each layer. We will use a weighted sum, implemented internally by `fuseMLR`, for the meta-analysis.

```{r lrner, include=TRUE, eval=TRUE}
lrner_ge <- Lrner$new(id = "ranger",
package = "ranger",
lrn_fct = "ranger",
param = same_param,
param_train_list = list(probability = TRUE,
mtry = 1L),
train_layer = tl_ge)
lrner_pr <- Lrner$new(id = "ranger",
package = "ranger",
lrn_fct = "ranger",
param = same_param,
param_train_list = list(probability = TRUE,
mtry = 1L),
train_layer = tl_pr)
lrner_me <- Lrner$new(id = "ranger",
package = "ranger",
lrn_fct = "ranger",
param = same_param,
param_train_list = list(probability = TRUE,
mtry = 1L),
train_layer = tl_me)
lrner_meta <- Lrner$new(id = "weighted",
lrn_fct = "weightedMeanLearner",
param = ParamLrner$new(id = "ParamWeighted",
param_list = list(),
hyperparam_list = list()),
param_train_list = list(),
na_rm = FALSE,
train_layer = tl_meta)
```
Expand All @@ -215,7 +203,8 @@ disease <- training$getTargetValues()$disease
trained <- training$train(resampling_method = "caret::createFolds",
resampling_arg = list(y = disease,
k = 10L),
use_var_sel = TRUE)
use_var_sel = TRUE,
verbose = FALSE)
# Let us now check the status of our training resources.
print(trained)
# Let us check the status of a layer as well.
Expand Down
Loading

0 comments on commit e1d6c20

Please sign in to comment.