Announcement

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

  • #16
    Originally posted by Sebastian Kripfgan
    I believe the feature is deliberately undocumented because StataCorp may decide in the future to no longer return the r(table) result in the present form. A user who writes do-files (or ado-files) that rely on the current structure of r(table) shoud be aware that their code might break in future versions of Stata (even under version control).
    Thanks for the reminder.
    Until now, I have relied heavily on the existence of r(table) in do-files or in a published command. Now, I might have to consider changing the existing code, so that it works even if r(table) does not exist but only e(b) and e(V).

    Comment


    • #17
      Re #16. There is no need to do that. This is not "undocumented" and it isn't going away. In fact, you can find it in the help file for -ereturn-. Just open that helpfile and then search for r(table). It's clearly there, and mentioned as something created by -ereturn display-. Moreover, in the PDF docoumentation for -ereturn-, it states that -ereturn display- creates a table whose description matches what r(table) is, although it doesn't actually mention r(table) by name.

      So, as I said a few posts back, it is badly documented, and hard to find. But it is not "undocumented" and you can feel comfortable using it in your code.

      Comment


      • #18
        Re #15. Ah, the ever-popular "return more of the results" from the Stata 17 wish list, where there was a long discussion on that topic a while ago.

        If it comes up again on the wish list, I'll be sure to clarify my position as "return more of the results, and clearly document that you do so", with "in a 9-year-old whatsnew help file" said nobody ever.
        Last edited by William Lisowski; 08 Aug 2020, 16:03.

        Comment


        • #19
          Backward compatibility is (rightfully) a big deal for Stata Corp, and you have no reasons to worry about -r(table)- disappearing, at all.

          For the 20 years of using Stata, I am aware of one and only example where backward compatibility was broken, and this example is my legacy. I discovered a bug in -predict-, Stata Corp fixed it, and some program started generating a minor error on post-fix versions of Stata.

          Otherwise I am stilling using the ancient -for- when doing simple loops from command line (" for is an out-of-date command as of Stata 8"), and it works just fine.


          Originally posted by Sven-Kristjan Bormann View Post
          Thanks for the reminder.
          Until now, I have relied heavily on the existence of r(table) in do-files or in a published command. Now, I might have to consider changing the existing code, so that it works even if r(table) does not exist but only e(b) and e(V).

          Comment


          • #20
            Re #16: I was considering changing the code, just in case there will be a change, which is not preserved under version control. It's not too difficult to rewrite the code, just annoying.
            In #3 I should have cited the help file for -ereturn- as suggested. I assumed that everybody interested in this topic would read eventually the help file and the PDF documentation for -ereturn- and find the parts about r(table).
            I guess that we can consider this case as closed for the moment

            Comment


            • #21
              We agree that r(table) should be more thoroughly documented. We are working on adding r(table) to the list of stored results for each estimation command, and you will see these changes in the manuals over the next few updates.

              Comment


              • #22
                Very good to hear that we are working to a common goal here, great minds work alike :P.

                I wrote a Stata Tip on -r(table)- (attached) and I submitted it for publication in Stata Journal of Monday.

                Originally posted by Kristin MacDonald (StataCorp) View Post
                We agree that r(table) should be more thoroughly documented. We are working on adding r(table) to the list of stored results for each estimation command, and you will see these changes in the manuals over the next few updates.
                Attached Files

                Comment


                • #23
                  So how would I copy the contents of r(table) into a dataset?

                  Comment


                  • #24
                    -svmat- will do the trick.

                    Comment


                    • #25
                      Not that I see any application of doing something like this--the use of r(table) is to have the results there, and to retrieve only what you need for further calculations--but here:

                      Code:
                      . sysuse auto, clear
                      (1978 Automobile Data)
                      
                      . qui reg price mpg headroom
                      
                      . mat v = r(table)
                      
                      . matlist v
                      
                                   |       mpg   headroom      _cons 
                      -------------+---------------------------------
                                 b | -259.1057  -334.0215   12683.31 
                                se |  58.42485   399.5499   2074.497 
                                 t | -4.434854  -.8359943   6.113922 
                            pvalue |  .0000329   .4059628   4.72e-08 
                                ll | -375.6015  -1130.701   8546.885 
                                ul | -142.6098   462.6585   16819.74 
                                df |        71         71         71 
                              crit |  1.993943   1.993943   1.993943 
                             eform |         0          0          0 
                      
                      . clear
                      
                      . svmat v, names(matcol)
                      number of observations will be reset to 9
                      Press any key to continue, or Break to abort
                      number of observations (_N) was 0, now 9
                      
                      . list, sep(0)
                      
                           +----------------------------------+
                           |      vmpg   vheadroom     v_cons |
                           |----------------------------------|
                        1. | -259.1057   -334.0215   12683.31 |
                        2. |  58.42485    399.5499   2074.497 |
                        3. | -4.434854   -.8359943   6.113922 |
                        4. |  .0000329    .4059628   4.72e-08 |
                        5. | -375.6015   -1130.701   8546.885 |
                        6. | -142.6098    462.6585   16819.74 |
                        7. |        71          71         71 |
                        8. |  1.993943    1.993943   1.993943 |
                        9. |         0           0          0 |
                           +----------------------------------+
                      
                      .
                      Originally posted by paulvonhippel View Post
                      So how would I copy the contents of r(table) into a dataset?

                      Comment


                      • #26
                        OK, this is helpful, but the dataset doesn't have a column indicating what row refers to what value. E.g., it doesn't say that the first row is the coefficients and the second is the SEs. Any way to get those?

                        Comment


                        • #27
                          Yes there are many ways. For example like this:

                          Code:
                          . local rownames : rowfullnames v
                          
                          . dis "`rownames'"
                          b se t pvalue ll ul df crit eform
                          
                          . generate stat = "", before(vmpg)
                          (9 missing values generated)
                          
                          . local j=1
                          
                          . foreach lname of local   rownames   {
                            2. replace stat = "`lname'" in `j'
                            3. local ++j
                            4. }
                          
                          . list, sep(0)
                          
                               +-------------------------------------------+
                               |   stat        vmpg   vheadroom     v_cons |
                               |-------------------------------------------|
                            1. |      b   -259.1057   -334.0215   12683.31 |
                            2. |     se    58.42485    399.5499   2074.497 |
                            3. |      t   -4.434854   -.8359943   6.113922 |
                            4. | pvalue    .0000329    .4059628   4.72e-08 |
                            5. |     ll   -375.6015   -1130.701   8546.885 |
                            6. |     ul   -142.6098    462.6585   16819.74 |
                            7. |     df          71          71         71 |
                            8. |   crit    1.993943    1.993943   1.993943 |
                            9. |  eform           0           0          0 |
                               +-------------------------------------------+

                          Originally posted by paulvonhippel View Post
                          OK, this is helpful, but the dataset doesn't have a column indicating what row refers to what value. E.g., it doesn't say that the first row is the coefficients and the second is the SEs. Any way to get those?

                          Comment


                          • #28
                            Unfortunately, even prefixing coefficient name with the matrix name isn't enough to ensure that the result is a legitimate Stata variable name. Following on Joro's technique for inserting rownames into the dataset, if we transpose the results table we can avoid this problem.
                            Code:
                            . sysuse auto, clear
                            (1978 Automobile Data)
                            
                            . quietly regress mpg weight i.foreign
                            
                            . matrix v = r(table)
                            
                            . matlist v
                            
                                         |                   0b.         1.          
                                         |    weight    foreign    foreign      _cons
                            -------------+--------------------------------------------
                                       b | -.0065879          0  -1.650029    41.6797
                                      se |  .0006371          .   1.075994   2.165547
                                       t | -10.34022          .  -1.533493   19.24673
                                  pvalue |  8.28e-16          .   .1295987   6.90e-30
                                      ll | -.0078583          .    -3.7955   37.36172
                                      ul | -.0053175          .   .4954422   45.99768
                                      df |        71         71         71         71
                                    crit |  1.993943   1.993943   1.993943   1.993943
                                   eform |         0          0          0          0
                            
                            . clear
                            
                            . svmat v, names(matcol)
                            invalid syntax
                            r(198);
                            
                            . matrix v = v'
                            
                            . svmat v, names(col)
                            number of observations will be reset to 4
                            Press any key to continue, or Break to abort
                            number of observations (_N) was 0, now 4
                            
                            . local rownames : rowfullnames v
                            
                            . macro list _rownames
                            _rownames:      weight 0b.foreign 1.foreign _cons
                            
                            . generate coef = ""
                            (4 missing values generated)
                            
                            . order coef, first
                            
                            . local j 0
                            
                            . foreach lname of local rownames {
                              2.     quietly replace coef = "`lname'" in `++j'
                              3. }
                            
                            . list, clean noobs
                            
                                      coef           b         se           t     pvalue          ll          ul   df       crit   eform  
                                    weight   -.0065879   .0006371   -10.34022   8.28e-16   -.0078583   -.0053175   71   1.993943       0  
                                0b.foreign           0          .           .          .           .           .   71   1.993943       0  
                                 1.foreign   -1.650029   1.075994   -1.533493   .1295987   -3.795501    .4954422   71   1.993943       0  
                                     _cons     41.6797   2.165547    19.24673   6.90e-30    37.36172    45.99768   71   1.993943       0  
                            
                            .
                            Last edited by William Lisowski; 15 Aug 2020, 06:23.

                            Comment


                            • #29
                              This postscript is directed to the new user of Stata who encounters this discussion.

                              Capturing and saving the regression table as shown in post #28 can be helpful in limited circumstances. I expect I will find it helpful when I need to prepare tabular output of regression results in a very specific format to meet my needs. But in general, I agree with Joro's suggestion in post #5: moving estimates into a Stata dataset is often a second-best solution to a problem.

                              If you see this and think "this is how I can get the coefficients from a model to calculate predicted values and residuals", then, no, this not how to do that in Stata. You need to learn about Stata "postestimation" commands like predict. See Chapter 20 Estimation and postestimaion commands of the Stata User's Guide PDF included in your Stata installation and accessible through Stata's Help menu.

                              Or perhaps you think "this is how I can get the coefficients from a model built on a training dataset to use to calculate predictions for the new data as it is received in the future." Again, no, this is not how to do that in Stata. You need to learn about saving estimation results and recalling them at a later time, using the estimates set of commands. Again, this is described in the same Chapter 20 of the Stata User's Guide.

                              Comment


                              • #30
                                Unfortunately, the Forum software does not allow me to upvote #29 more than once. The importance of what William Lisowski says there cannot be overemphasized. The situations where saving r(table) in a Stata data set will be helpful are extremely few and very far between. I imagine that a life-long Stata user will encounter them perhaps once or twice in a career. For almost all purposes, the useful way to save estimation command results is with -estimates save-, as an .ster file. And you will then be able to make use of those results easily with the postestimation commands.

                                Comment

                                Working...
                                X