0


I am trying to build a training and test model from a data frame. I am using random forest method and building the model.
My data frame has 6 columns where the first column is the outcome (yes/no) and rest 5 columns are independent variables. I build the model using the all the independent variables (outcome ~ ., data =training, method="rf"). What I am trying to do is to try all the possible combinations of the independent variables and generate a model (like col1, col1 + col2, col1+col2+col3 and so on..) there will be 120 combinations possible with my data frame.
I need help to do this automatically in loop but am not able to figure out how I can put the loop together. Thanks in advance.

data <- read.csv("temp.csv", header = TRUE)
splitdata <- createDataPartition(y=data$var1, p=0.75)
training <- df[splitdata,]
testing  <- df[-splitdata,]
no_of_cols <- 2:ncol(training)
for (i in no_col)
{
   permn (no_of_col)
   #model <- train(var1 ~ ., data=training,  method="rf")
    model[i] <- train(var1 ~ training[,i], data=training,  method="rf")        
    predictions[i] <- predict(model[i],newdata=testing)
} 
rkg
  • 27
  • 6

1 Answers1

0

I believe all you need is a quick fix of your for loop:

for (i in 1:no_col)
{
  code
} 
  • Thanks Aristides for your suggestions. I used the above fix, now getting errors: Error in UseMethod("predict") : no applicable method for 'predict' applied to an object of class "character" In addition: There were 29 warnings (use warnings() to see them) – rkg Jun 07 '16 at 20:35
  • well we fixed the for loop, thats good. With your other errors, maybe this similar question might help you out: http://stackoverflow.com/questions/20346633/prediction-using-saved-model-object – Aristides Mairena Jun 08 '16 at 12:45