在ctree中使用tuneGrid和Controls(Caret)

时间:2016-12-22 18:32:26

标签: r r-caret party

在使用插入符号中的tuneGrid和controls选项时,我遇到了一个问题。在这个例子中,我想设置mincriterion和max depth,但是也想指定min bucket大小。当任何选项传递给ctree_control()时,似乎会发生此错误。

我收到错误:

在eval(expr,envir,enclos)中:   Fold1的模型拟合失败:mincriterion = 0.95,maxdepth = 7(函数(cl,name,valueClass)中的错误:   在“TreeGrowControl”类的对象中,“numeric”类对象的赋值对@'maxdepth'无效;是(值,"整数")不是TRUE"

这可以通过运行来复制:

library(caret)
data("GermanCredit")

trainCtrl <- trainControl(method = 'cv', number = 2, sampling = 'down', 
verboseIter = FALSE, allowParallel = FALSE, classProbs = TRUE, 
                      summaryFunction = twoClassSummary)

tune <- expand.grid(.mincriterion = .95, .maxdepth = seq(5, 10, 2))

ctree_fit <- train(Class ~ ., data = GermanCredit, 
method = 'ctree2', trControl = trainCtrl, metric = "Sens", 
tuneGrid = tune, controls = ctree_control(minbucket = 10))

我正在根据这里发布的答案尝试这种方法: Run cforest with controls = cforest_unbiased() using caret package

根据错误的外观,它与插入符号如何将最大深度传递给ctree有关,但我不确定是否还有这个问题。直接使用ctree_control运行ctree可以正常工作。

非常感谢任何帮助

1 个答案:

答案 0 :(得分:2)

这看起来像是一个可能的错误。如果您使用as.integer()

,则可以使其正常工作
tune <- expand.grid(.mincriterion = .95, 
                    .maxdepth = as.integer(seq(5, 10, 2)))

原因:如果您使用controls参数,那么插入素就是

theDots$controls@tgctrl@maxdepth <- param$maxdepth
theDots$controls@gtctrl@mincriterion <- param$mincriterion
ctl <- theDots$controls

如果我们看一下treeControl类看起来像这样

Formal class 'TreeControl' [package "party"] with 4 slots
  ..@ varctrl  :Formal class 'VariableControl' [package 
  ..@ tgctrl   :Formal class 'TreeGrowControl' [package "party"] with 4 slots

[left stuff out]

  .. .. ..@ stump         : logi FALSE
  .. .. ..@ maxdepth      : int 0
  .. .. ..@ savesplitstats: logi TRUE
  .. .. ..@ remove_weights: logi FALSE 

因此它期望maxdepth为整数,并且插入符号尝试分配数字(可以是整数但不是类整数),但仅在指定controls时。

如果您未指定controls,则

ctl <- do.call(getFromNamespace("ctree_control", "party"), 
                                      list(maxdepth = param$maxdepth,
                                           mincriterion = param$mincriterion))

...然后从那里以一种我不完全理解的方式,仅仅通过现在查看来源。如果您有兴趣,请查看https://github.com/topepo/caret/blob/master/models/files/ctree2.R