trans_classifier: Create 'trans_classifier' object for machine-learning-based...

trans_classifierR Documentation

Create trans_classifier object for machine-learning-based model prediction.

Description

This class is a wrapper for methods of machine-learning-based classification or regression models, including data pre-processing, feature selection, data split, model training, prediction, confusionMatrix and ROC (Receiver Operator Characteristic) or PR (Precision-Recall) curve.

Author(s): Felipe Mansoldo and Chi Liu

Methods

Public methods


Method new()

Create a trans_classifier object.

Usage
trans_classifier$new(
  dataset,
  x.predictors = "Genus",
  y.response = NULL,
  n.cores = 1
)
Arguments
dataset

an object of microtable class.

x.predictors

default "Genus"; character vector or data.frame; a character vector represents selecting the corresponding data from microtable$taxa_abund; data.frame denotes other customized input. See the following available options:

'Genus'

use Genus level table in microtable$taxa_abund, or other specific taxonomic rank, e.g., 'Phylum'. At most one input level (e.g., ASV) is allowed to be absent in the names of taxa_abund list; in that case the function will use otu_table to calculate relative abundance of features.

'all'

use all the levels stored in microtable$taxa_abund. Only available when it is the sole input.

multiple levels

use several levels at the same time, e.g., c("Genus", "Species"); the corresponding tables in microtable$taxa_abund are combined by rows as the predictors.

other input

must be a data.frame object. It should have the same format with the tables in microtable$taxa_abund, i.e. rows are features; columns are samples with same names in sample_table.

y.response

default NULL; the response variable in sample_table of input microtable object.

n.cores

default 1; the number of CPU threads used in the model training and the feature selection. The parallel backend of the foreach package is registered only temporarily during the computation and the original backend is restored afterwards, so that the global parallel setting is not permanently changed.

Returns

data_feature and data_response stored in the object.

Examples
\donttest{
data(dataset)
t1 <- trans_classifier$new(
		dataset = dataset, 
		x.predictors = "Genus",
		y.response = "Group")
# use multiple taxonomic levels as the predictors
t1 <- trans_classifier$new(
		dataset = dataset, 
		x.predictors = c("Genus", "Species"),
		y.response = "Group")
}

Method cal_split()

Split data for training and testing.

Usage
trans_classifier$cal_split(prop.train = 3/4)
Arguments
prop.train

default 3/4; the ratio of the data used for the training.

Returns

data_train and data_test in the object.

Examples
\dontrun{
t1$cal_split(prop.train = 3/4)
}

Method cal_preProcess()

Pre-process (centering, scaling etc.) of features based on the caret::preProcess function. See https://topepo.github.io/caret/pre-processing.html for more details. Some methods of preProcess, such as 'nzv', 'zv', 'pca' and 'ica', change the number of features. In that case the data_train and data_test are rebuilt with the new features and the corresponding information is printed, so that the feature names always match the data. The existing data_preProcess is reused when this function is called more than once; please assign NULL to object$data_preProcess if a new preprocess model is needed.

Usage
trans_classifier$cal_preProcess(...)
Arguments
...

parameters pass to preProcess function of caret package.

Returns

data_preProcess, data_train and data_test in the object. data_preProcess is the return data generated by the preProcess function of caret package based on the training data. data_train and data_test are preprocessed training and testing data based on the data_preProcess.

Examples
\dontrun{
# "nzv" removes near zero variance predictors
t1$cal_preProcess(method = c("center", "scale", "nzv"))
}

Method cal_feature_sel()

Perform feature selection. See https://topepo.github.io/caret/feature-selection-overview.html for more details. An error is raised when no feature is selected, as the following model training can not be performed in that case. Please consider increasing boruta.repetitions, decreasing boruta.pValue or using another x.predictors.

Usage
trans_classifier$cal_feature_sel(
  boruta.maxRuns = 300,
  boruta.pValue = 0.01,
  boruta.repetitions = 4,
  boruta.min.repetitions = 2,
  seed = NULL,
  ...
)
Arguments
boruta.maxRuns

default 300; maximal number of importance source runs; passed to the maxRuns parameter in Boruta function of Boruta package. Note that the Boruta function requires a value greater than 10.

boruta.pValue

default 0.01; p value passed to the pValue parameter in Boruta function of Boruta package.

boruta.repetitions

default 4; repetition runs for the feature selection.

boruta.min.repetitions

default 2; a feature is selected only when it is confirmed as important in at least this number of the boruta.repetitions independent runs. It must not be greater than boruta.repetitions.

seed

default NULL; the random seed used to make the repeated Boruta runs reproducible. When it is NULL, the random seeds are generated automatically and the result may change between the runs.

...

parameters pass to Boruta function of Boruta package.

Returns

optimized data_train and data_test in the object.

Examples
\dontrun{
t1$cal_feature_sel(boruta.maxRuns = 300, boruta.pValue = 0.01)
}

Method set_trainControl()

Control parameters for the following training. Please see trainControl function of caret package for details.

Usage
trans_classifier$set_trainControl(
  method = "repeatedcv",
  classProbs = TRUE,
  savePredictions = "final",
  ...
)
Arguments
method

default 'repeatedcv'; 'repeatedcv': Repeated k-Fold cross validation; see method parameter in trainControl function of caret package for available options.

classProbs

default TRUE; should class probabilities be computed for classification models?; see classProbs parameter in caret::trainControl function.

savePredictions

default 'final'; see savePredictions parameter in caret::trainControl function. 'final' means that only the predictions of the final model with the best tuning parameters are saved, which is required by the cal_ROC function when input = "train". TRUE (or "all") saves the predictions of all the tuning parameter combinations and is not recommended, as the repeated predictions of the same sample are not independent observations and would distort the ROC analysis.

...

parameters pass to trainControl function of caret package.

Returns

trainControl in the object.

Examples
\dontrun{
t1$set_trainControl(method = 'repeatedcv')
}

Method cal_train()

Run the model training. Please see https://topepo.github.io/caret/available-models.html for available models. The mtry and ntree optimization is performed for the "rf" method in both the classification and the regression. The mean Accuracy of the resamples is used to select the best ntree for the classification and the mean RMSE for the regression. The selected mtry and ntree are stored in object$train_params.

Usage
trans_classifier$cal_train(method = "rf", max.mtry = 2, ntree = 500, ...)
Arguments
method

default "rf"; "rf": random forest; see method in train function of caret package for other options. For method = "rf", the tuneGrid is set: expand.grid(mtry = seq(from = 1, to = max.mtry))

max.mtry

default 2; for method = "rf"; maximum mtry used in the tuneGrid to do hyperparameter tuning to optimize the model.

ntree

default 500; for method = "rf"; Number of trees to grow. The default 500 is same with the ntree parameter in randomForest function in randomForest package. When it is a vector with more than one element, the function will try to optimize the model to select a best one, such as c(100, 500, 1000).

...

parameters pass to caret::train function.

Returns

res_train in the object.

Examples
\dontrun{
# random forest
t1$cal_train(method = "rf")
# Support Vector Machines with Radial Basis Function Kernel
t1$cal_train(method = "svmRadial", tuneLength = 15)
}

Method cal_feature_imp()

Get feature importance from the training model.

Usage
trans_classifier$cal_feature_imp(rf_feature_sig = FALSE, ...)
Arguments
rf_feature_sig

default FALSE; whether calculate feature significance in 'rf' model using rfPermute package; only available for method = "rf" in cal_train function.

...

parameters pass to varImp function of caret package. If rf_feature_sig is TRUE and train_method is "rf", the parameters will be passed to rfPermute function of rfPermute package.

Returns

res_feature_imp in the object. One row for each predictor variable. The column(s) are different importance measures. When rf_feature_sig = FALSE, the importance is calculated with the varImp function of caret package and the column is usually 'Overall' for both the classification and the regression. When rf_feature_sig = TRUE, the importance values generated by the rfPermute package are used, such as 'MeanDecreaseAccuracy' and 'MeanDecreaseGini' for the classification, and the corresponding p values are stored in the columns with the '.pval' suffix.

Examples
\dontrun{
t1$cal_feature_imp()
}

Method plot_feature_imp()

Bar plot for feature importance.

Usage
trans_classifier$plot_feature_imp(
  rf_sig_show = NULL,
  show_sig_group = FALSE,
  ...
)
Arguments
rf_sig_show

default NULL; "MeanDecreaseAccuracy" (Default) or "MeanDecreaseGini" for random forest classification; "%IncMSE" (Default) or "IncNodePurity" for random forest regression; Only available when rf_feature_sig = TRUE in function cal_feature_imp, which generate "MeanDecreaseGini" (and "MeanDecreaseAccuracy") or "%IncMSE" (and "IncNodePurity") in the column names of res_feature_imp; Function can also generate "Significance" according to the p value.

show_sig_group

default FALSE; whether show the features with different significant groups; Only available when "Significance" is found in the data.

...

parameters pass to plot_diff_bar function of trans_diff package.

Returns

ggplot2 object.

Examples
\dontrun{
t1$plot_feature_imp(use_number = 1:20, coord_flip = FALSE)
}

Method cal_predict()

Run the prediction.

Usage
trans_classifier$cal_predict(positive_class = NULL)
Arguments
positive_class

default NULL; see positive parameter in confusionMatrix function of caret package; If positive_class is NULL, use the first group in data as the positive class automatically.

Returns

res_predict and the performance data stored in the object. The res_predict is the predicted result for data_test. For the classification, res_confusion_fit and res_confusion_stats are stored in the object. For the regression, res_regression_stats with RMSE, Rsquared and MAE is stored in the object. Several evaluation metrics in res_confusion_fit are defined as follows:

Accuracy = \frac{TP + TN}{TP + TN + FP + FN}

Sensitivity = Recall = TPR = \frac{TP}{TP + FN}

Specificity = TNR = 1 - FPR = \frac{TN}{TN + FP}

Precision = \frac{TP}{TP + FP}

Prevalence = \frac{TP + FN}{TP + TN + FP + FN}

F1-Score = \frac{2 * Precision * Recall}{Precision + Recall}

Kappa = \frac{Accuracy - Pe}{1 - Pe}

where TP is true positive; TN is ture negative; FP is false positive; and FN is false negative; FPR is False Positive Rate; TPR is True Positive Rate; TNR is True Negative Rate; Pe is the hypothetical probability of chance agreement on the classes for reference and prediction in the confusion matrix. Accuracy represents the ratio of correct predictions. Precision identifies how the model accurately predicted the positive classes. Recall (sensitivity) measures the ratio of actual positives that are correctly identified by the model. F1-score is the weighted average score of recall and precision. The value at 1 is the best performance and at 0 is the worst. Prevalence represents how often positive events occurred. Kappa identifies how well the model is predicting.

Examples
\dontrun{
t1$cal_predict()
}

Method plot_confusionMatrix()

Plot the cross-tabulation of observed and predicted classes with associated statistics based on the results of function cal_predict.

Usage
trans_classifier$plot_confusionMatrix(
  plot_confusion = TRUE,
  plot_statistics = TRUE,
  as_ggplot = FALSE
)
Arguments
plot_confusion

default TRUE; whether plot the confusion matrix.

plot_statistics

default TRUE; whether plot the statistics.

as_ggplot

default FALSE; whether return a ggplot object instead of the gtable object assembled by the gridExtra package. As the statistics table is plotted with the tableGrob function, only the confusion matrix is returned when as_ggplot = TRUE; the statistics table is available in object$res_confusion_stats.

Returns

gtable object assembled by the gridExtra package when as_ggplot = FALSE (default), or ggplot object when as_ggplot = TRUE.

Examples
\dontrun{
t1$plot_confusionMatrix()
}

Method cal_ROC()

Get ROC (Receiver Operator Characteristic) curve data and the performance data. When input = "train", the predictions are obtained from object$res_train$pred, which requires savePredictions in set_trainControl to be 'final' (default). When the predictions of several tuning parameter combinations are saved (e.g. savePredictions = TRUE), only the first prediction of each sample is used, as the repeated predictions of a sample are not independent observations.

Usage
trans_classifier$cal_ROC(input = "pred")
Arguments
input

default "pred"; 'pred' or 'train'; 'pred' represents using prediction results; 'train' represents using training results.

Returns

a list res_ROC stored in the object. It has two tables: res_roc and res_pr. AUC: Area Under the ROC Curve. For the definition of metrics, please refer to the return part of function cal_predict.

Examples
\dontrun{
t1$cal_ROC()
}

Method plot_ROC()

Plot ROC curve.

Usage
trans_classifier$plot_ROC(
  plot_type = c("ROC", "PR")[1],
  plot_group = "all",
  color_values = RColorBrewer::brewer.pal(8, "Dark2"),
  add_AUC = TRUE,
  plot_method = FALSE,
  ...
)
Arguments
plot_type

default c("ROC", "PR")[1]; 'ROC' represents ROC (Receiver Operator Characteristic) curve; 'PR' represents PR (Precision-Recall) curve.

plot_group

default "all"; 'all' represents all the classes in the model; 'add' represents all adding micro-average and macro-average results, see https://scikit-learn.org/stable/auto_examples/model_selection/plot_roc.html; other options should be one or more class names, same with the names in Group column of res_ROC$res_roc from cal_ROC function.

color_values

default RColorBrewer::brewer.pal(8, "Dark2"); colors used in the plot.

add_AUC

default TRUE; whether add AUC in the legend.

plot_method

default FALSE; If TRUE, show the method in the legend though only one method is found.

...

parameters pass to geom_path function of ggplot2 package.

Returns

ggplot2 object.

Examples
\dontrun{
t1$plot_ROC(size = 1, alpha = 0.7)
}

Method cal_caretList()

Use caretList function of caretEnsemble package to run multiple models. For the available models, please run names(getModelInfo()).

Usage
trans_classifier$cal_caretList(...)
Arguments
...

parameters pass to caretList function of caretEnsemble package.

Returns

res_caretList_models in the object.

Examples
\dontrun{
t1$cal_caretList(methodList = c('rf', 'svmRadial'))
}

Method cal_caretList_resamples()

Use resamples function of caret package to collect the metric values based on the res_caretList_models data.

Usage
trans_classifier$cal_caretList_resamples(...)
Arguments
...

parameters pass to resamples function of caret package.

Returns

res_caretList_resamples list and res_caretList_resamples_reshaped table in the object.

Examples
\dontrun{
t1$cal_caretList_resamples()
}

Method plot_caretList_resamples()

Visualize the metric values based on the res_caretList_resamples_reshaped data.

Usage
trans_classifier$plot_caretList_resamples(
  color_values = RColorBrewer::brewer.pal(8, "Dark2"),
  ...
)
Arguments
color_values

default RColorBrewer::brewer.pal(8, "Dark2"); colors palette for the box.

...

parameters pass to geom_boxplot function of ggplot2 package.

Returns

ggplot object.

Examples
\dontrun{
t1$plot_caretList_resamples()
}

Method print()

Print the trans_classifier object.

Usage
trans_classifier$print()

Method clone()

The objects of this class are cloneable with this method.

Usage
trans_classifier$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.

Examples


## ------------------------------------------------
## Method `trans_classifier$new`
## ------------------------------------------------


data(dataset)
t1 <- trans_classifier$new(
		dataset = dataset, 
		x.predictors = "Genus",
		y.response = "Group")
# use multiple taxonomic levels as the predictors
t1 <- trans_classifier$new(
		dataset = dataset, 
		x.predictors = c("Genus", "Species"),
		y.response = "Group")


## ------------------------------------------------
## Method `trans_classifier$cal_split`
## ------------------------------------------------

## Not run: 
t1$cal_split(prop.train = 3/4)

## End(Not run)

## ------------------------------------------------
## Method `trans_classifier$cal_preProcess`
## ------------------------------------------------

## Not run: 
# "nzv" removes near zero variance predictors
t1$cal_preProcess(method = c("center", "scale", "nzv"))

## End(Not run)

## ------------------------------------------------
## Method `trans_classifier$cal_feature_sel`
## ------------------------------------------------

## Not run: 
t1$cal_feature_sel(boruta.maxRuns = 300, boruta.pValue = 0.01)

## End(Not run)

## ------------------------------------------------
## Method `trans_classifier$set_trainControl`
## ------------------------------------------------

## Not run: 
t1$set_trainControl(method = 'repeatedcv')

## End(Not run)

## ------------------------------------------------
## Method `trans_classifier$cal_train`
## ------------------------------------------------

## Not run: 
# random forest
t1$cal_train(method = "rf")
# Support Vector Machines with Radial Basis Function Kernel
t1$cal_train(method = "svmRadial", tuneLength = 15)

## End(Not run)

## ------------------------------------------------
## Method `trans_classifier$cal_feature_imp`
## ------------------------------------------------

## Not run: 
t1$cal_feature_imp()

## End(Not run)

## ------------------------------------------------
## Method `trans_classifier$plot_feature_imp`
## ------------------------------------------------

## Not run: 
t1$plot_feature_imp(use_number = 1:20, coord_flip = FALSE)

## End(Not run)

## ------------------------------------------------
## Method `trans_classifier$cal_predict`
## ------------------------------------------------

## Not run: 
t1$cal_predict()

## End(Not run)

## ------------------------------------------------
## Method `trans_classifier$plot_confusionMatrix`
## ------------------------------------------------

## Not run: 
t1$plot_confusionMatrix()

## End(Not run)

## ------------------------------------------------
## Method `trans_classifier$cal_ROC`
## ------------------------------------------------

## Not run: 
t1$cal_ROC()

## End(Not run)

## ------------------------------------------------
## Method `trans_classifier$plot_ROC`
## ------------------------------------------------

## Not run: 
t1$plot_ROC(size = 1, alpha = 0.7)

## End(Not run)

## ------------------------------------------------
## Method `trans_classifier$cal_caretList`
## ------------------------------------------------

## Not run: 
t1$cal_caretList(methodList = c('rf', 'svmRadial'))

## End(Not run)

## ------------------------------------------------
## Method `trans_classifier$cal_caretList_resamples`
## ------------------------------------------------

## Not run: 
t1$cal_caretList_resamples()

## End(Not run)

## ------------------------------------------------
## Method `trans_classifier$plot_caretList_resamples`
## ------------------------------------------------

## Not run: 
t1$plot_caretList_resamples()

## End(Not run)

microeco documentation built on Oct. 20, 2026, 9:06 a.m.