Announcement

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

  • extract coefficients from e(b) after regress

    Stata listers--

    Can I extract individual coefficients directly from e(b) after the regress command, or must I copy e(b) to another matrix first and then address the elements.
    e(b) is a matrix, right?

    Thank you.

    Mitchell Berman
    Columbia University


    Last edited by Mitchell Berman; 31 Oct 2016, 10:32.

  • #2
    e(b) is a "virtual" matrix. You can do some matrix things with it, but not all:

    Code:
    . sysuse auto, clear
    (1978 Automobile Data)
    
    . regress price mpg headroom
    
          Source |       SS           df       MS      Number of obs   =        74
    -------------+----------------------------------   F(2, 71)        =     10.44
           Model |   144280501         2  72140250.4   Prob > F        =    0.0001
        Residual |   490784895        71  6912463.32   R-squared       =    0.2272
    -------------+----------------------------------   Adj R-squared   =    0.2054
           Total |   635065396        73  8699525.97   Root MSE        =    2629.2
    
    ------------------------------------------------------------------------------
           price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
             mpg |  -259.1057   58.42485    -4.43   0.000    -375.6015   -142.6098
        headroom |  -334.0215   399.5499    -0.84   0.406    -1130.701    462.6585
           _cons |   12683.31   2074.497     6.11   0.000     8546.885    16819.74
    ------------------------------------------------------------------------------
    
    .
    . //      YOU CAN -matrix list- e(b)
    . matrix list e(b)
    
    e(b)[1,3]
               mpg    headroom       _cons
    y1  -259.10566  -334.02147   12683.315
    
    .
    . //      BUT YOU CAN'T EXTRACT FROM IT IN THE USUAL WAYS
    . capture noisily display e(b)[1, 2]
    invalid syntax
    
    . capture noisily display el(e(b), 1, 2)
    matrix operators that return matrices not allowed in this context
    
    .
    .
    .
    . //      YOU CAN EXTRACT EASILY FROM _B
    . display _b[headroom]
    -334.02147
    
    .
    . //      OR YOU CAN COPY e(b) TO A REAL MATRIX
    . //      AND EXTRACT IN THE USUAL WAYS
    . matrix b = e(b)
    
    . display b[1,2]
    -334.02147
    
    . display el(b, 1, 2)
    -334.02147
    FWIW, when I need to refer to coefficients I usually use _b[] because it is straightforward, and you can refer to the coefficients by name without having to know (or find) the corresponding column numbers.

    Comment


    • #3
      you don't say what you want this for or how you are going to use it; my first suggestion is just to grab the coefficient using something like:
      Code:
      local coef1=_b[varname]
      my second suggestion is to save e(b) as another matrix and extract from that:
      Code:
      mat coef=e(b)
      local coef1=el(coef,#,#)
      where you replace the "#s" with the row (1 for many regression commands) and the column number (you can easily get this by, e.g., displaying the matrix via:
      Code:
      mat li coef

      Comment


      • #4
        Wow!! Thank you for this masterful explanation. I think I've got it, now.
        MB

        Comment


        • #5
          Re #2, in case any other (relative) Stata newbies are wondering (as I was), the second character in el(b, 1, 2) is the letter L, not the digit 1. From the fine manual:

          el(s,i,j) Description: s[floor(i),floor(j)], the i, j element of the matrix named s; missing if i or j are out of range or if matrix s does not exist

          HTH.
          --
          Bruce Weaver
          Email: [email protected]
          Version: Stata/MP 18.5 (Windows)

          Comment


          • #6
            Hello,

            So I am doing a diff-in-diff, and I want my results to be like this:


            Outcome Change in E Change in L DID N
            Outcome 1 r(diff1) r(diff0) r(diffdiff)
            .
            .
            .
            Outcome N



            Is this the right command ?

            local vars cook_inside handwashing no_toilet no_operational_toilet water scale_women elec time_total B2_07 closed (these are my variables)
            matrix Results = J(10, 4, -99)
            matrix colnames Results = β(early) Late(a) DID N
            matrix rownames Results = `vars'


            local irow = 0
            qui{
            foreach var of varlist cook_inside handwashing no_toilet no_operational_toilet ///
            water scale_women elec time_total B2_07 closed {
            diff `var', period(tperiod) treated(tstatus3) cov(A01 caste* educ*) kernel rcs cluster(a_06) support
            }
            local ++irow
            matrix b=e(b)
            matrix Results[`irow',3] = b[1, 3]
            matrix Results[`irow',1]= r(diff1)
            matrix Results[`irow',2]= r(diff0)
            matrix Results[`irow',4]= e(N)

            Comment


            • #7
              Hello Everyone,
              Please I'm estimating stochastic cost frontier and I want to know how to extract the individual estimated coefficient and save them as vectors for further computation. For example
              Code:
              webuse frontier2
              cons def 1 lnp_l+ lnp_k=1
              sfcross lncost lnout lnp_l lnp_k, cost constr(1)
              Please how do I extract the individual coefficient and save them as vectors? Thanks in advance.

              Comment


              • #8
                -sfcross- is not an official Stata command, and I don't know anything about it. But, if it works like official Stata estimation commands (and most user-written estimation commands do, or try to) it will leave behind the coefficient matrix in e(b). So -matrix Wanted = e(b)- will get it for you as a row vector.

                Comment


                • #9
                  Hi

                  I am trying to extract the beta coefficients assciated to 78 cohorts(groups) of the variable id_region_birt_3. Such results were obtained from the following regression:

                  regress ln_inglab_hora i.id_region_birt_3 experiencia experiencia_cua escolaridad period2 period3

                  I am only interested in the coefficients for the 78 groups contained in that variable id_region_birt_3, for later using them as the input for an index .

                  matrix b= e(b)'
                  mat list b



                  matrix b= e(b)
                  mat b = e(b)[1, "2.id_region_birt_3" .. "78.id_region_birt_3"]
                  mat b = b, e(b)[1, "_cons"]
                  mat l b
                  matrix b= (b)'
                  mat l b

                  I am stuck in the following part: I want to use " mat rowname b", but I don' know how to use it in a short way. This is what I have and I am getting a conformability error:


                  mat rowname b = b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12 b13 b14 b15 b16 b17 b18 b19 b20 b21 b22 b23 b24 b25 b26 b27 b28 b29 b30 b31 b32 b33 b34 b35 b36 b37 b38 b39 b40 b41 b42 b43 ///
                  b44 b45 b46 b47 b48 b49 b50 b51 b52 b53 b54 b55 b56 b57 b58 b59 b60 b61 b62 b63 b64 b65 b66 b67 b68 b69 b70 b71 b72 b73 b74 b75 b76 b77 b78 b79

                  Can anyone suggest a simpler way for doing this?


                  Comment


                  • #10
                    Well, your code has some inconsistencies in it.

                    Although you stated that you wanted to extract the 78 id_region_birt_3 coefficients, in fact, you only extracted 77 of them. You started with 2.id_region_birt_3 and went up through 78.id_region_birt_3. That means you never extracted 1.id_region_birt_3. Yes, I know, 1.id_region_birt_3 will be omitted as the base category--but it is still there as a zero in e(b).

                    Anyway, you then tack on the constant term at the end. That still leaves you with 77 + 1 = 78 columns in b. You then transpose it, so you now have a 78 rows by 1 column matrix in b. So, with only 78 rows, you cannot apply 79 names to them.

                    To fix this you either have to include 1.id_region_birt_3 in the original extraction, or you have to remove b1 from your -matrix rownames- command. (But not both.)

                    Comment


                    • #11
                      Thank you Clyde.
                      I have another issue now.

                      I had to create dummies for those categories I was getting from using i.id_region_birt_3, in order to store them for later use. I did it manually.

                      I have the following ( there must be a shorter and less prone- error way of doing this)

                      regress ln_inglab_hora id_region_birt_3_2 id_region_birt_3_3 id_region_birt_3_4 id_region_birt_3_5 id_region_birt_3_6 id_region_birt_3_7 id_region_birt_3_8 ///
                      id_region_birt_3_9 id_region_birt_3_10 id_region_birt_3_11 id_region_birt_3_12 id_region_birt_3_13 id_region_birt_3_14 id_region_birt_3_15 id_region_birt_3_16 id_region_birt_3_17 ///
                      id_region_birt_3_18 id_region_birt_3_19 id_region_birt_3_20 id_region_birt_3_21 id_region_birt_3_22 id_region_birt_3_23 id_region_birt_3_24 id_region_birt_3_25 id_region_birt_3_26 ///
                      id_region_birt_3_27 id_region_birt_3_28 id_region_birt_3_29 id_region_birt_3_30 id_region_birt_3_31 id_region_birt_3_32 id_region_birt_3_33 id_region_birt_3_34 id_region_birt_3_35 ///
                      id_region_birt_3_36 id_region_birt_3_37 id_region_birt_3_38 id_region_birt_3_39 id_region_birt_3_40 id_region_birt_3_41 id_region_birt_3_42 id_region_birt_3_43 id_region_birt_3_44 ///
                      id_region_birt_3_45 id_region_birt_3_46 id_region_birt_3_47 id_region_birt_3_48 id_region_birt_3_49 id_region_birt_3_50 id_region_birt_3_51 id_region_birt_3_52 id_region_birt_3_53 ///
                      id_region_birt_3_54 id_region_birt_3_55 id_region_birt_3_56 id_region_birt_3_57 id_region_birt_3_58 id_region_birt_3_59 id_region_birt_3_60 id_region_birt_3_61 id_region_birt_3_62 ///
                      id_region_birt_3_63 id_region_birt_3_64 id_region_birt_3_65 id_region_birt_3_66 id_region_birt_3_67 id_region_birt_3_69 id_region_birt_3_70 id_region_birt_3_71 id_region_birt_3_72 ///
                      id_region_birt_3_73 id_region_birt_3_74 id_region_birt_3_75 id_region_birt_3_76 id_region_birt_3_77 id_region_birt_3_78 experiencia experiencia_cua escolaridad period2 period3


                      matrix b= e(b)
                      mat b = e(b)[1, "id_region_birt_3_2" .. "id_region_birt_3_78"]
                      mat b = b, e(b)[1, "_cons"]
                      mat l b
                      matrix b= (b)'
                      mat l b


                      mat rowname b = b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12 b13 b14 b15 b16 b17 b18 b19 b20 b21 b22 b23 b24 b25 b26 b27 b28 b29 b30 b31 b32 b33 b34 b35 b36 b37 b38 b39 b40 b41 b42 b43 ///
                      b44 b45 b46 b47 b48 b49 b50 b51 b52 b53 b54 b55 b56 b57 b58 b59 b60 b61 b62 b63 b64 b65 b66 b67 b68 b69 b70 b71 b72 b73 b74 b75 b76 b77 b78


                      sum id_region_birt_3_2
                      local id_region_birt_3_2 = `r(mean)'
                      sum id_region_birt_3_3
                      local id_region_birt_3_3 = `r(mean)'
                      sum id_region_birt_3_4
                      local id_region_birt_3_4= `r(mean)'
                      sum id_region_birt_3_5
                      local id_region_birt_3_5= `r(mean)'
                      sum id_region_birt_3_6
                      local id_region_birt_3_6= `r(mean)'

                      .................................

                      sum id_region_birt_3_77
                      local id_region_birt_3_77 = `r(mean)'


                      The error I am getting is in the following code:

                      mat C = ( `id_region_birt_3_2', `id_region_birt_3_3', `id_region_birt_3_4', `id_region_birt_3_5', `id_region_birt_3_6', `id_region_birt_3_7', `id_region_birt_3_8', ///
                      `id_region_birt_3_9', `id_region_birt_3_10', `id_region_birt_3_11', `id_region_birt_3_12', `id_region_birt_3_13', `id_region_birt_3_14', `id_region_birt_3_15', `id_region_birt_3_16', ///
                      `id_region_birt_3_17', `id_region_birt_3_18', `id_region_birt_3_19', `id_region_birt_3_20', `id_region_birt_3_21', `id_region_birt_3_22', `id_region_birt_3_23', `id_region_birt_3_24', ///
                      `id_region_birt_3_25', `id_region_birt_3_26', `id_region_birt_3_27', `id_region_birt_3_28', `id_region_birt_3_29', `id_region_birt_3_30', `id_region_birt_3_31', `id_region_birt_3_32', ///
                      `id_region_birt_3_33', `id_region_birt_3_34', `id_region_birt_3_35', `id_region_birt_3_36', `id_region_birt_3_37', `id_region_birt_3_38', `id_region_birt_3_39', `id_region_birt_3_40', ///
                      `id_region_birt_3_41', `id_region_birt_3_42', `id_region_birt_3_43', `id_region_birt_3_44', `id_region_birt_3_45', `id_region_birt_3_46', `id_region_birt_3_47', `id_region_birt_3_48', ///
                      `id_region_birt_3_49', `id_region_birt_3_50', `id_region_birt_3_51', `id_region_birt_3_52', `id_region_birt_3_53', `id_region_birt_3_54', `id_region_birt_3_55', `id_region_birt_3_56' , ///
                      `id_region_birt_3_57', `id_region_birt_3_58', `id_region_birt_3_59', `id_region_birt_3_60', `id_region_birt_3_61', `id_region_birt_3_62', `id_region_birt_3_63', `id_region_birt_3_64', ///
                      `id_region_birt_3_65', `id_region_birt_3_66', `id_region_birt_3_67', `id_region_birt_3_68', `id_region_birt_3_69', `id_region_birt_3_70', `id_region_birt_3_71', `id_region_birt_3_72' , ///
                      `id_region_birt_3_73', `id_region_birt_3_74', `id_region_birt_3_75', `id_region_birt_3_76', `id_region_birt_3_77', 1)

                      Comment


                      • #12
                        And what exactly is the error you are getting with that command?

                        I am at the same time awed and appalled at the code you show in #11. Awed because I would never be able to write that code myself--in the unlikely event I had the patience to even try, it would be riddled with typos by the time I was done. Appalled at the thought of how much time and effort it took to do that when you could have just done this to get the same results:
                        Code:
                        regress ln_inglab_hora i.id_region_birt_3 experiencia experiencia_cua ///
                            escolaridad period2 period3
                            
                        matrix b = e(b)
                        matrix b = b[1, "2.id_region_birt_3" .. "78.id_region_birt_3"], b[1, "_cons"]
                        matrix b = b'
                        matrix list b
                        
                        local rownames
                        forvalues i = 2/78 {
                            local rownames `rownames' b`i'
                        }
                        local rownames `rownames' constant
                        display `"`rownames'"'
                        
                        matrix rownames b = `rownames'
                        matrix list b
                        
                        forvalues i = 2/77 {
                            summ `i'.id_region_birt_3, meanonly
                            matrix C = nullmat(C), (`r(mean)')
                        }
                        matrix C = C, (1)
                        matrix list C
                        In the above, I am wondering why, in creating matrix C, you are going out to 77, but not 78. If you are going to somehow combine matrices b and C, say by multiplying them together, you will get an error as the dimensions of the matrix won't be compatible.

                        I am wondering, also, what is the later use for which you created and stored your hand-coded indicators for categories 2 through 10 of variable id_region_birt_3. There really is very little you can do with such variables that you cannot do more easily using factor-variable notation in modern Stata. The main use for such variables is to apply a limited number of old Stata commands, or old user-written commands, that were written back before factor-variable notation was introduced and have never been updated.

                        May I make a suggestion? You have obviously gotten through the elementary techniques of Stata and can use them well. But you are starting to undertake intermediate level problems, and for that you need to become familiar with intermediate Stata techniques. In particular, you need to start doing loops. Read the PDF documentation for -foreach ... of-, -foreach ... in-, and -forvalues-. And you definitely need to read up on and practice using local macros. It's time for you to bump your Stata skills up to the next level.
                        Last edited by Clyde Schechter; 21 Mar 2024, 19:42.

                        Comment


                        • #13
                          Thanks Clyde.

                          This is the error:


                          . mat C = ( `id_region_birt_3_2', `id_region_birt_3_3', `id_region_birt_3_4', `id_region_birt_3_5', `id_region_birt_3_6',
                          > `id_region_birt_3_7', `id_region_birt_3_8' ///
                          > `id_region_birt_3_9', `id_region_birt_3_10', `id_region_birt_3_11', `id_region_birt_3_12', `id_region_birt_3_13', `id_re
                          > gion_birt_3_14', `id_region_birt_3_15', `id_region_birt_3_16' ///
                          > `id_region_birt_3_17', `id_region_birt_3_18', `id_region_birt_3_19', `id_region_birt_3_20', `id_region_birt_3_21', `id_r
                          > egion_birt_3_22', `id_region_birt_3_23', `id_region_birt_3_24' ///
                          > `id_region_birt_3_25', `id_region_birt_3_26', `id_region_birt_3_27', `id_region_birt_3_28', `id_region_birt_3_29', `id_r
                          > egion_birt_3_30', `id_region_birt_3_31', `id_region_birt_3_32' ///
                          > `id_region_birt_3_33', `id_region_birt_3_34', `id_region_birt_3_35', `id_region_birt_3_36', `id_region_birt_3_37', `id_r
                          > egion_birt_3_38', `id_region_birt_3_39', `id_region_birt_3_40' ///
                          > `id_region_birt_3_41', `id_region_birt_3_42', `id_region_birt_3_43', `id_region_birt_3_44', `id_region_birt_3_45', `id_r
                          > egion_birt_3_46', `id_region_birt_3_47', `id_region_birt_3_48' ///
                          > `id_region_birt_3_49', `id_region_birt_3_50', `id_region_birt_3_51', `id_region_birt_3_52', `id_region_birt_3_53', `id_r
                          > egion_birt_3_54', `id_region_birt_3_55', `id_region_birt_3_56' ///
                          > `id_region_birt_3_57', `id_region_birt_3_58', `id_region_birt_3_59', `id_region_birt_3_60', `id_region_birt_3_61', `id_r
                          > egion_birt_3_62', `id_region_birt_3_63', `id_region_birt_3_64' ///
                          > `id_region_birt_3_65', `id_region_birt_3_66', `id_region_birt_3_67', `id_region_birt_3_68', `id_region_birt_3_69', `id_r
                          > egion_birt_3_70', `id_region_birt_3_71', `id_region_birt_3_72' ///
                          > `id_region_birt_3_73', `id_region_birt_3_74', `id_region_birt_3_75', `id_region_birt_3_76', `id_region_birt_3_77', 1)
                          ,, not found

                          Comment


                          • #14
                            OK, I don't know what that message is referring to. I don't see any ",," anywhere in that code. But I do notice that you did not put commas at the points where you start new lines of code. So, for example, there is no comma between `id_region_birt_3_8' and `id_region_birt_3_9', nor 16 and 17, nor 24 and 25. In fact each place you have /// you are missing a comma before it.

                            I was editing my response to #12 while you were posting #13, and there I added some code that accomplishes the same thing more efficiently, along with some commentary. So I suggest you read what I have put there and go forward with that instead instead of trying to fix your current code. This problem actually illustrates what I was saying about needing to use a more advanced approach to a more advanced problem--the kind of detailed hand-coding you are doing really just doesn't scale well to problems of this size. (And if instead of 78 values of id_region_birt_3 you had 7,800 it would clearly be impossible.)

                            Comment


                            • #15
                              Dear Clyde,
                              I really appreciate you take your time to indicate how to upgrade my code and your recommendations on improving my stata skills.
                              I do need to come to the next level . I ave downloaded "An Introduction to Stata Programming" by Christopher Baum (2009).

                              Thank you

                              Comment

                              Working...
                              X