9

I am using the multinomial distribution from the gbm package in R. When I use the predict function, I get a series of values:

5.086328 -4.738346 -8.492738 -5.980720 -4.351102 -4.738044 -3.220387 -4.732654

but I want to get the probability of each class occurring. How do I recover the probabilities? Thank You.

smci
  • 32,567
  • 20
  • 113
  • 146
Jim Johnson
  • 93
  • 1
  • 4
  • 3
    (There is no "GBM" package. The case of letters is important in R.) If you don't want to produce data then why not produce code that uses sample data from the help pages? – IRTFM Aug 15 '13 at 17:01
  • You've accepted an answer which was factually incorrect for the last 3.5 years. I gave you an answer which actually works. – smci Jan 08 '17 at 21:43

2 Answers2

10

predict.gbm(..., type='response') is not implemented for multinomial, or indeed any distribution other than bernoulli or poisson.

So you have to find the most likely class (apply(.., 1, which.max) on the vector output from prediction), as desertnaut wrote:

preds = predict(your_model, n.trees, newdata=...,type='response')

pred_class <- apply(preds, 1, which.max)

Just write a wrapper which accepts type='response' and returns this when it's a multinomial model.

Community
  • 1
  • 1
smci
  • 32,567
  • 20
  • 113
  • 146
1

Take a look at ?predict.gbm, you'll see that there is a "type" parameter to the function. Try out predict(<gbm object>, <new data>, type="response").

David
  • 9,284
  • 3
  • 41
  • 40
  • 2
    No, predict.gbm(..., type="response") is not implemented for multinomial, or indeed any distribution other than bernoulli or poisson. – smci May 31 '15 at 20:49
  • 1
    Per @smci, currently the latest version of GBM is 2.1.1 released in May 2015, and `type="response"` is still not implemented. @smci's suggested use of `apply` below works, though. – morten Jan 04 '17 at 18:51