Announcement

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

  • Storing multiple coefficients from a regression with interaction term

    Hi all!


    I am trying to store the coefficients of the following output under different names:
    Code:
    reg  c.ptile c.ptileFather##t
    
          Source |       SS           df       MS      Number of obs   =       857
    -------------+----------------------------------   F(3, 853)       =      3.72
           Model |  9227.91757         3  3075.97252   Prob > F        =    0.0112
        Residual |  704689.473       853  826.130684   R-squared       =    0.0129
    -------------+----------------------------------   Adj R-squared   =    0.0095
           Total |  713917.391       856  834.015644   Root MSE        =    28.742
    
    ---------------------------------------------------------------------------------
              ptile |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    ----------------+----------------------------------------------------------------
        ptileFather |  -.0203642    .053272    -0.38   0.702    -.1249238    .0841953
                1.t |   .8206032   4.124634     0.20   0.842    -7.275018    8.916224
                    |
    t#c.ptileFather |
                 1  |  -.0997823   .0693683    -1.44   0.151     -.235935    .0363703
                    |
              _cons |    54.0756   3.289373    16.44   0.000     47.61939    60.53182
    ---------------------------------------------------------------------------------
    ideally I'd like to store the coefficients of
    • ptileFather as a_1
    • 1.t as a_2
    • t#c.ptileFather as a_3
    • _con as a_0
    I've tried to run
    Code:
    gen a_2=.
    qui reg c.ptile c.ptileFather##t
    replace a_2=_b[x2]
    to start off, but Stata does not find [x2]. Is there another way...?

    Thanks a lot for your time!

    Best,
    Melanie

  • #2
    all the coefficients are saved by Stata in several places but, at least in your regression, none of them are called "x2" so of course Stata cannot find it; however, you can find what is there by looking at "r(table)" which is automatically saved by Stata after each regression; to be sure you don't lose it, you should probably make a new matrix that is just a copy of it and then extract what you want - why do you want a coefficient to be a variable as that also makes no (little) sense to me? The existence and use of r(table) has been discussed in this forum many times and a search on that term should help

    Comment


    • #3
      Melanie:
      as an aside to Rich's helpful advice, you may want to consider -statsby-:
      Code:
       use "C:\Program Files\Stata17\ado\base\a\auto.dta"
      (1978 automobile data)
      
      . sysuse auto
      (1978 automobile data)
      . statsby, by(foreign): regress c.mpg##c.gear turn
      (running regress on estimation sample)
      
      Command: regress c.mpg##c.gear turn
      By: foreign
      
      Statsby groups
      1 ---+--- 2 ---+--- 3 ---+--- 4 ---+--- 5 
      .
      
      . mat list e(b)
      
      e(b)[1,4]
      c.mpg#                            
      gear_ratio  c.gear_ratio          turn         _cons
      y1    -6.1057963     .27334161    -.09069269       25.4113
      
      .
      Usually, when I code an interaction, I ask Stata to provide me with the -e(b)- matrix to double-check the names of the resulting coefficients provided by -statsby-.
      Kind regards,
      Carlo
      (Stata 19.0)

      Comment


      • #4
        Coefficients are more efficiently stored as scalars, locals, or matrices, rather than variables.

        To see how your coefficients are called, you can do the following:

        Code:
        . sysuse auto, clear
        (1978 automobile data)
        
        . reg price i.rep##c.mpg, coeflegend
        
              Source |       SS           df       MS      Number of obs   =        69
        -------------+----------------------------------   F(9, 59)        =      3.65
               Model |   206362465         9  22929162.7   Prob > F        =    0.0011
            Residual |   370434494        59  6278550.75   R-squared       =    0.3578
        -------------+----------------------------------   Adj R-squared   =    0.2598
               Total |   576796959        68  8482308.22   Root MSE        =    2505.7
        
        ------------------------------------------------------------------------------
               price | Coefficient  Legend
        -------------+----------------------------------------------------------------
               rep78 |
                  2  |   10881.12  _b[2.rep78]
                  3  |   8973.281  _b[3.rep78]
                  4  |   651.7399  _b[4.rep78]
                  5  |   4363.191  _b[5.rep78]
                     |
                 mpg |  -123.1667  _b[mpg]
                     |
         rep78#c.mpg |
                  2  |  -507.6563  _b[2.rep78#c.mpg]
                  3  |  -375.7209  _b[3.rep78#c.mpg]
                  4  |   43.26329  _b[4.rep78#c.mpg]
                  5  |  -81.52802  _b[5.rep78#c.mpg]
                     |
               _cons |       7151  _b[_cons]
        ------------------------------------------------------------------------------
        
        .

        Comment

        Working...
        X