Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Storing names of interaction terms in logistic regression to be used with testparm in a loop

    Hi,
    I am trying to run testparm for multiple interaction coefficients (for a categorical variable with multiple levels) after logistic regression in a loop. After testparm I need to put the names of the interaction coefficients generated but I am not finding a way of accessing them through postestimation commands; from the Stata documentation they do not seem to be stored. Do you know if there is a way of accessing the names of the interaction coefficients generated so I can easily use them in a loop without copying and paste each of them?

    Best

  • #2
    The return list after -logit- includes r(table), from whose column names you can obtain a list of all the variables in the model with an extended macro function. The following might start you in the right direction:
    Code:
    logit .....
    local varnames: colfullnames r(table)
    foreach v of local varnames {
      di "`v'"
    }

    Comment


    • #3
      You can find out the names with an option called coefleg. Here is an example:

      Code:
      sysuse nlsw88, clear
      logit married age i.race##i.collgrad, coefleg
      Results:
      Code:
      -------------------------------------------------------------------------------------
                  married | Coefficient  Legend
      --------------------+----------------------------------------------------------------
                      age |    -.02144  _b[age]
                          |
                     race |
                   Black  |  -1.029574  _b[2.race]
                   Other  |  -.0159885  _b[3.race]
                          |
                 collgrad |
            College grad  |  -.1069247  _b[1.collgrad]
                          |
            race#collgrad |
      Black#College grad  |   .1709965  _b[2.race#1.collgrad]
      Other#College grad  |   -.060614  _b[3.race#1.collgrad]
                          |
                    _cons |   1.730005  _b[_cons]
      -------------------------------------------------------------------------------------
      And if you just mean the name of the whole set of interaction, then it's just the row title (race#collgrad), as:

      Code:
      testparm race#collgrad

      Comment


      • #4
        testparm uses _ms_extract_varlist to translate a
        varlist specification to matrix stripe elements in the current
        estimation results. While _ms_extract_varlist is undocumented,
        it does have a help file.

        Here is an example of this command in action.
        Code:
        . sysuse auto
        (1978 automobile data)
        
        . regress mpg turn trunk for#rep
        note: 1.foreign#1b.rep78 identifies no observations in the sample.
        note: 1.foreign#2.rep78 identifies no observations in the sample.
        
              Source |       SS           df       MS      Number of obs   =        69
        -------------+----------------------------------   F(9, 59)        =     10.99
               Model |  1465.91929         9  162.879921   Prob > F        =    0.0000
            Residual |  874.283608        59  14.8183662   R-squared       =    0.6264
        -------------+----------------------------------   Adj R-squared   =    0.5694
               Total |   2340.2029        68  34.4147485   Root MSE        =    3.8495
        
        -------------------------------------------------------------------------------
                  mpg | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
        --------------+----------------------------------------------------------------
                 turn |  -.8183495   .1731554    -4.73   0.000    -1.164833   -.4718663
                trunk |    -.25088   .1475067    -1.70   0.094    -.5460402    .0442802
                      |
        foreign#rep78 |
          Domestic#2  |    1.60522   3.145736     0.51   0.612    -4.689383    7.899823
          Domestic#3  |   .3552652   2.991137     0.12   0.906    -5.629985    6.340516
          Domestic#4  |   .3116473   3.215529     0.10   0.923    -6.122612    6.745906
          Domestic#5  |   7.159133   3.963637     1.81   0.076    -.7720857    15.09035
           Foreign#1  |          0  (empty)
           Foreign#2  |          0  (empty)
           Foreign#3  |  -1.342274   3.762394    -0.36   0.723    -8.870806    6.186259
           Foreign#4  |  -.5612612    3.23632    -0.17   0.863    -7.037122      5.9146
           Foreign#5  |    1.72808   3.261867     0.53   0.598    -4.798901     8.25506
                      |
                _cons |   56.68481   7.139194     7.94   0.000     42.39931     70.9703
        -------------------------------------------------------------------------------
        
        . testparm for#rep
        
         ( 1)  0b.foreign#2.rep78 = 0
         ( 2)  0b.foreign#3.rep78 = 0
         ( 3)  0b.foreign#4.rep78 = 0
         ( 4)  0b.foreign#5.rep78 = 0
         ( 5)  1.foreign#3.rep78 = 0
         ( 6)  1.foreign#4.rep78 = 0
         ( 7)  1.foreign#5.rep78 = 0
        
               F(  7,    59) =    1.26
                    Prob > F =    0.2872
        
        . _ms_extract_varlist for#rep
        
        . di "{p}" r(varlist) "{p_end}"
        0.foreign#1.rep78 0.foreign#2.rep78 0.foreign#3.rep78 0.foreign#4.rep78 0.foreign#5.rep78
        1.foreign#1.rep78 1.foreign#2.rep78 1.foreign#3.rep78 1.foreign#4.rep78 1.foreign#5.rep78
        Note that testparm automatically removes base, omitted, and empty levels from the reported constraints, but _ms_extract_varlist shows all levels by default. You can use option noomitted to remove the omitted and empty levels from being posted to r(varlist).

        Comment


        • #5
          Thank you very much for your reply! I am trying to extract just two names in from the list of names in local varnames with the extended function ": word of" but I cannot extract multiple words at once. So I tried creating a local macro for the position of the 2 names I need (position 4 and 5 in the list) and used it within the extended function ": word of" as below:
          Code:
          logit .....
          local varnames: colfullnames r(table)
          local i "4 5"
          local ter "`: word `i' of `varnames''"
          invalid syntax
          cannot find the issue with the syntax though. Or is there a better way of extracting only a few words from the list of the matrix coulmn names?


          Comment


          • #6
            The : word # of extended macro function can only get one word/piece at a time.

            You can grab the sub matrix then pull the stripe elements from that.
            Here is an example.
            Code:
            . sysuse auto
            (1978 automobile data)
            
            . regress mpg turn for#rep
            note: 1.foreign#1b.rep78 identifies no observations in the sample.
            note: 1.foreign#2.rep78 identifies no observations in the sample.
            
                  Source |       SS           df       MS      Number of obs   =        69
            -------------+----------------------------------   F(8, 60)        =     11.64
                   Model |  1423.05369         8  177.881711   Prob > F        =    0.0000
                Residual |  917.149207        60  15.2858201   R-squared       =    0.6081
            -------------+----------------------------------   Adj R-squared   =    0.5558
                   Total |   2340.2029        68  34.4147485   Root MSE        =    3.9097
            
            -------------------------------------------------------------------------------
                      mpg | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
            --------------+----------------------------------------------------------------
                     turn |  -.9575443   .1549821    -6.18   0.000    -1.267555    -.647534
                          |
            foreign#rep78 |
              Domestic#2  |   .3991678   3.112735     0.13   0.898     -5.82723    6.625566
              Domestic#3  |  -1.326173   2.867219    -0.46   0.645    -7.061465     4.40912
              Domestic#4  |  -1.598011   3.060287    -0.52   0.603    -7.719496    4.523473
              Domestic#5  |   6.212278   3.985763     1.56   0.124    -1.760435    14.18499
               Foreign#1  |          0  (empty)
               Foreign#2  |          0  (empty)
               Foreign#3  |  -3.092751   3.675524    -0.84   0.403    -10.44489    4.259391
               Foreign#4  |  -1.856377   3.194688    -0.58   0.563    -8.246705    4.533951
               Foreign#5  |   .1200365   3.170697     0.04   0.970    -6.222301    6.462374
                          |
                    _cons |   60.25932   6.929618     8.70   0.000     46.39802    74.12062
            -------------------------------------------------------------------------------
            
            . matrix list r(table)
            
            r(table)[9,12]
                                  0b.foreign#  0b.foreign#  0b.foreign#  0b.foreign#  0b.foreign#  1o.foreign#  1o.foreign#
                           turn     1b.rep78      2.rep78      3.rep78      4.rep78      5.rep78     1b.rep78     2o.rep78
                 b   -.95754432            0    .39916776   -1.3261725   -1.5980112    6.2122784            0            0
                se    .15498208            .    3.1127355    2.8672192    3.0602866    3.9857629            .            .
                 t   -6.1784194            .    .12823697   -.46252917   -.52217699    1.5586171            .            .
            pvalue    6.174e-08            .    .89839031    .64537493    .60346973    .12434653            .            .
                ll   -1.2675546            .   -5.8272303   -7.0614648   -7.7194958   -1.7604346            .            .
                ul     -.647534            .    6.6255658    4.4091198    4.5234733    14.184991            .            .
                df           60           60           60           60           60           60           60           60
              crit    2.0002978    2.0002978    2.0002978    2.0002978    2.0002978    2.0002978    2.0002978    2.0002978
             eform            0            0            0            0            0            0            0            0
            
                      1.foreign#   1.foreign#   1.foreign#            
                        3.rep78      4.rep78      5.rep78        _cons
                 b   -3.0927512    -1.856377    .12003647    60.259317
                se    3.6755237    3.1946885    3.1706968    6.9296176
                 t   -.84144503   -.58108233    .03785807     8.695908
            pvalue    .40343989    .56336149    .96992652    3.217e-12
                ll   -10.444893   -8.2467055   -6.2223015    46.398018
                ul    4.2593909    4.5339514    6.4623744    74.120616
                df           60           60           60           60
              crit    2.0002978    2.0002978    2.0002978    2.0002978
             eform            0            0            0            0
            
            . matrix sel = r(table)[1...,4..5]
            
            . local ter : colfullnames sel
            
            . di "`ter'"
            0b.foreign#3bn.rep78 0b.foreign#4.rep78
            Last edited by Jeff Pitblado (StataCorp); 29 Sep 2023, 10:32.

            Comment

            Working...
            X