Search code examples
rmachine-learningglmpredict

Predict a factor of 3 levels and return the % of each factor


I am trying to predict a three-level factor with a set of variables. The group is A, B and C.

m<-glm(as.factor(Group)~Sex+BloodType+Pressure,data=Hel,family = "binomial")

newdata <- data.frame(Sex="M",BloodType="A+", Pressure=80)

predict(m,newdata)

and this returns:

        1 
0.7133324 

I would like that to give me back:

A        B        C
20.00   40.00    40.000

How can I do this? Thanks.


Solution

  • I guess you have to use something that allows multinomial regression like nnet?

    library(nnet)
    trn = sample(1:nrow(iris),100)
    fit_nnet <- multinom(Species ~ ., iris[trn,])
    
    head(predict(fit_nnet,iris[-trn,],type="prob"))
          setosa   versicolor    virginica
    7  1.0000000 2.348893e-11 2.166176e-58
    9  0.9999323 6.765017e-05 2.438304e-52
    10 0.9999940 6.035319e-06 1.530614e-56
    12 1.0000000 1.355834e-09 1.315066e-57