我在使用caret
中的awtan
方法创建预测模型时,训练过程持续失败,出现以下错误:
model fit failed for Resample04: score=bic, smooth=1 Error : 'lpawnb' is not an exported object from 'namespace:bnclassify'
我使用的是bnclassify版本0.3.4。根据GitHub上的发布说明,看起来lpawnb()
在0.3.2版本中被lp()
替换了,所以我初步猜测问题是某种遗留的错误(在caret中?在bnclassify中?),导致调用了后者而不是前者。
另一方面,或许是我自己操作有误。以下是一个示例代码:
library(caret)library(bnclassify)data <- structure(list(var1 = structure(c(2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("0", "1"), class = "factor"), var2 = structure(c(2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 2L), .Label = c("0", "1"), class = "factor"), var3 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L), .Label = c("0", "1"), class = "factor"), var4 = structure(c(2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L), .Label = c("0", "1"), class = "factor"), var5 = structure(c(2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("0", "1"), class = "factor"), var6 = structure(c(2L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L), .Label = c("0", "1"), class = "factor"), var7 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L), .Label = c("0", "1"), class = "factor"), var8 = structure(c(2L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 2L), .Label = c("0", "1"), class = "factor"), outcome = structure(c(2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L ), .Label = c("0", "1"), class = "factor")), .Names = c("var1", "var2", "var3", "var4", "var5", "var6", "var7", "var8", "outcome" ), row.names = c(NA, 20L), class = "data.frame") model <- train(x = data %>% select(-outcome), y = data$outcome, method = 'awtan')# 最终会出现以下错误## 出了问题;所有准确率指标值都缺失:# Accuracy Kappa # Min. : NA Min. : NA # 1st Qu.: NA 1st Qu.: NA # Median : NA Median : NA # Mean :NaN Mean :NaN # 3rd Qu.: NA 3rd Qu.: NA # Max. : NA Max. : NA # NA's :6 NA's :6 # 错误:停止# 此外:有50个或更多的警告(使用warnings()查看前50个)# # # > warnings()# 警告信息:# 1: model fit failed for Resample01: score=loglik, smooth=1 Error : 'lpawnb' is not an exported object from 'namespace:bnclassify'# # 2: model fit failed for Resample01: score=bic, smooth=1 Error : 'lpawnb' is not an exported object from 'namespace:bnclassify'# # 3: model fit failed for Resample01: score=aic, smooth=1 Error : 'lpawnb' is not an exported object from 'namespace:bnclassify'
使用相同的数据,我能够使用bnclassify
的函数构建模型,所以我猜测这是caret
中的一个错误,在调用lpawnb()
时应该使用bnclassify::lp()
,但我还不清楚如何确认这一点。
在责怪比我聪明得多的包开发者之前,有人能帮我解释一下我可能做错了什么吗?
回答:
看起来CRAN上当前版本的caret,6.0-78,使用bnclassify::lpawnb()
定义了awtan模型的拟合函数,而bnclassify已经用lp()
替换了它:
getModelInfo('awtan')... $awtan$fitfunction (x, y, wts, param, lev, last, classProbs, ...) { dat <- if (is.data.frame(x)) x else as.data.frame(x) dat$.outcome <- y struct <- bnclassify::tan_cl(class = ".outcome", dataset = dat, score = as.character(param$score)) bnclassify::lpawnb(struct, dat, smooth = param$smooth, trees = 10, bootstrap_size = 0.5, ...)}...