R rfe function “caret” Package error: there should be the same number of samples in x and y

孤者浪人 提交于 2019-12-01 21:26:20

There is a convention that rows are observations and columns are features. The way you provided x argument to rfe means you have 2901 observations, which produces a mismatch with 15 outcomes. Use transpose function t on your data (if it has 15 columns of course).

The y = c(1,1,1...) vector shouldn't be called predictor. It is dependent variable or outcome. First argument is a data.frame of predictor variables.

We don't know your data, but this works with simulated data:

set.seed(7)
d=data.frame(matrix(rnorm(2901*15,1,.5),ncol=15))
#something like dependent variable
dp=factor(sample(c(1,1,1,1, 1, 1,2,2,2, 3 ,3,3,4, 4, 4),2901,replace = TRUE))

# define the control using a random forest selection function
control <- rfeControl(functions=rfFuncs, method="cv", number=10)

# run the RFE algorithm
sz=50 # Change sz to 2901 for full sample
results <- rfe(d[1:sz, ],   dp[1:sz],   sizes=c(1:15), rfeControl=control)

# summarize the results
print(results)
## End of the printed results
## The top 5 variables (out of 6):
##   X5, X6, X15, X14, X3
horia mihai popescu
rfe(x, y,sizes = subsets, rfeControl = ctrl)

Your problem is that you dont have the nr of rows of x as same length of the vector y

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!