如何在mlogit之后创建一个混淆矩阵?

时间:2014-05-28 13:01:23

标签: stata

我有一个分类变量n_produttore:A-B-C-D-E-F 和多项logit(mlogit)的输出。

如何创建混淆矩阵?

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 

其中:n_produttore = ALTRO A B C D E

UVAtevola =哑0或1

NUTILIZZODIVOLTE = 1 ... 15

EXPOSTFIORITURA =哑0或1

DIMENSIONE = 1 .... 50 kg

DOSI:1 .... 20

1 个答案:

答案 0 :(得分:0)

您的代码似乎取自Tzygmund McFarlane在Statalist.org中的回答。我重现下面的完整,有效的例子:

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
像我说的那样,它有效。因此,除非您提供有关您手头的问题的更多信息,否则人们可能无法帮助您。您的模型规范可能存在问题,包括数据结构,组合或其他内容。你的陈述

  

......但它不起作用。

没有任何人可以使用。好的做法是发布完全输入/输出,包括错误。请阅读https://stackoverflow.com/help中的完整提问部分。