How to create a confusion matrix after mlogit?

人走茶凉 提交于 2020-02-02 21:31:19

问题


I have a categorical variable n_produttore: A-B-C-D-E-F and the output of multinomial logit (mlogit).

How do I create the confusion matrix?

mlogit n_produttore UVAtevola NUTILIZZODIVOLTE EXPOSTFIORITURA DIMENSIONE DOSI


Iteration 0:   log likelihood = -898.93386  
Iteration 1:   log likelihood = -868.27679  
Iteration 2:   log likelihood = -864.38774  
Iteration 3:   log likelihood = -864.28614  
Iteration 4:   log likelihood = -864.26279  
Iteration 5:   log likelihood = -864.25805  
Iteration 6:   log likelihood = -864.25724  
Iteration 7:   log likelihood = -864.25705  
Iteration 8:   log likelihood = -864.25701  
Iteration 9:   log likelihood =   -864.257  

Multinomial logistic regression                   Number of obs   =        929
                                                  LR chi2(25)     =      69.35
                                                  Prob > chi2     =     0.0000
Log likelihood =   -864.257                       Pseudo R2       =     0.0386

----------------------------------------------------------------------------------
    n_produttore |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-----------------+----------------------------------------------------------------
ALTRO            |  (base outcome)
-----------------+----------------------------------------------------------------
A                |
       UVAtevola |  -.1579633   .3240817    -0.49   0.626    -.7931517    .4772251
NUTILIZZODIVOLTE |  -.2306291   .0957196    -2.41   0.016     -.418236   -.0430221
 EXPOSTFIORITURA |  -.1879822   .2277447    -0.83   0.409    -.6343536    .2583893
      DIMENSIONE |  -.0621528    .022512    -2.76   0.006    -.1062755     -.01803
            DOSI |   .0749472   .0469926     1.59   0.111    -.0171565     .167051
           _cons |  -.9914935   .2967274    -3.34   0.001    -1.573068   -.4099185
-----------------+----------------------------------------------------------------
B                |
       UVAtevola |  -1.125263   .5444485    -2.07   0.039    -2.192362   -.0581633
NUTILIZZODIVOLTE |  -.0667538   .0966905    -0.69   0.490    -.2562637    .1227562
 EXPOSTFIORITURA |   -.769514   .2891922    -2.66   0.008     -1.33632   -.2027077
      DIMENSIONE |  -.0293445    .022586    -1.30   0.194    -.0736122    .0149232
            DOSI |  -.0451004   .1109894    -0.41   0.684    -.2626356    .1724349
           _cons |  -1.361353   .3900545    -3.49   0.000    -2.125846   -.5968602
-----------------+----------------------------------------------------------------
C                |
       UVAtevola |  -1.232848   1.075072    -1.15   0.251     -3.33995    .8742545
NUTILIZZODIVOLTE |  -.1639186   .2256885    -0.73   0.468    -.6062599    .2784227
 EXPOSTFIORITURA |   -.154228   .5543342    -0.28   0.781    -1.240703    .9322469
      DIMENSIONE |  -.0993675   .0590232    -1.68   0.092    -.2150508    .0163159
            DOSI |   .0816812   .1273864     0.64   0.521    -.1679916    .3313541
           _cons |  -2.727106   .7170044    -3.80   0.000    -4.132409   -1.321803
-----------------+----------------------------------------------------------------
D                |
       UVAtevola |  -14.83818   1290.627    -0.01   0.991    -2544.421    2514.745
NUTILIZZODIVOLTE |  -.3792106   .4314916    -0.88   0.379    -1.224919    .4664973
 EXPOSTFIORITURA |  -.4976473   .8798813    -0.57   0.572    -2.222183    1.226888
      DIMENSIONE |  -.0976071   .0905061    -1.08   0.281    -.2749958    .0797817
            DOSI |  -.2036094   .4729157    -0.43   0.667    -1.130507    .7232883
           _cons |  -2.242187   1.425189    -1.57   0.116    -5.035506    .5511316
-----------------+----------------------------------------------------------------
E                |
       UVAtevola |   .7193533   .2948825     2.44   0.015     .1413942    1.297312
NUTILIZZODIVOLTE |  -.1058946   .0921645    -1.15   0.251    -.2865337    .0747446
 EXPOSTFIORITURA |  -.4057074   .2529228    -1.60   0.109     -.901427    .0900122
      DIMENSIONE |  -.0641196    .025192    -2.55   0.011     -.113495   -.0147442
            DOSI |   .0965401   .0441483     2.19   0.029      .010011    .1830692
           _cons |  -1.615742   .3101875    -5.21   0.000    -2.223698   -1.007786
----------------------------------------------------------------------------------



predict prob*
    egen pred_max = rowmax(prob*)
(23 missing values generated)

. 
. 
. 
. g pred_choice = .
(952 missing values generated)

. 
. forvalues i = 1/6 {
  2. 
.  replace pred_choice = `i' if (pred_max == prob`i')
  3. 
. }
(951 real changes made)
(23 real changes made)
(23 real changes made)
(23 real changes made)
(23 real changes made)
(24 real changes made)

. 
. 
. 
. local produttore_lab: value label n_produttore

. 
. label values pred_choice `produttore_lab'

. 
. tab pred_choice n_produttore

pred_choic |                           n_produttore
         e |     ALTRO          A          B          C          D          E |     Total
-----------+------------------------------------------------------------------+----------
     ALTRO |       666         95         67         14          6         80 |       928 
         E |        21          1          0          0          0          2 |        24 
-----------+------------------------------------------------------------------+----------
     Total |       687         96         67         14          6         82 |       952 

where: n_produttore = ALTRO A B C D E

UVAtevola = dummy 0 or 1

NUTILIZZODIVOLTE = 1...15

EXPOSTFIORITURA = dummy 0 or 1

DIMENSIONE = 1....50 kg

DOSI: 1....20


回答1:


Your code seems to be taken from an answer by Tzygmund McFarlane in Statalist.org. I reproduce the complete, working example below:

webuse sysdsn1, clear

mlogit insure age male nonwhite i.site

predict prob*
egen pred_max = rowmax(prob*)

g pred_choice = .
forv i=1/3 {
 replace pred_choice = `i' if (pred_max == prob`i')
}

local insure_lab: value label insure
label values pred_choice `insure_lab'
tab pred_choice insure

Like I said, it works. So unless you give more information on the problem you have at hand, people may not be able to help you. There may be an issue with your model specification, with your data structure, a combination, or something else. Your statement

... but it doesn't work.

gives nothing for people to work with. Good practice is to post exact input/output, including errors. Please read the complete Asking section in https://stackoverflow.com/help.



来源:https://stackoverflow.com/questions/23912352/how-to-create-a-confusion-matrix-after-mlogit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!