I am trying to model a good example of bartMachine
usage in Caret
, and I can't seem to model a bartMachine
with Caret
correctly, can anyone tell me, what is exactly the main error means? or is there a simple reproducible code for BART Modeling?
Here is the snippets I use to model a bartMachine using some dummy data of HouseVotes84 and cars dataset:
library(mlbench)
library(caret)
data("HouseVotes84")
#Using HouseVotes84 as Classification Task Dataset and mtcars as Regression Task Dataset
dummy_data_classif <- HouseVotes84[,2:length(colnames(HouseVotes84))] %>%
mutate_if(is.factor, as.numeric)
dummy_data_classif <- data.frame(cbind(Class=HouseVotes84[,1], dummy_data_classif))
dummy_data_classif[is.na(dummy_data_classif)] <- 0
data("cars")
dummy_data_regr <- cars
caret_method_tester <- function(dummy_data, formula, resample_plan=1,
test_method, time_limit=30,
grid_param=c(), parallel_mode=FALSE){
library(caret)
library(R.utils)
formula <- as.formula(formula)
resampling <- NULL
if(resample_plan==1){
resampling <- trainControl(method = "repeatedcv",
number = 10,
repeats = 5,
allowParallel = parallel_mode)
}
else if(resample_plan==2){
resampling <- trainControl(method = "cv",
number = 5,
allowParallel = parallel_mode)
}
else if(resample_plan==3){
resampling <- trainControl(method = "adaptive_cv",
number = 10, repeats = 5,
allowParallel = parallel_mode,
adaptive = list(min = 3, alpha = 0.05,
method = "BT", complete = FALSE))
}
else if(resample_plan==4){
resampling <- trainControl(method = "boot",
number = 5,
allowParallel = parallel_mode)
}
else if(resample_plan==5){
resampling <- trainControl(method = "boot_all",
number = 5,
allowParallel = parallel_mode)
}
tryCatch(
expr={
if(length(grid_param) > 0){
withTimeout(
model <- caret::train(formula,
data = dummy_data,
method = test_method,
trControl = resampling,
tuneGrid=grid_param), timeout = 300
)
}
else{
withTimeout(
model <- caret::train(formula,
data = dummy_data,
method = test_method,
trControl = resampling), timeout=300
)
}
return(model)
},
error=function(cond){
message("Test Model Failed")
message("Here's the original error message:")
message(cond)
return(NULL)
},
warning=function(cond){
message("Warning Triggered!")
message("Here's the original warning message:")
message(cond)
return(model)
}
)
}
bart_reg <- caret_method_tester(dummy_data_regr, "Price ~ .",
test_method="bartMachine", time_limit=30, resample_plan=2)
Test Model Failed
Here's the original error message:
argument is of length zero
bart_classif <- caret_method_tester(dummy_data_classif, "Class ~ .",
test_method="bartMachine", time_limit=30, resample_plan=2)
Test Model Failed
Here's the original error message:
incorrect number of dimensions
I used try Catch method to easily notify things about the code progress, so it is clear when the code fails, issues warning, or is successful.
the dataset also doesn't have any NA Values as far as I am concerned
Would be much better if you reduce the code to the essential part, basically the train function with
bartMachine
doesn't work. We can illustrate that with this example and we get the same error message:The error is a bug with the code in
caret
, if you don't provide the tuning grid, the default function used to create it does not return a data.frame:You can either provide a tune grid:
Or you fix the function above and create a new method, you can read more here: