Anubhav Dikshit
Anubhav Dikshit

Reputation: 1829

output the predicted value of random forest as a new column in the test dataset

I ran a random forest on a training data set (60% of the parent data set).

I used the "predict" function in R to evaluate how well the model performs on the test data set (40% of the parent data set)

I used

 require(randomForest)

 trained_model <- randomForest(y~.,data = training,
                                   importance=TRUE, 
                                   keep.forest=TRUE)

 result <- predict(trained_model, type = response, newdata=testing[-17])

removed 17th column since it's "y"

The problem is that the "result" is an array, this there a way to add this predicted value back into the testing data set as a new column

Something like

Id     x1     x2 ......   Xn    y     predicted
1      0.1    0.12        23    no    no 

Upvotes: 2

Views: 1930

Answers (1)

TheMI
TheMI

Reputation: 1751

Do this:

testing[ ,(ncol(testing)+1)] <- result

When result will added back to testing dataset, there will be a column not array.

Upvotes: 1

Related Questions