Reputation: 109
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.
Upvotes: 2
Views: 1151
Reputation: 93851
There are two issues to address:
First, glm
can only do logistic regression with a two-level outcome. If you provide an outcome variable with more than two levels, it lumps all levels except the first (reference) level into the second level. To do multinomial regression, you'll need to use a different function (there are several packages that can do multinomial regression, for example, polr
from the MASS
package, multinom
from the nnet
package (see @Stupidwolf's answer), vglm
from the VGAM
package; also see the mlogit
and gmnl
packages).
Second, predict(m1, newdata)
provides predictions on the log-odds scale. To get predictions on the probability scale, you would need to do predict(m1, newdata, type="response")
. Similar options (though possibly with different names for the arguments and/or argument options) will need to be set to get predictions on the probability scale for multinomial models.
Upvotes: 2
Reputation: 46948
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
Upvotes: 0