尝试使用caret/rfe工具的R语言问题

4

我有一个数据集,想在R中使用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")
> 

有任何想法为什么我会收到这个消息?
2个回答

5

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

0

它应该是因子和向量:

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

在我的情况下,第14列是df中的一个类。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接