1

这很简单,下面的 rfe 函数抛出此错误:“rfe.default 中的错误(预测器,as.vector(结果),大小 = c(5),rfeControl = rfeControl(函数 =“lmFuncs”,:应该有x 和 y 的样本数相同"

df 第一列是具有 2 个级别的因子。其余的 df 列是数字的。没有 NA 值。

我一点也不知道这个错误是关于什么的。我已经尝试了其他一些解决方案无济于事。

rfe_linear <- caret::rfe(
  df[ , -1 ],
  df[ , 1 ],
  sizes = c( 5 ),
  rfeControl = rfeControl(
    functions = 'lmFuncs',
    method = 'boot',
    number = 20
  )
)

我尝试了以下,它给出了同样的错误。

rfe_linear <- caret::rfe(
  as.matrix( df[ , -1 ] ),
  df[ , 1 ],
  sizes = c( 5 ),
  rfeControl = rfeControl(
    functions = 'lmFuncs',
    method = 'boot',
    number = 20
  )
)

尝试了以下。引发此错误:“错误:必须在 中使用向量[,而不是类矩阵的对象。”

rfe_linear <- caret::rfe(
  as.matrix( df[ , -1 ] ),
  as.factor( df[ , 1 ] ),
  sizes = c( 5 ),
  rfeControl = rfeControl(
    functions = 'lmFuncs',
    method = 'boot',
    number = 20
  )
)

还尝试了以下方法,抛出此错误:“错误:$ 运算符对原子向量无效”。说我疯了,但我在代码中的任何地方都没有看到 $ 运算符。

rfe_linear <- caret::rfe(
  df[ , -1 ],
  df[[ 1 ]],
  sizes = c( 5 ),
  rfeControl = rfeControl(
    functions = 'lmFuncs',
    method = 'boot',
    number = 20
  )
)

我已经尝试过 as.factor()、as.data.frame()、as.matrix()、df[、1]、df[、-1]、df[、2:ncol(df)] 的所有组合, 和可以想到的 df[, 1:1 ]。

所以我尝试了这个:

rfe_linear <- caret::rfe(
  df[ , -1 ],
  df$Phenotype,
  sizes = c( 5 ),
  rfeControl = rfeControl(
    functions = 'lmFuncs',
    method = 'boot',
    number = 20
  )
)

这似乎运行了一段时间,但是当然,R 不会让我轻易地运行 rfe(),所以在日志的末尾它又抛出了另一个错误并且 rfe_linear 对象仍然没有找到.

+(rfe) fit Resample01 size: 5191 
-(rfe) fit Resample01 size: 5191 
+(rfe) imp Resample01 
-(rfe) imp Resample01 
+(rfe) fit Resample02 size: 5191 
-(rfe) fit Resample02 size: 5191 
+(rfe) imp Resample02 
-(rfe) imp Resample02 
+(rfe) fit Resample03 size: 5191 
-(rfe) fit Resample03 size: 5191 
+(rfe) imp Resample03 
-(rfe) imp Resample03 
+(rfe) fit Resample04 size: 5191 
-(rfe) fit Resample04 size: 5191 
+(rfe) imp Resample04 
-(rfe) imp Resample04 
+(rfe) fit Resample05 size: 5191 
-(rfe) fit Resample05 size: 5191 
+(rfe) imp Resample05 
-(rfe) imp Resample05 
+(rfe) fit Resample06 size: 5191 
-(rfe) fit Resample06 size: 5191 
+(rfe) imp Resample06 
-(rfe) imp Resample06 
+(rfe) fit Resample07 size: 5191 
-(rfe) fit Resample07 size: 5191 
+(rfe) imp Resample07 
-(rfe) imp Resample07 
+(rfe) fit Resample08 size: 5191 
-(rfe) fit Resample08 size: 5191 
+(rfe) imp Resample08 
-(rfe) imp Resample08 
+(rfe) fit Resample09 size: 5191 
-(rfe) fit Resample09 size: 5191 
+(rfe) imp Resample09 
-(rfe) imp Resample09 
+(rfe) fit Resample10 size: 5191 
-(rfe) fit Resample10 size: 5191 
+(rfe) imp Resample10 
-(rfe) imp Resample10 
+(rfe) fit Resample11 size: 5191 
-(rfe) fit Resample11 size: 5191 
+(rfe) imp Resample11 
-(rfe) imp Resample11 
+(rfe) fit Resample12 size: 5191 
-(rfe) fit Resample12 size: 5191 
+(rfe) imp Resample12 
-(rfe) imp Resample12 
+(rfe) fit Resample13 size: 5191 
-(rfe) fit Resample13 size: 5191 
+(rfe) imp Resample13 
-(rfe) imp Resample13 
+(rfe) fit Resample14 size: 5191 
-(rfe) fit Resample14 size: 5191 
+(rfe) imp Resample14 
-(rfe) imp Resample14 
+(rfe) fit Resample15 size: 5191 
-(rfe) fit Resample15 size: 5191 
+(rfe) imp Resample15 
-(rfe) imp Resample15 
+(rfe) fit Resample16 size: 5191 
-(rfe) fit Resample16 size: 5191 
+(rfe) imp Resample16 
-(rfe) imp Resample16 
+(rfe) fit Resample17 size: 5191 
-(rfe) fit Resample17 size: 5191 
+(rfe) imp Resample17 
-(rfe) imp Resample17 
+(rfe) fit Resample18 size: 5191 
-(rfe) fit Resample18 size: 5191 
+(rfe) imp Resample18 
-(rfe) imp Resample18 
+(rfe) fit Resample19 size: 5191 
-(rfe) fit Resample19 size: 5191 
+(rfe) imp Resample19 
-(rfe) imp Resample19 
+(rfe) fit Resample20 size: 5191 
-(rfe) fit Resample20 size: 5191 
+(rfe) imp Resample20 
-(rfe) imp Resample20 
Error in { : task 1 failed - "replacement has 1 row, data has 0"
In addition: There were 50 or more warnings (use warnings() to see the first 50)
4

1 回答 1

1

lmFuncsy当是一个因素时不起作用。rfFuncs工作,例如。

library(caret)
library(randomForest)

df <- data.frame(y=factor(rnorm(100)>0), 
                 x1=rnorm(100), x2=rnorm(100), x3=rnorm(100), x4=rnorm(100), x5=rnorm(100),
                 x6=rnorm(100), x7=rnorm(100), x8=rnorm(100), x9=rnorm(100), x10=rnorm(100))

rfe_linear <- caret::rfe(
    x = df[,-1],
    y = df[,1],
    sizes = 5,
    rfeControl = rfeControl(
        functions = rfFuncs,
        method = 'boot',
        number = 20
    )
)

rfe_linear
#> 
#> Recursive feature selection
#> 
#> Outer resampling method: Bootstrapped (20 reps) 
#> 
#> Resampling performance over subset size:
#> 
#>  Variables Accuracy   Kappa AccuracySD KappaSD Selected
#>          5   0.5398 0.09997    0.08876  0.1514         
#>         10   0.5478 0.10734    0.07928  0.1433        *
#> 
#> The top 5 variables (out of 10):
#>    x8, x2, x4, x10, x9

reprex 包(v0.2.1)于 2019 年 4 月 26 日创建

于 2019-04-25T20:21:47.920 回答