Announcement

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

  • How to recover stored results from e() class?

    Hello,
    I am working on mwitch command of Stata. I work in Stata 15.
    I ran the following:

    . mswitch dr gcf, switch(gds) varswitch states(2) nolog vsquish

    Performing EM optimization:

    Performing gradient-based optimization:


    Markov-switching dynamic regression

    Sample: 1970 - 2016 No. of obs = 47
    Number of states = 2 AIC = 4.4781
    Unconditional probabilities: transition HQIC = 4.5966
    SBIC = 4.7930
    Log likelihood = -97.23547


    gcf Coef. Std. Err. z P>z [95% Conf. Interval]

    State1
    gds .0924856 .1473673 0.63 0.530 -.1963491 .3813203
    _cons 15.42887 3.11739 4.95 0.000 9.318895 21.53884

    State2
    gds .736845 .0485931 15.16 0.000 .6416043 .8320856
    _cons 6.129055 1.193445 5.14 0.000 3.789946 8.468164

    sigma1 2.250846 .3264654 1.693895 2.990922

    sigma2 1.059222 .2000414 .7315295 1.533707

    p11 .9288938 .0537234 .7262418 .9846927

    p21 .0772527 .0579521 .0167304 .2917522



    . matrix list e(b)

    e(b)[1,8]
    State1: State1: State2: State2: lnsigma1: lnsigma2: p11: p21:
    gds _cons gds _cons _cons _cons _cons _cons
    y1 .09248559 15.428868 .73684496 6.1290551 .8113061 .05753501 -2.5698196 2.4802733

    . matrix list e(uncprob)
    e(uncprob)[1,2]
    State1 State2
    r1 .52071497 .47928503

    I want to generate transition probabilities p11 and p21 from the above table. When i try to recover p11 and p21 by running the following code:
    gen p11=[p11]_cons

    It is generating the value as -2.5698196, NOT .9288938, as it is reported in Stata's output
    Similarly, I want to export results into excel using outreg2. When I'm exporting, p11 and p21 are stored as (-2.5698196 2.4802733), not as (.9288938, .0772527). Do not know why.

    Observations:
    1. Transition probabilities reported in the result (shaded in blue) do not match with probabilities stored in matrix (shaded in red). Why is the difference?
    2. Further, the unconditional probabilities do not match with the results of matrix list e(uncprob) (shaded in purple)


    Any help would be greatly appreciated.

  • #2
    Please enclose any code and output in code tags, that makes them much easier to read.

    To extract data from the e-results, you should first copy the e-matrix of interest.

    Code:
    mat results = e(b)
    Then you can extract the elements you need using standard matrix syntax, see
    Code:
    help matrix extraction

    Comment


    • #3
      Actually, in this case, the correct probabilities are returned in the r(table) matrix. That is the matrix that Stata usually outputs to your console. A demonstration:

      Code:
      use https://www.stata-press.com/data/r16/usmacro
      mswitch dr fedfunds
      
      Performing EM optimization:
      
      Performing gradient-based optimization:
      
      Iteration 0:   log likelihood = -508.66031  
      Iteration 1:   log likelihood =  -508.6382  
      Iteration 2:   log likelihood = -508.63592  
      Iteration 3:   log likelihood = -508.63592  
      
      Markov-switching dynamic regression
      
      Sample: 1954q3 - 2010q4                         No. of obs        =        226
      Number of states =    2                         AIC               =     4.5455
      Unconditional probabilities: transition         HQIC              =     4.5760
                                                      SBIC              =     4.6211
      Log likelihood = -508.63592
      
      ------------------------------------------------------------------------------
          fedfunds |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
      -------------+----------------------------------------------------------------
      State1       |
             _cons |    3.70877   .1767083    20.99   0.000     3.362428    4.055112
      -------------+----------------------------------------------------------------
      State2       |
             _cons |   9.556793   .2999889    31.86   0.000     8.968826    10.14476
      -------------+----------------------------------------------------------------
             sigma |   2.107562   .1008692                      1.918851    2.314831
      -------------+----------------------------------------------------------------
               p11 |   .9820939   .0104002                      .9450805    .9943119
      -------------+----------------------------------------------------------------
               p21 |   .0503587   .0268434                      .0173432    .1374344
      ------------------------------------------------------------------------------
      
      
      mat t = r(table)
      mat list t
      
      t[9,8]
                  State1:     State2:    lnsigma:        p11:        p21:   _diparm1:   _diparm1:   _diparm1:
                   _cons       _cons       _cons       _cons       _cons       sigma         p11         p21
           b     3.70877   9.5567934   .74553174  -4.0045427   2.9369133   2.1075618   .98209385   .05035868
          se   .17670834   .29998894    .0478606    .5914089   .56131057   .10086917   .01040023   .02684338
           z   20.988087   31.857152   15.577151   -6.771191   5.2322429          .b          .b          .b
      pvalue   8.427e-98   1.05e-222   1.041e-54   1.277e-11   1.675e-07          .b          .b          .b
          ll    3.362428   8.9688259   .65172669  -5.1636828   1.8367648   1.9188512   .94508055   .01734316
          ul    4.055112   10.144761   .83933678  -2.8454025   4.0370618   2.3148312   .99431195   .13743437
          df           .           .           .           .           .           .           .           .
        crit    1.959964    1.959964    1.959964    1.959964    1.959964    1.959964    1.959964    1.959964
       eform           0           0           0           0           0           0           0           0
      The bolded values should look familiar.

      Basically, it appears that the raw coefficients for the transitions (e.g. p11:_cons) are not probabilities. They're some transformation of the probability. I can't quite tell what the transformation is; it's not an exponent (I checked), and it's not a natural log (ln of a negative number is undefined). e(b) only appears to contain the raw coefficients, whatever scale they were estimated on. It's moot, since the actual probabilities are returned in the matrix r(table), which the code above saves to a matrix called t.

      I have no idea how you would export this to outreg2, so you're on your own there.
      Last edited by Weiwen Ng; 23 Jan 2020, 12:10.
      Be aware that it can be very hard to answer a question without sample data. You can use the dataex command for this. Type help dataex at the command line.

      When presenting code or results, please use the code delimiters format them. Use the # button on the formatting toolbar, between the " (double quote) and <> buttons.

      Comment


      • #4

        @Jesse, Thank you for your reply. I had read and tried but it was not of much help.

        Comment


        • #5
          Weiwen Ng Thank you for your reply. It is difficult to export matrix table to an excel. However, last night I used another way to recover the probabilities and their SE. That worked well.

          Code:
          * Extracting prob
          estat transition
          matrix b = r(prob)
          gen p11_coef= b[1,1]
          * Extracting SE of prob
          matrix b = r(se)
          gen p11_se = b[1,1]
          That worked well. Now, I am struggling to extract the standard deviation of the sigma.
          I could extract the sigma using the following code and then take the exponent of lnsigma1 to get the value which is reported in the Stata table.

          Code:
          di _b[lnsigma:_cons]
          di exp(_b[lnsigma:_cons])
          Using your example, I have extracted the sigma as 0.74553174
          exp(0.74553174)= 2.1075618 which is the same as sigma reported in your table.

          However, I did the same to export the standard deviation of the sigma. It is not working.

          Code:
          di _se[lnsigma:_cons]
          The output is .0478606
          Then I tried taking exponent of it

          Code:
          di exp(_se[lnsigma:_cons])
          I am getting it as 1.0490244 whereas it is .1008692 in the Stata output table.

          Do not know how to extract it.
          Any help in this regard will be greatly appreciated.
          Last edited by Santosh Dash; 23 Jan 2020, 23:32. Reason: better formating

          Comment


          • #6
            Weiwen Ng Finally, I could nail it. Your code of recovering the matrix table helped it.

            Code:
            mat t = r(table)
            mat list t
            di t[2,9]
            That would give output .0478606 which is reported in your illustration table.

            Thank you for your reply

            Comment

            Working...
            X