screechOwl
screechOwl

Reputation: 28129

R trying to get caret / rfe to work

I have a data set that I'm trying to use rfe() from the caret package in R on.

x is the prices I'm trying to predict.

y is the variables I'm using to make the prediction.

I can't get rfe to stop giving the following error message:

> 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

Here's some info:

> 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

Also:

> 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")
> 

Any idea why I'm getting the message?

Upvotes: 4

Views: 3154

Answers (2)

Sayed
Sayed

Reputation: 1

It should be factor and vector:

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

In my case column 14 is a class in df.

Upvotes: 0

John Colby
John Colby

Reputation: 22588

y should be a numeric or factor vector. Here you have it as a data frame. Compare:

> 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

vs.

> 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

Upvotes: 5

Related Questions