I am creating a learner for Maximum Entropy (MaxEnt) models using the mlr3 package. I use the maxnet function (from the maxnet package) to fit MaxEnt models. Here is a link to the function: maxnet.
I am testing my code. I have already solved some issues, but I don't know how to fix this one. Here is the error message:
Error in FUN(X[[i]], ...) :
only defined on a data frame with all numeric-alike variables
This leads me to two questions:
1) How can I solve this issue ?
2) Will the line of code below update properly during the tuning process?
param_set$values = list(regmult = 1, addsamplestobackground = TRUE, clamp = TRUE, classes = "default", response_type = "cloglog")
I am completely new to the mlr3 package and machine learning models in general. So, any help or advice would be greatly appreciated.
Here is my code:
LearnerClassifMaxnet = R6Class("LearnerClassifMaxnet",
inherit = LearnerClassif,
public = list(
initialize = function() {
## Define the parameter set of the learner
print("code 1")
param_set = ps(
regmult = p_dbl(default = 1, tags = "train"),
addsamplestobackground = p_lgl(default = TRUE, tags = "train"),
clamp = p_lgl(default = TRUE, tags = "predict"),
classes = p_fct(default = "default", levels = c("default", "l", "lq", "h", "lqh", "lqhp", "lqhpt"), tags = "train"),
response_type = p_fct(default = "cloglog", levels = c("link","exponential","cloglog","logistic"), tags = "predict")
)
print("code 2")
param_set$values = list(regmult = 1, addsamplestobackground = TRUE, clamp = TRUE, classes = "default", response_type = "cloglog")
## Initialize the learner
print("code 3")
super$initialize(
id = "classif.maxnet",
feature_types = c("logical", "integer", "numeric", "factor", "ordered"), ## feature types of predictor variables
predict_types = "prob",
packages = "maxnet",
param_set = param_set,
properties = c("weights", "twoclass"),
label = "Maximum Entropy",
man = "mlr3learners::mlr_learners_classif.maxnet"
)
}
),
## Define the train function
private = list(
.train = function(task) {
print("code 4")
pv = self$param_set$get_values(tags = "train")
if ("weights" %in% task$properties) {
pv$weights = task$weights$weight
}
## Build the maxnet model
print("code 5")
invoke(
maxnet::maxnet,
p = task$data(cols = task$target_names), ## response variable
data = task$data(cols = task$feature_names), ## predictor variables
f = maxnet.formula(p = task$data(cols = task$target_names), data = task$data(cols = task$feature_names), classes = pv$classes),
regmult = pv$regmult,
regfun = maxnet.default.regularization,
addsamplestobackground = pv$addsamplestobackground
)
},
## Define the predict function
.predict = function(task){
print("code 6")
pv = self$param_set$get_values(tags = "predict")
## Ensure the same column order in training and prediction data
print("code 7")
newdata = mlr3extralearners:::ordered_features(task, self)
## Compute predictions
print("code 8")
prob = invoke(predict, object = self$model, newdata = newdata, clamp = pv$clamp, type = pv$response_type)
list(prob = prob)
}
)
)
Here is a reproducible example to generate the error message:
library(mlr3)
library(mlr3learners)
library(paradox)
library(R6)
library(mlr3misc)
library(maxnet)
source("C:/R_functions/LearnerClassifMaxnet.R")
data("bradypus")
bradypus_data <- bradypus[, !(colnames(bradypus) %in% c("ecoreg"))]
bradypus_data$presence <- as.factor(bradypus_data$presence)
bradypus_task <- mlr3::as_task_classif(x = bradypus_data, target = "presence", positive = "1")
## summary(bradypus_task)
learner_test = LearnerClassifMaxnet$new()
learner_test$train(bradypus_task)
p = learner_test$predict(bradypus_task)