我正在尝试为bartMachine
in 中的一个很好的用法示例建模Caret
,但我似乎无法正确地为 a 建模bartMachine
,Caret
谁能告诉我,主要错误到底是什么意思?或者是否有用于 BART 建模的简单可重现代码?
这是我使用 HouseVotes84 和汽车数据集的一些虚拟数据对 bartMachine 建模的片段:
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
我使用 try Catch 方法轻松地通知有关代码进度的事情,因此代码失败、发出警告或成功时很清楚。
就我而言,数据集也没有任何 NA 值