【问题标题】:R trying to get caret / rfe to workR试图让插入符号/ rfe工作
【发布时间】:2018-12-01 12:49:49
【问题描述】:

我有一个数据集,我正在尝试使用 R on 中 caret 包中的 rfe()

x 是我要预测的价格。

y 是我用来进行预测的变量。

我无法让 rfe 停止给出以下错误消息:

> lmProfile2 <- rfe(x1,y1,
+ sizes = subsets,
+ rfeControl = ctrl)
Error in rfe.default(x1, y1, sizes = subsets, rfeControl = ctrl) : 
  there should be the same number of samples in x and y

这里有一些信息:

> class(x1)
[1] "data.frame"
> class(y1)
[1] "data.frame"
> nrow(x1)
[1] 500
> nrow(y1)
[1] 500
> ncol(x1)
[1] 68
> ncol(y1)
[1] 1

还有:

> y1 <- data.frame(y = tiny4[,2])
> x1 <- data.frame(tiny4[,-c(1,2)])
> subsets <- c(5,10)
> 
> ctrl <- rfeControl(functions = lmFuncs,
+ method = "cv",
+ verbose = FALSE,
+ returnResamp = "final")
> 

知道为什么我会收到这条消息吗?

【问题讨论】:

    标签: r


    【解决方案1】:

    y 应该是数字或因子向量。在这里,您将其作为数据框。比较:

    > rfe(data.frame(matrix(rnorm(100*3), ncol=3)), sample(2, 100, replace=T), sizes=1:3, rfeControl=rfeControl(functions=lmFuncs))
    
    Recursive feature selection
    
    Outer resampling method: Bootstrap (25 reps) 
    
    Resampling performance over subset size:
    
     Variables   RMSE Rsquared  RMSESD RsquaredSD Selected
             1 0.5154  0.02120 0.02421    0.02752        *
             2 0.5162  0.02295 0.02722    0.03204         
             3 0.5162  0.02295 0.02722    0.03204         
    
    The top 1 variables (out of 1):
       X3
    

    对比

    > rfe(data.frame(matrix(rnorm(100*3), ncol=3)), data.frame(sample(2, 100, replace=T)), sizes=1:3, rfeControl=rfeControl(functions=lmFuncs))
    Error in rfe.default(data.frame(matrix(rnorm(100 * 3), ncol = 3)), data.frame(sample(2,  : 
      there should be the same number of samples in x and y
    

    【讨论】:

    • 做到了。非常感谢。
    【解决方案2】:

    应该是因子和向量:

    as.factor(noquote(as.vector(t(df[,14])))) 
    

    在我的例子中,第 14 列是 df 中的一个类。

    【讨论】:

      猜你喜欢
      • 2019-06-02
      • 2016-04-26
      • 2014-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多