我正在使用caretR中的库来生成模型.我想生成一个earth(又名MARS)模型,我想degree为这个模型生成指定参数.根据文档(第11页),该earth方法支持此参数.
指定参数时,我收到以下错误消息:
> library(caret)
> data(trees)
> train(Volume~Girth+Height, data=trees, method='earth', degree=1)
Error in { :
task 1 failed - "formal argument "degree" matched by multiple actual arguments"
Run Code Online (Sandbox Code Playgroud)
指定degree参数时如何避免此错误?
> sessionInfo()
R version 2.15.0 (2012-03-30)
Platform: x86_64-pc-linux-gnu (64-bit)
locale:
[1] LC_CTYPE=en_GB.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_GB.UTF-8 LC_COLLATE=en_GB.UTF-8
[5] LC_MONETARY=en_GB.UTF-8 LC_MESSAGES=en_GB.UTF-8
[7] LC_PAPER=C LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] earth_3.2-3 plotrix_3.4 plotmo_1.3-1 leaps_2.9 caret_5.15-023
[6] foreach_1.4.0 cluster_1.14.2 reshape_0.8.4 plyr_1.7.1 lattice_0.20-6
loaded via a namespace (and not attached):
[1] codetools_0.2-8 compiler_2.15.0 grid_2.15.0 iterators_1.0.6
[5] tools_2.15.0
Run Code Online (Sandbox Code Playgroud)
jor*_*ran 10
我总是发现插入符号中的函数既有用又有些令人发狂.这是正在发生的事情.
您正在尝试一个参数传递给earth通过...参数train.该文档train包含该参数的描述:
传递给分类或回归例程的参数(例如randomForest).如果在此处传递调整参数的值,则会发生错误.
调整参数,是吗?好吧,如果你向下滚动并检查每个模型类型的正式调整参数列表,你会看到earth它们是,degree和nprune.
所以这里的问题train是设计用于沿着调整参数自动化一些网格搜索,并且该...参数用于将更多参数传递给模型拟合函数,除了那些调整参数.
如果要设置调整参数,则需要使用其他参数,如下所示:
train(Volume~Girth+Height, data=trees, method='earth',
tuneGrid = data.frame(.degree = 1,.nprune = 5))
Run Code Online (Sandbox Code Playgroud)
请注意列如何以前导句点命名.另外,令人沮丧的是,因为earthfor 的默认值nprune是NULL,我不确定你是否可以通过这种方式只传递默认值.(通常,NULL在数据框中设置内容只会删除它们.)
我发现了怎么做,约兰带领我走向了正确的方向:
创建一个生成训练网格的新功能.此函数必须接受两个参数len和data.为了检索原始训练网格,您可以调用包createGrid提供的方法caret.然后,您可以根据需要修改网格.例如,为了使nprune参数保持不变并degree从1 添加到5,请使用以下代码:
createMARSGrid <- function(len, data) {
g = createGrid("earth", len, data)
g = expand.grid(.nprune=g$.nprune, .degree=seq(1,5))
return(g)
}
Run Code Online (Sandbox Code Playgroud)
然后像这样调用它:
train(formula, data=data, method='earth', tuneGrid = createMARSGrid)
Run Code Online (Sandbox Code Playgroud)