多カテゴリへの回帰

二値ならbinaryだが多カテゴリのときにどうするか、ということでnnet::multinommlogit::mlogitが使えるがnnet::multinomのほうが使いやすかった。

m <- nnet::multinom(Species ~ ., data=iris)
# weights:  18 (10 variable)
initial  value 164.791843 
iter  10 value 16.177348
iter  20 value 7.111438
iter  30 value 6.182999
iter  40 value 5.984028
iter  50 value 5.961278
iter  60 value 5.954900
iter  70 value 5.951851
iter  80 value 5.950343
iter  90 value 5.949904
iter 100 value 5.949867
final  value 5.949867 
stopped after 100 iterations
Call:
nnet::multinom(formula = Species ~ ., data = iris)

Coefficients:
           (Intercept) Sepal.Length Sepal.Width Petal.Length Petal.Width
versicolor    18.69037    -5.458424   -8.707401     14.24477   -3.097684
virginica    -23.83628    -7.923634  -15.370769     23.65978   15.135301

Residual Deviance: 11.89973 
AIC: 31.89973 
predict(m, data=iris, type="prob")
          setosa   versicolor    virginica
1   1.000000e+00 1.526406e-09 2.716417e-36
2   9.999996e-01 3.536476e-07 2.883729e-32
3   1.000000e+00 4.443506e-08 6.103424e-34
4   9.999968e-01 3.163905e-06 7.117010e-31
5   1.000000e+00 1.102983e-09 1.289946e-36
6   1.000000e+00 3.521573e-10 1.344907e-35
7   1.000000e+00 4.098064e-08 3.016154e-33
8   1.000000e+00 2.615330e-08 2.972971e-34
9   9.999871e-01 1.294210e-05 7.048364e-30
.
.
.