Announcement

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

  • Storing beta after reg as a value in another variable

    Hello Statalist.

    I have a problem in a larger code I’m writing and it’s basically reduced to how I store the value of beta after reg in a single value.

    If I do this:


    Code:
    gen beta=.
    reg y x
    replace beta=beta coef.
    My problem is that beta is stored as a matrix with both constant and beta and I don’t know how to refer to that specific value as I would with e(N). The reason for the replace is that it needs to work in a loop later on, so I need to be able to replace beta with many different coefficients... I just don’t know how to call beta as a single value. I tried parmest but that has the same problem; here I can store beta but can either save a dataset I can’t merge with the original or I can call a list that I can’t extract values from. Can I store the beta in a local or something?

  • #2
    Code:
    replace beta = _b[x]
    If you are planning to loop over subsets of observations, and that is the reason you first generate beta as missing and then replace it, then you will also need to put some -if- condition on the -replace- command so that it only does the replacement in the appropriate observations.

    Finally, you don't indicate what kind of looping you plan to do, but if you have a very large data set, this can be very time consuming. Depending on what the conditions controlling the loops are, you might be able to do this more efficiently using -runby- or -rangestat-. Both of these are available from SSC. -runby- is written by Robert Picard and me. -rangestat- is written by Robert Picard, Nick Cox and Roberto Ferrer. -runby- will be helpful if you are looping over the values of some variable(s); -rangestat- (which also does the regression, so no separate -reg- command is needed) will be useful if time ranges (or ranges of some other numeric variable) define the different estimation samples.

    Comment


    • #3
      Hello Clyde.

      Thank you so much for your reply. Part of me is annoyed that I didn’t know how simple that was - is this a standard thing that _ will result in a specific value? What does _ imply here? I haven’t seen this used except from _n or something similar.

      Comment


      • #4
        There is a StataCorp convention to use _b for coefficients estimated in a model fit. These coefficients are saved results until overwritten by the next such command.

        The simplest syntax is that _b[x] is the coefficient associated with predictor x.

        Code:
        . sysuse auto
        (1978 Automobile Data)
        
        . regress mpg weight
        
              Source |       SS           df       MS      Number of obs   =        74
        -------------+----------------------------------   F(1, 72)        =    134.62
               Model |   1591.9902         1   1591.9902   Prob > F        =    0.0000
            Residual |  851.469256        72  11.8259619   R-squared       =    0.6515
        -------------+----------------------------------   Adj R-squared   =    0.6467
               Total |  2443.45946        73  33.4720474   Root MSE        =    3.4389
        
        ------------------------------------------------------------------------------
                 mpg |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
        -------------+----------------------------------------------------------------
              weight |  -.0060087   .0005179   -11.60   0.000    -.0070411   -.0049763
               _cons |   39.44028   1.614003    24.44   0.000     36.22283    42.65774
        ------------------------------------------------------------------------------
        
        . di _b[weight]
        -.00600869
        
        . regress mpg price
        
              Source |       SS           df       MS      Number of obs   =        74
        -------------+----------------------------------   F(1, 72)        =     20.26
               Model |  536.541807         1  536.541807   Prob > F        =    0.0000
            Residual |  1906.91765        72  26.4849674   R-squared       =    0.2196
        -------------+----------------------------------   Adj R-squared   =    0.2087
               Total |  2443.45946        73  33.4720474   Root MSE        =    5.1464
        
        ------------------------------------------------------------------------------
                 mpg |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
        -------------+----------------------------------------------------------------
               price |  -.0009192   .0002042    -4.50   0.000    -.0013263   -.0005121
               _cons |   26.96417   1.393952    19.34   0.000     24.18538    29.74297
        ------------------------------------------------------------------------------
        
        . di _b[weight]
        [weight] not found
        r(111);

        Comment


        • #5
          FWIW, I'll also point out that if you want the standard error of the coefficient of x, you can refer to that as _se[x].

          But that's as far as it goes. There is no _t[x], nor _p[x], etc. If you want to pull confidence limits, test statistics, or p-values out after a regression like that, then you need to store the matrix r(table) which is left behind after all Stata estimation commands. r(table) contains coefficients and standard errors as well as test statistics and confidence intervals. It's a matter of just referring to the corresponding rows and columns of that matrix.

          Comment


          • #6
            Sir i want to store series of beta and Standard error values for each firm for panel data. for instance, i am using command bys id reg dep_var Indep_var. i need to beta values for each firm to construct portfolios based on beta values. Dear Prof, i will be grateful for your prompt response.

            Comment


            • #7
              my querry is from Clyde Schechter, as i read sir my answers, which are explained very simple and logical. Sir i want to store series of beta and Standard error values for each firm for panel data. for instance, i am using command bys id reg dep_var Indep_var. i need to beta values for each firm to construct portfolios based on beta values. Dear Prof, i will be grateful for your prompt response.

              Comment


              • #8
                You can't do that using -bys id: regress-, because it runs all the regressions and there is no storage. You don't show example data, so I'll illustrate how you can do this with the auto.dta

                Code:
                clear*
                sysuse auto, clear
                
                rangestat (reg) price mpg, by(rep78) interval(mpg . .)
                -rangestat- is written by Robert Picard, Nick Cox, and Roberto Ferrer. It is available from SSC.

                Comment

                Working...
                X