Reputation: 7601
I use the predict function to predict the results based on a model. What I get is a vector of the predicted classes. I want to retrieve the same results but instead of the form
1 class_1
2 class_1
3 class_4
4 class_2
I want to have the results in the form
class_1 class_2 class_3 class_4
1 1 0 0 0
2 1 0 0 0
3 0 0 0 1
4 0 1 0 0
I have tried passing type=class
and type=response
but the results are the same.
I am completely new to R and I am still trying to find my way around R's documentation but I think that this is something trivial that I should be able to figure out, though I am pretty stuck.
Upvotes: 1
Views: 517
Reputation: 8270
After viewing the docs on predict.randomForest at https://cran.r-project.org/web/packages/randomForest/randomForest.pdf
It appears the valid choices for type are response
, prob
. or votes
.
Using the code below, I was able to reproduce your format, but using probabilities.
> predict(model, x, type='prob')
0 1
1 1.000 0.000
2 0.180 0.820
3 0.138 0.862
attr(,"class")
To obtain booleans, another option is you could do one hot encoding on the response results
result_classes = list()
for (level in levels(y)){
result_classes[[level]] <- predict(model, x, type='response') == level
}
data.frame(result_classes )
Result:
X0 X1
1 TRUE FALSE
2 FALSE TRUE
3 FALSE TRUE
Upvotes: 2