### Install Learner Packages Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/quick-reference.md Install necessary R packages for specific mlr3learners. Each package provides functionality for a set of learners. ```r install.packages("glmnet") ``` ```r install.packages("ranger") ``` ```r install.packages("xgboost") ``` ```r install.packages("e1071") ``` ```r install.packages("kknn") ``` ```r install.packages("MASS") ``` ```r install.packages("nnet") ``` ```r install.packages("DiceKriging") ``` -------------------------------- ### Install mlr3learners Core Package Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/quick-reference.md Instructions for installing the core mlr3learners package using install.packages or the development version with pak. ```r install.packages("mlr3learners") # or development version pak::pak("mlr-org/mlr3learners") ``` -------------------------------- ### Install mlr3learners with Dependencies (Development Version) Source: https://github.com/mlr-org/mlr3learners/blob/main/README.md Installs the development version of mlr3learners and all its dependent packages using the 'pak' package manager. ```r pak::pak("mlr-org/mlr3learners", dependencies = TRUE) ``` -------------------------------- ### Convert Ratio Parameter to Count Example Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/helpers.md Demonstrates how to convert a feature ratio parameter to a feature count using the convert_ratio function. The example shows the calculation for mtry. ```R pv = list(mtry.ratio = 0.5) pv = convert_ratio(pv, "mtry", "mtry.ratio", 10) # mtry is set to max(ceiling(0.5 * 10), 1) = 5 ``` -------------------------------- ### Install mlr3learners Development Version Source: https://github.com/mlr-org/mlr3learners/blob/main/README.md Installs the latest development version of the mlr3learners package using the 'pak' package manager. ```r pak::pak("mlr-org/mlr3learners") ``` -------------------------------- ### Install mlr3learners with Dependencies from CRAN Source: https://github.com/mlr-org/mlr3learners/blob/main/README.md Installs the stable version of mlr3learners and all its dependent packages from CRAN. ```r install.packages("mlr3learners", dependencies = TRUE) ``` -------------------------------- ### Configuring Learning Rate and Iterations (xgboost) Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/configuration.md Example of setting key hyperparameters for the xgboost learner, including the number of boosting rounds (nrounds), learning rate (eta), and tree complexity (max_depth). ```r learner = lrn("classif.xgboost") learner$param_set$values = list( nrounds = 100, # Number of boosting rounds eta = 0.1, # Learning rate (step size) max_depth = 6 # Tree complexity ) ``` -------------------------------- ### Install mlr3learners from CRAN Source: https://github.com/mlr-org/mlr3learners/blob/main/README.md Installs the stable version of the mlr3learners package from CRAN. ```r install.packages("mlr3learners") ``` -------------------------------- ### Instantiate k-Nearest Neighbors Classifier Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/classification-learners.md Creates a new instance of the k-Nearest Neighbors classification learner. No specific setup is required beyond having the learner loaded. ```r LearnerClassifKKNN$new() ``` -------------------------------- ### Breaking Change Footer Example Source: https://github.com/mlr-org/mlr3learners/blob/main/extra-rules/commit-messages.md Illustrates how to denote a breaking change using the 'BREAKING CHANGE:' footer. ```text BREAKING CHANGE: parameter 'x' has been removed ``` -------------------------------- ### Retrieving Package Version Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/types.md Get the installed version of the mlr3learners package. ```r packageVersion("mlr3learners") ``` -------------------------------- ### Accessing and Setting Learner Parameters Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/configuration.md Demonstrates how to list, get, and set hyperparameter values for an mlr3 learner using its param_set object. ```r learner = lrn("classif.log_reg") # List all parameters learner$param_set$params # Get parameter by name param = learner$param_set$param("link") # Get all parameter values values = learner$param_set$values # Set parameter values learner$param_set$values = list(link = "probit", maxit = 100) ``` -------------------------------- ### Defining Logical Parameters Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/configuration.md Example of defining a boolean/logical parameter with a default value and tags. ```r # Example: replace in ranger (sample with replacement) replace = p_lgl(default = TRUE, tags = "train") ``` -------------------------------- ### Instantiate SVM Classifier Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/classification-learners.md Creates a new instance of the SVM classification learner. No specific setup is required beyond having the learner loaded. ```r LearnerClassifSVM$new() ``` -------------------------------- ### Instantiate Quadratic Discriminant Analysis Classifier Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/classification-learners.md Creates a new instance of the Quadratic Discriminant Analysis classification learner. No specific setup is required beyond having the learner loaded. ```r LearnerClassifQDA$new() ``` -------------------------------- ### Defining Categorical Parameters Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/configuration.md Example of defining a factor/categorical parameter with a predefined set of levels, a default value, and tags. ```r # Example: kernel in svm kernel = p_fct(c("linear", "polynomial", "radial", "sigmoid"), default = "radial", tags = "train") ``` -------------------------------- ### Defining Numeric Parameters (Integer) Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/configuration.md Example of defining an integer-valued parameter with a lower bound, a default value, and tags. ```r # Example: num.trees in ranger num.trees = p_int(1L, default = 500L, tags = c("train", "predict", "hotstart")) ``` -------------------------------- ### Instantiate Linear Discriminant Analysis Classifier Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/classification-learners.md Creates a new instance of the Linear Discriminant Analysis classification learner. No specific setup is required beyond having the learner loaded. ```r LearnerClassifLDA$new() ``` -------------------------------- ### Defining Numeric Parameters (Double) Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/configuration.md Example of defining a double-precision numeric parameter with lower and upper bounds, a default value, and tags. ```r # Example: eta in xgboost (learning rate, 0-1) eta = p_dbl(0, 1, default = 0.3, tags = "train") ``` -------------------------------- ### Defining Parameter Dependencies Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/configuration.md Example of defining a parameter with a dependency on another parameter's value. This ensures the parameter is only relevant or applied when the condition is met. ```r # Example: SVM gamma only meaningful for polynomial/radial/sigmoid kernels # In parameter specification: gamma = p_dbl(0, tags = "train", depends = quote(kernel %in% c("polynomial", "radial", "sigmoid"))) ``` -------------------------------- ### Create and Train a Simple Pipeline Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/advanced-usage.md Demonstrates creating a pipeline with normalization and a logistic regression learner, followed by training and prediction. ```r pipeline = po("scale") %>>% lrn("classif.log_reg") pipeline$train(task) pred = pipeline$predict(task_test) ``` -------------------------------- ### Instantiate a Classification Learner Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/index.md Demonstrates the standard pattern for creating a new classification learner instance. This pattern is consistent across all classification learners in the package. ```r learner = LearnerClassifLogReg$new() ``` -------------------------------- ### Check Parameter Dependencies Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/configuration.md Shows how to inspect the dependencies of a learner's parameters. ```r learner$param_set$deps ``` -------------------------------- ### Basic Learner Training and Prediction Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/quick-reference.md Demonstrates the fundamental workflow of creating a learner, training it on a task, making predictions, and scoring the results. ```r # Create learner learner = lrn("classif.log_reg") # Train on task learner$train(task_train) # Make predictions predictions = learner$predict(task_test) # Get metrics predictions$score(msr("classif.acc")) ``` -------------------------------- ### Glmnet: Get Selected Features Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/advanced-usage.md Retrieves features with non-zero coefficients from a trained Glmnet learner. Supports specifying a lambda value to get features for a specific regularization strength. Ensure the learner is trained. ```r learner = lrn("classif.glmnet", lambda = 0.01) learner$train(task) # Get features with non-zero coefficients features = learner$selected_features() # Returns: character() - names of selected features # With specific lambda (after training with multiple lambdas) features = learner$selected_features(lambda = 0.005) ``` -------------------------------- ### Implementing Try-Catch for Error Handling Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/types.md Demonstrates a robust way to handle potential errors during learner training using R's tryCatch mechanism. ```r tryCatch({ learner$train(task) }, error = function(e) { message("Training failed: ", e$message) }) ``` -------------------------------- ### Registering a New Learner Source: https://github.com/mlr-org/mlr3learners/blob/main/extra-rules/mlr3.md Demonstrates how to add a new learner to the mlr_learners dictionary. Ensure the learner class is defined and the dictionary file is included. ```r #' @include mlr_learners.R mlr_learners$add("classif.rpart", function() LearnerClassifRpart$new()) ``` -------------------------------- ### Classification Prediction Formats Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/types.md Examples of how response and probability matrices are formatted for classification predictions. ```r response = factor(c("yes", "no"), levels = c("no", "yes")) prob = matrix(c(0.3, 0.7, 0.9, 0.1), ncol = 2) colnames(prob) = c("no", "yes") ``` -------------------------------- ### Build Complex Pipelines with mlr3pipelines Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/advanced-usage.md Demonstrates building a complex pipeline using mlr3pipelines, including scaling, PCA, and a ranger learner, followed by training and prediction. ```r library(mlr3pipelines) # Build complex pipelines graph = po("scale") %>>% po("pca") %>>% lrn("classif.ranger") graph$train(task) pred = graph$predict(task_test) ``` -------------------------------- ### Train and Predict with XGBoost Learner Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/classification-learners.md Shows how to instantiate an XGBoost learner with custom parameters for training rounds and learning rate, then train and predict. ```r learner = lrn("classif.xgboost", nrounds = 500, eta = 0.1) learner$train(task_train) pred = learner$predict(task_test) ``` -------------------------------- ### Regression Prediction Formats Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/types.md Examples of how response and standard error vectors are formatted for regression predictions. ```r response = c(10.5, 20.3, 15.1) se = c(0.5, 0.8, 0.3) ``` -------------------------------- ### Configuring Regularization Parameters (glmnet) Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/configuration.md Demonstrates setting regularization parameters for penalized regression learners like glmnet, including alpha (type of regularization) and lambda (strength). ```r learner = lrn("classif.glmnet", lambda = 0.01) learner$param_set$values = list( alpha = 1, # 0=ridge, 0.5=elastic net, 1=lasso lambda = 0.01, # Regularization strength standardize = TRUE # Standardize features first ) ``` -------------------------------- ### Validate Learner Parameters Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/configuration.md Shows how to validate parameter values for a learner using paradox and check parameter constraints. ```r learner = lrn("classif.ranger") # Invalid parameter value raises error tryCatch( learner$param_set$values = list(num.threads = -1), # Must be >= 1 error = function(e) message("Validation failed: ", e$message) ) # Check parameter constraints learner$param_set$param("num.threads")$lower ``` -------------------------------- ### Handling Observation Weights Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/configuration.md Shows how to prepare a task with an observation weight column and train a learner using these weights. The task must have a column designated with the 'weight' role. ```r # Weights are automatically extracted from task # Task must have a "weight" column task_with_weights = task$clone() task_with_weights$set_col_roles("weight_column", new_roles = "weight") learner = lrn("classif.ranger") learner$train(task_with_weights) ``` -------------------------------- ### Instantiate and Train Elastic Net Learner with Hyperparameters Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/regression-learners.md Instantiate LearnerRegrGlmnet with custom alpha and train it. The selected_features() method can retrieve features with non-zero coefficients. ```r learner = lrn("regr.glmnet", alpha = 0.5) learner$train(task_train) features = learner$selected_features() ``` -------------------------------- ### Gradient Boosting (xgboost) Key Parameters Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/quick-reference.md Key parameters for Gradient Boosting learners using the xgboost package. Configure boosting rounds, learning rate, tree depth, minimum child weight, sampling ratios, and early stopping. Specify the number of threads and the loss function. ```r # Key parameters nrounds = 1000 # Boosting rounds eta = 0.3 # Learning rate (0-1) max_depth = 6 # Tree depth min_child_weight = 1 # Minimum child weight subsample = 1 # Row sampling ratio colsample_bytree = 1 # Column sampling ratio early_stopping_rounds = NULL # Stop if no improvement nthread = 1 # Threads objective = "multi:softmax" # Loss function ``` -------------------------------- ### Defining Untyped Parameters Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/configuration.md Example of defining an untyped parameter that can accept arbitrary R objects, with optional special value handling and tags. ```r # Example: prior in lda prior = p_uty(tags = "train") # Example with special values (NULL allowed) lambda = p_uty(tags = "train") # Can be NULL or numeric vector ``` -------------------------------- ### Configure XGBoost with Early Stopping Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/configuration.md Sets up an XGBoost learner with early stopping to prevent overfitting and specifies a custom metric for monitoring. ```r learner = lrn("classif.xgboost") learner$param_set$values = list( nrounds = 10000, # Max boosting rounds early_stopping_rounds = 50, # Stop if no improvement for N rounds custom_metric = mlr3::msr("auc") # Metric to monitor ) # Validation set must be configured separately via learner$validate ``` -------------------------------- ### Redocument Package with Roxygen Source: https://github.com/mlr-org/mlr3learners/blob/main/AGENTS.md Update package documentation, including man pages and NAMESPACE file, based on roxygen2 comments. This should be run after any changes to documentation. ```bash Rscript -e "devtools::document()" ``` -------------------------------- ### XGBoost: Get Variable Importance Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/advanced-usage.md Retrieves variable importance scores from a trained XGBoost learner. Returns a named numeric vector. Ensure the learner is trained. ```r learner = lrn("classif.xgboost") learner$train(task) # Get variable importance importance = learner$importance() # Returns: named numeric vector ``` -------------------------------- ### Invoke glmnet or cv.glmnet Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/helpers.md Invokes glmnet::glmnet() or glmnet::cv.glmnet() with proper control setup. It handles saving and restoring glmnet global control parameters. ```R glmnet_invoke <- function(data, target, pv, cv = FALSE) { # Save current glmnet control parameters old_control <- glmnet::glmnet.control() on.exit(glmnet::glmnet.control(old_control)) # Separate control parameters from regular parameters pv_control <- pv[names(pv) %in% names(formals(glmnet::glmnet.control))] pv_regular <- pv[!names(pv) %in% names(formals(glmnet::glmnet.control))] # Set new control parameters if (length(pv_control) > 0) { do.call(glmnet::glmnet.control, pv_control) } # Invoke the appropriate glmnet function if (cv) { fit <- glmnet::cv.glmnet(x = data, y = target, ... = pv_regular) } else { fit <- glmnet::glmnet(x = data, y = target, ... = pv_regular) } return(fit) } ``` -------------------------------- ### Instantiate and Train Linear Regression Learner Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/regression-learners.md Instantiate the LearnerRegrLM and train it on a task. Prediction types include 'response' and 'se'. ```r learner = lrn("regr.lm") learner$train(task_train) pred = learner$predict(task_test) ``` -------------------------------- ### Get Selected Features from glmnet Model Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/helpers.md Returns features with non-zero coefficients from a glmnet model. It can optionally use a specified lambda value or the default one from the learner. ```R glmnet_selected_features <- function(self, lambda = NULL) { if (is.null(lambda)) { lambda <- glmnet_get_lambda(self) } model <- self$model$learner.model if (is.null(model)) { stop("No model stored") } # Use predict method of glmnet to get non-zero coefficients # The 'nonzero' type is available from glmnet version 4.1-2 if (packageVersion("glmnet") >= "4.1.2") { preds <- predict(model, type = "nonzero", s = lambda) # preds is a list of vectors, one for each response # For a single response, it's a list with one element if (is.list(preds)) { selected_indices <- unlist(preds) } else { selected_indices <- preds } } else { # Fallback for older glmnet versions # This requires extracting coefficients manually beta <- coef(model, s = lambda) if (is.list(beta)) { # For multi-response models, coef returns a list of matrices/vectors # We need to combine them or select one, depending on the task # For simplicity, let's assume single response or take the first one beta <- beta[[1]] } selected_indices <- which(beta[-1, 1] != 0) # Exclude intercept } feature_names <- self$feature_names if (selected_indices[1] == "(Intercept)") { selected_indices <- selected_indices[-1] } # Convert indices to names selected_names <- feature_names[as.integer(selected_indices)] # Ensure unique and sorted names unique(sort(selected_names)) } ``` -------------------------------- ### Train and Predict with CV.GLMNET Learner Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/classification-learners.md Demonstrates how to initialize, train, and predict using the CV.GLMNET classification learner. ```r learner = lrn("classif.cv_glmnet") learner$train(task_train) pred = learner$predict(task_train) ``` -------------------------------- ### Defining Hyperparameters with Paradox Source: https://github.com/mlr-org/mlr3learners/blob/main/extra-rules/mlr3.md Shows how to define hyperparameters for a learner using the paradox package. Parameters are tagged as 'train' or 'predict' to indicate their usage. ```r ps = ps( cp = p_dbl(0, 1, default = 0.01, tags = "train"), keep_model = p_lgl(default = FALSE, tags = "train") ) ``` -------------------------------- ### Train Elastic Net Learner and Get Selected Features Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/classification-learners.md Trains an Elastic Net model with a specified lambda and retrieves the features with non-zero coefficients. Requires the `glmnet` package. ```r learner = lrn("classif.glmnet", lambda = 0.01) learner$train(task_train) features = learner$selected_features() ``` -------------------------------- ### Initialize k-Nearest Neighbors Regression Learner Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/regression-learners.md Creates an instance of the k-NN regression learner with a custom number of neighbors (k). This is a lazy learner that stores training data. ```r learner = lrn("regr.kknn", k = 5) learner$train(task_train) pred = learner$predict(task_test) ``` -------------------------------- ### Ranger: Get Selected Features Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/advanced-usage.md Retrieves the names of features used in splits from a trained Ranger learner. This indicates which features were considered important for model construction. Ensure the learner is trained. ```r learner = lrn("classif.ranger") learner$train(task) # Get features used in splits features = learner$selected_features() # Returns: character() - names of features with split nodes ``` -------------------------------- ### Ranger: Get Variable Importance Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/advanced-usage.md Accesses the variable importance scores from a trained Ranger learner. Returns a named numeric vector sorted by importance. Ensure the learner is trained and 'importance' parameter is set. ```r learner = lrn("classif.ranger", importance = "impurity") learner$train(task) # Get variable importance importance = learner$importance() # Returns: named numeric vector sorted by importance (decreasing) # Plot importance plot(sort(importance, decreasing = TRUE)) ``` -------------------------------- ### Configuring Early Stopping for XGBoost Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/advanced-usage.md Sets up an XGBoost learner with early stopping parameters. It specifies the maximum boosting rounds, the number of rounds for early stopping, and the metrics to monitor. ```r learner = lrn("classif.xgboost") # Configure early stopping learner$param_set$values = list( nrounds = 10000, # Max boosting rounds early_stopping_rounds = 50, # Stop if no improvement custom_metric = msr("classif.auc"), # Metric to monitor eval_metric = "logloss" # Also compute logloss ) # Set validation set learner$validate = 0.2 # Use 20% for validation # Train learner$train(task) # Access tuning results if (!is.null(learner$internal_valid_scores)) { # Early stopping occurred best_round = learner$internal_tuned_values$nrounds } ``` -------------------------------- ### Ranger: Get Out-of-Bag Error Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/advanced-usage.md Retrieves the out-of-bag (OOB) error estimate from a trained Ranger learner. This provides a measure of prediction error on samples not used for training individual trees. Ensure the learner is trained. ```r learner = lrn("classif.ranger") learner$train(task) # Get OOB error estimate oob_error = learner$oob_error() # Returns: numeric(1) - the prediction error on OOB samples ``` -------------------------------- ### Train and Predict with Regression Kriging Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/regression-learners.md Demonstrates how to initialize the regression kriging learner with specific parameters, train it on a task, and make predictions. ```r learner = lrn("regr.km", covtype = "matern5_2", nugget.stability = 1e-8) learner$train(task_train) pred = learner$predict(task_test) ``` -------------------------------- ### Response Level Reordering for Binary Classification Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/types.md For binary classification, response levels may need reordering to match specific conventions. For example, log_reg reverses levels to align with glm conventions, placing the negative class first. ```r # log_reg reverses levels to match glm convention # y = swap_levels(y) # Negative class first ``` -------------------------------- ### Initialize Kriging Regression Learner Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/regression-learners.md Creates a Kriging regression learner. This learner supports computing standard errors and covariance for predictions. ```r LearnerRegrKM$new() ``` -------------------------------- ### Define Parameter with Dependency Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/configuration.md Demonstrates how to define a parameter that is only used when a specific condition is met, such as a kernel type. ```r coef0 = p_dbl(default = 0, tags = "train", depends = quote(kernel %in% c("polynomial", "sigmoid"))) ``` -------------------------------- ### Standard MLR3 Training and Prediction Workflow Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/index.md Illustrates the typical sequence of operations for training a learner on a task and then making predictions on a test set using the mlr3 framework. ```r # Create learner learner = LearnerClassifLogReg$new() # Train on task learner$train(task) # Make predictions predictions = learner$predict(task_test) ``` -------------------------------- ### Compare Training Times of Multiple Learners Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/advanced-usage.md Uses the microbenchmark package to compare the training times of different learners. It runs each learner's training process multiple times and prints the mean execution time. ```r library(microbenchmark) learners = list( lrn("classif.log_reg"), lrn("classif.ranger"), lrn("classif.svm") ) timing = sapply(learners, function(learner) { microbenchmark( {learner$train(task)}, times = 3 )$mean }) print(timing) ``` -------------------------------- ### R6 Class Hierarchy Overview Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/types.md Illustrates the inheritance structure of learners in mlr3learners, showing how classification and regression learners extend base mlr3 classes. ```text Learner (mlr3) ├── LearnerClassif (mlr3) │ ├── LearnerClassifLogReg │ ├── LearnerClassifGlmnet │ ├── LearnerClassifCVGlmnet │ ├── LearnerClassifRanger │ ├── LearnerClassifXgboost │ ├── LearnerClassifSVM │ ├── LearnerClassifKKNN │ ├── LearnerClassifLDA │ ├── LearnerClassifQDA │ ├── LearnerClassifNaiveBayes │ ├── LearnerClassifNnet │ └── LearnerClassifMultinom │ └── LearnerRegr (mlr3) ├── LearnerRegrLM ├── LearnerRegrGlmnet ├── LearnerRegrCVGlmnet ├── LearnerRegrRanger ├── LearnerRegrXgboost ├── LearnerRegrSVM ├── LearnerRegrKKNN ├── LearnerRegrNnet └── LearnerRegrKM ``` -------------------------------- ### Configuring Feature Count vs. Ratio (ranger) Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/configuration.md Shows how to set either an exact feature count (mtry) or a ratio (mtry.ratio) for the ranger learner. Setting both is mutually exclusive and will result in an error. ```r learner = lrn("classif.ranger") # Option 1: Specify exact feature count learner$param_set$values = list(mtry = 5) # Option 2: Specify ratio (converted to count) learner$param_set$values = list(mtry.ratio = 0.5) # These are mutually exclusive; setting both raises an error ``` -------------------------------- ### Hyperparameter Tuning with Random Search Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/quick-reference.md Illustrates hyperparameter tuning using random search with cross-validation for a ranger learner. Requires the mlr3tuning package. ```r library(mlr3tuning) learner = lrn("classif.ranger") search_space = ps(mtry.ratio = p_dbl(0.1, 1)) autotuner = auto_tuner( tuner = tnr("random_search"), learner = learner, resampling = rsmp("cv", folds = 3), measure = msr("classif.acc"), search_space = search_space, term_evals = 20 ) autotuner$train(task_train) predictions = autotuner$predict(task_test) ``` -------------------------------- ### Parameter Values List Structure Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/types.md Illustrates the structure for defining parameter values for a learner, typically a list containing hyperparameters like eta, nrounds, and max_depth. ```r pv = list( eta = 0.1, nrounds = 500, max_depth = 6, # ... ) ``` -------------------------------- ### Instantiate Logistic Regression Learner Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/classification-learners.md Creates a new instance of the Logistic Regression learner. This learner wraps `stats::glm()` with a binomial family. ```r LearnerClassifLogReg$new() ``` -------------------------------- ### Implementing a Read-Only Public Field Binding Source: https://github.com/mlr-org/mlr3learners/blob/main/extra-rules/mlr3.md Demonstrates how to create an active binding for a read-only public field. Assignment attempts to this binding will raise an error. ```r task_type = function(rhs) { assert_ro_binding(rhs) private$.data$task_type } ``` -------------------------------- ### XGBoost Thread Control Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/advanced-usage.md Demonstrates equivalent ways to configure the number of threads for XGBoost learners, either directly during learner creation or by setting the parameter after initialization. This allows control over XGBoost's internal parallelization. ```r # Equivalent configurations learner1 = lrn("classif.xgboost", nthread = 1) learner2 = lrn("classif.xgboost") learner2$param_set$values$nthread = 2 ``` -------------------------------- ### Check Pkgdown Documentation Source: https://github.com/mlr-org/mlr3learners/blob/main/AGENTS.md Verify the integrity and rendering of the package's website generated by pkgdown. Ensures documentation links and structure are correct. ```bash Rscript -e "pkgdown::check_pkgdown()" ``` -------------------------------- ### Handling Offsets Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/configuration.md Illustrates how to prepare a task with an offset column and train a GLM-based learner that supports offsets. The task must have a column designated with the 'offset' role. ```r # Task must have an "offset" column task_with_offset = task$clone() task_with_offset$set_col_roles("offset_column", new_roles = "offset") learner = lrn("classif.log_reg") learner$param_set$values = list(use_pred_offset = TRUE) learner$train(task_with_offset) ``` -------------------------------- ### Handle Training Failures with tryCatch Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/advanced-usage.md Demonstrates using a tryCatch block to handle potential training errors. If an error occurs, it prints a message and falls back to training a simpler 'classif.naive_bayes' learner. ```r learner = lrn("classif.log_reg") tryCatch({ learner$train(task) }, error = function(e) { message("Training error: ", e$message) # Fallback to simpler learner learner <<- lrn("classif.naive_bayes") learner$train(task) }) ``` -------------------------------- ### Default Contrast Options Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/helpers.md Provides a default list of contrast options for formula-based learners. These settings ensure consistent encoding of categorical variables across different models. ```R list(contrasts = c("contr.treatment", "contr.poly")) ``` -------------------------------- ### Implementing a Mutable Public Field Binding Source: https://github.com/mlr-org/mlr3learners/blob/main/extra-rules/mlr3.md Illustrates how to create an active binding for a mutable public field in an R6 class. The binding validates input using assert_* functions. ```r id = function(rhs) { if (missing(rhs)) { return(private$.id) } private$.id = assert_id(rhs) } ``` -------------------------------- ### Instantiate and Train Cross-Validated Elastic Net Learner Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/regression-learners.md Instantiate LearnerRegrCVGlmnet for cross-validated elastic net regression. It automatically tunes the lambda parameter. ```r learner = lrn("regr.cv_glmnet") learner$train(task_train) pred = learner$predict(task_test) ``` -------------------------------- ### Filtering Parameters by Tags Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/configuration.md Demonstrates how to retrieve parameters from a learner's param_set based on specified tags, including single tags, multiple tags, and combinations. ```r # Get only training parameters train_pars = learner$param_set$get_values(tags = "train") # Get only prediction parameters predict_pars = learner$param_set$get_values(tags = "predict") # Get parameters with multiple tags control_pars = learner$param_set$get_values(tags = c("train", "control")) ``` -------------------------------- ### Train and Predict with QDA Learner Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/classification-learners.md Instantiates a Quadratic Discriminant Analysis learner with the 'mle' method, trains it on a task, and makes predictions. ```r learner = lrn("classif.qda", method = "mle") learner$train(task_train) pred = learner$predict(task_test) ``` -------------------------------- ### Access Extended Learners from mlr3extralearners Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/advanced-usage.md Shows how to load the mlr3extralearners package and access additional learners like 'classif.lightgbm' and 'classif.catboost'. ```r # Additional learners available library(mlr3extralearners) # Access extended learners lrn("classif.lightgbm") lrn("classif.catboost") ``` -------------------------------- ### Train and Predict with SVM Regression Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/regression-learners.md Initializes an SVM regression learner with a specified kernel and cost, then trains and predicts on test data. Only numeric features are supported. ```r learner = lrn("regr.svm", kernel = "rbf", cost = 1) learner$train(task_train) pred = learner$predict(task_test) ``` -------------------------------- ### k-Nearest Neighbors (kknn) Key Parameters Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/quick-reference.md Key parameters for k-Nearest Neighbors learners. Specify the number of neighbors (k), Minkowski distance order, kernel type, and whether to standardize features. Set store_model to FALSE to avoid storing the kknn object. ```r # Key parameters k = 7 # Number of neighbors distance = 2 # Minkowski distance order kernel = "optimal" # Kernel type scale = TRUE # Standardize features store_model = FALSE # Store kknn object ``` -------------------------------- ### MLR3 Package Load Hook Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/helpers.md The .onLoad function registers namespace callbacks with mlr3, ensuring learners are registered when mlr3 loads. ```R .onLoad = function(libname, pkgname) { # Registers namespace callback with mlr3 # ... implementation details ... } ``` -------------------------------- ### Run Code with devtools::load_all() Source: https://github.com/mlr-org/mlr3learners/blob/main/AGENTS.md Execute R code within the package development environment. Ensures the latest package code is loaded before execution. ```bash Rscript -e "devtools::load_all(); code" ``` -------------------------------- ### Feature Selection with Importance Filter Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/advanced-usage.md Shows how to create a pipeline that includes feature selection using importance scores from a ranger learner, followed by training and prediction. ```r # Feature selection + ranger fselect_pipe = po("filter", filter = mlr3filters::flt("importance", learner = lrn("classif.ranger"))) %>>% lrn("classif.ranger") fselect_pipe$train(task) pred = fselect_pipe$predict(task_test) ``` -------------------------------- ### Run All Package Tests Source: https://github.com/mlr-org/mlr3learners/blob/main/AGENTS.md Execute the entire test suite for the R package. This is crucial for verifying the integrity of the package. ```bash Rscript -e "devtools::test()" ``` -------------------------------- ### Accessing ParamSet Parameters Source: https://github.com/mlr-org/mlr3learners/blob/main/_autodocs/types.md Shows how to access parameters and their properties from a learner's ParamSet object. ```r # Accessing parameters learner$param_set$params # List of all parameters learner$param_set$param("eta") # Specific parameter learner$param_set$ids # Parameter IDs ```