首页 文章

在ctree中使用tuneGrid和Controls(Caret)

提问于
浏览
1

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

我收到错误:

在eval(expr,envir,enclos)中:Fold1的模型拟合失败:mincriterion = 0.95,maxdepth = 7(函数(cl,name,valueClass)中的错误:类“numeric”对象的赋值对@'无效类“TreeGrowControl”的对象中的maxdepth';(value,“integer”)不为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 回答

  • 2

    这看起来像是一个可能的错误 . 如果你使用 as.integer() ,你可以使它工作:

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

    Reason: 如果使用 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 .

相关问题