使用 glmnet 和 2 个类别时,插入符号中的训练函数出错

发布于 2024-12-20 09:32:59 字数 574 浏览 0 评论 0原文

以下代码块失败了,我无法辨别原因。

library(caret)
data(iris)
TrainData <- iris[,1:4]
TrainClasses <- factor(ifelse(iris[,5]=='versicolor','versicolor','other'))
model1 <- train(TrainData,TrainClasses,method='glmnet')

出现以下错误:

Error in { : task 1 failed - "'n' must be a positive integer >= 'x'"

如果我使用不同的模型,例如 glm ,它运行正常。如果我使用 3 个类,TrainClasses <- iris[,5],它也可以正常工作。

那么 2 个类是唯一导致 glmnet 方法失败的吗?

这是 Windows 上的 R 版本 2.14.0,插入符版本 5.09-006。同样的错误发生在我的 mac 和 linux 上。

The following block of code fails, for no reason I can discern.

library(caret)
data(iris)
TrainData <- iris[,1:4]
TrainClasses <- factor(ifelse(iris[,5]=='versicolor','versicolor','other'))
model1 <- train(TrainData,TrainClasses,method='glmnet')

With the following error:

Error in { : task 1 failed - "'n' must be a positive integer >= 'x'"

If I sub in a different model, such as glm it runs fine. If I uses 3 classes, TrainClasses <- iris[,5], it also works fine.

What about 2 classes is uniquely causing the glmnet method to fail?

This is R version 2.14.0, caret version 5.09-006, on windows. The same error happens on my mac and on linux.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痕至 2024-12-27 09:32:59

我无法回答您为什么会收到错误(因为代码在我的机器上运行良好),但我建议您遵循 R-hel 发布指南中的建议,并包含有关您的版本和版本的更多详细信息设置:

> model1
150 samples
  4 predictors
  2 classes: 'other', 'versicolor' 

No pre-processing
Resampling: Bootstrap (25 reps) 

Summary of sample sizes: 150, 150, 150, 150, 150, 150, ... 

Resampling results across tuning parameters:

  alpha  lambda  Accuracy  Kappa   Accuracy SD  Kappa SD
  0.1    0.1     0.698     0.19    0.0419       0.0891  
  0.1    0.462   0.675     0.0311  0.0399       0.0719  
# >>> snipped the rest of a page of code

我有一个相当完整的会话,而你的肯定会有所不同。由于函数被会话中稍后加载的其他包屏蔽,可能会出现冲突。刚才加载“caret”时出现了不少警告。

> sessionInfo()
R version 2.14.0 Patched (2011-11-13 r57650)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
 [1] tools     stats4    grid      splines   stats     graphics  grDevices utils    
 [9] datasets  methods   base     

other attached packages:
 [1] glmnet_1.7.1        Matrix_1.0-1        e1071_1.6           class_7.3-3        
 [5] caret_5.09-012      foreach_1.3.2       codetools_0.2-8     iterators_1.0.5    
 [9] cluster_1.14.1      mlogit_0.2-1        maxLik_1.0-2        miscTools_0.6-10   
[13] lmtest_0.9-29       statmod_1.4.13      Formula_1.0-1       mvbutils_2.5.101   
[17] data.table_1.7.1    party_0.9-99995     vcd_1.2-12          colorspace_1.1-0   
[21] strucchange_1.4-6   sandwich_2.2-8      coin_1.0-20         modeltools_0.2-18  
[25] lubridate_0.2.5     quantreg_4.71       SparseM_0.89        raster_1.9-41      
[29] MASS_7.3-16         ks_1.8.4            misc3d_0.8-1        rgl_0.92.798       
[33] mvtnorm_0.9-9991    KernSmooth_2.23-7   sp_0.9-91           latticeExtra_0.6-19
[37] RColorBrewer_1.0-5  zoo_1.7-6           ggplot2_0.8.9       proto_0.3-9.2      
[41] reshape_0.8.4       plyr_1.6            rms_3.3-2           Hmisc_3.9-0        
[45] survival_2.36-10    sos_1.3-1           brew_1.0-6          lattice_0.20-0     

loaded via a namespace (and not attached):
[1] compiler_2.14.0 digest_0.5.1    stringr_0.5 

I'm not able to give you an answer about why you are getting an error (since the code runs fine on my machine) but I will suggest you following the advice in the R-hel Posting Guide and include more details about your versions and set up.:

> model1
150 samples
  4 predictors
  2 classes: 'other', 'versicolor' 

No pre-processing
Resampling: Bootstrap (25 reps) 

Summary of sample sizes: 150, 150, 150, 150, 150, 150, ... 

Resampling results across tuning parameters:

  alpha  lambda  Accuracy  Kappa   Accuracy SD  Kappa SD
  0.1    0.1     0.698     0.19    0.0419       0.0891  
  0.1    0.462   0.675     0.0311  0.0399       0.0719  
# >>> snipped the rest of a page of code

I have a fairly full session and yours will surely have been different. There can be conflicts that arise because of functions having been masked by other packages that were loaded later in a session. There were quite a few warnings when I loaded "caret" just now.

> sessionInfo()
R version 2.14.0 Patched (2011-11-13 r57650)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
 [1] tools     stats4    grid      splines   stats     graphics  grDevices utils    
 [9] datasets  methods   base     

other attached packages:
 [1] glmnet_1.7.1        Matrix_1.0-1        e1071_1.6           class_7.3-3        
 [5] caret_5.09-012      foreach_1.3.2       codetools_0.2-8     iterators_1.0.5    
 [9] cluster_1.14.1      mlogit_0.2-1        maxLik_1.0-2        miscTools_0.6-10   
[13] lmtest_0.9-29       statmod_1.4.13      Formula_1.0-1       mvbutils_2.5.101   
[17] data.table_1.7.1    party_0.9-99995     vcd_1.2-12          colorspace_1.1-0   
[21] strucchange_1.4-6   sandwich_2.2-8      coin_1.0-20         modeltools_0.2-18  
[25] lubridate_0.2.5     quantreg_4.71       SparseM_0.89        raster_1.9-41      
[29] MASS_7.3-16         ks_1.8.4            misc3d_0.8-1        rgl_0.92.798       
[33] mvtnorm_0.9-9991    KernSmooth_2.23-7   sp_0.9-91           latticeExtra_0.6-19
[37] RColorBrewer_1.0-5  zoo_1.7-6           ggplot2_0.8.9       proto_0.3-9.2      
[41] reshape_0.8.4       plyr_1.6            rms_3.3-2           Hmisc_3.9-0        
[45] survival_2.36-10    sos_1.3-1           brew_1.0-6          lattice_0.20-0     

loaded via a namespace (and not attached):
[1] compiler_2.14.0 digest_0.5.1    stringr_0.5 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文