2

Consider the code:

require(rpart)
require(gbm)

fit = gbm(formula=Kyphosis~.,data=kyphosis,distribution="multinomial")
p   = predict.gbm(fit,kyphosis,n.trees=100)

Results:

enter image description here

Is there any way to directly get the predicted classes, e.g. c(absent,absent,...)

I can do this with the following code:

colnames(p)[apply(p,1,function(i){which(i==max(i))})]

Results:

[1] "absent" "absent" "absent" "absent" ...

However, this always returns strings. I would like the returned type to be the same as the predictor type (e.g. if the predictor was of type factor, I would like the result to be factor, or if it was integer, the result should be integer, etc...). Is there any default function that returns the predicted classes?

Example of desired behavior:

require(nnet)
fit = multinom(formula=Kyphosis~.,data=kyphosis)
predict(fit,kyphosis)
dlaser
  • 1,295
  • 1
  • 11
  • 15

0 Answers0