caret::train: 指定模型生成参数 [英] caret::train: specify model-generation-parameters

查看:34
本文介绍了caret::train: 指定模型生成参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 R 中使用 caret 库来生成模型.我想生成一个 earth(又名 MARS)模型,我想为此模型生成指定 degree 参数.根据文档(第11页)earth 方法支持此参数.

I'm using the caret library in R for model generation. I want to generate an earth (aka MARS) model and I want to specify the degree parameter for this model generation. According to the documentation (page 11) the earth method supports this parameter.

我在指定参数时收到以下错误消息:

I get the following error message when specifying the parameter:

> 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"

在指定 degree 参数时如何避免此错误?

How can I avoid this error when specifying the degree parameter?

> 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   

推荐答案

我找到了方法,joran 引导我走向正确的方向:

I found out how to do it, joran led me into the right direction:

创建一个新函数来生成训练网格.这个函数必须接受两个参数lendata.为了检索原始训练网格,您可以调用 caret 包提供的 createGrid 方法.然后,您可以根据需要修改网格.例如,要使 nprune 参数保持不变并添加 degree 从 1 到 5,请使用以下代码:

Create a new function which generates the training grid. This function must accept the two parameters len and data. In order to retrieve the original training grid, you can call the createGrid method provided by the caret package. You can then modify the grid to your needs. For example to neave the nprune parameter unchanged and add degree from 1 to 5 use the following code:

  createMARSGrid <- function(len, data) {
      g = createGrid("earth", len, data)
      g = expand.grid(.nprune=g$.nprune, .degree=seq(1,5))
      return(g)
  }   

然后像这样调用它:

train(formula, data=data, method='earth', tuneGrid = createMARSGrid)

这篇关于caret::train: 指定模型生成参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆