Announcement

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

  • Extracting Variable Frequencies from Regression Model

    Good afternoon, all

    I am hoping to figure out if it's possible, and if so, how to retrieve frequencies and summary statistics (as in those returned by the tab and sum commands) for variables included in regression models. As I cannot share my exact data for privacy concerns, please see the below for an example.

    When I run the following code on the system's 'auto' dataset, the below model is returned.

    Code:
    sysuse auto
    
    reg price mpg foreign, robust
    
    Linear regression                               Number of obs     =         74
                                                    F(2, 71)          =      12.72
                                                    Prob > F          =     0.0000
                                                    R-squared         =     0.2838
                                                    Root MSE          =     2530.9
    
    ------------------------------------------------------------------------------
                 |               Robust
           price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
             mpg |  -294.1955   60.33645    -4.88   0.000     -414.503   -173.8881
         foreign |   1767.292   607.7385     2.91   0.005     555.4961    2979.088
           _cons |   11905.42   1362.547     8.74   0.000     9188.573    14622.26
    ------------------------------------------------------------------------------
    My question, then, is whether it is possible to retrieve from this model the number of observations, mean, and standard deviation for each of the variables included.

    Thank you in advance for any help you can offer.
    Trent

  • #2
    Code:
    summarize price mpg foreign if e(sample)
    summarizes variables for precisely the observations used in the last model fitted.

    More at https://www.stata-journal.com/articl...article=dm0030

    Comment


    • #3
      Also, after -regress- and, I believe, other estimation commands, -estat summarize- will do this, and saves you the typing of the variable list and -if e(sample)-.

      Comment


      • #4
        the number of observations is kept as "e(N)" (see the help file for regress) so you can easily get that; for the others, you cannot get them FROM the model but you can use the saved "e(sample)" to get them from, e.g., the -summarize- command by going -sum varlist if e(sample)- immediately after estimating the model; if you do something else in between estimating the model and getting the summaries there may be some surprises so use "e(sample)" to make a new variable (e.g., "gen inuse=e(sample)") and then condition on "inuse" (or whatever you call it) in your "if" condition

        Clyde Schechter is correct and the results of -estat summarize- are saved in a matrix (see "return li" after running estat summarize) and you can grab from that
        Last edited by Rich Goldstein; 23 Feb 2022, 12:05.

        Comment


        • #5
          Thank you all very much for your speedy replies! After running the above model, Nick Cox 's solution returned the following:

          Code:
          summarize price mpg foreign if e(sample)
          
              Variable |        Obs        Mean    Std. Dev.       Min        Max
          -------------+---------------------------------------------------------
                 price |         74    6165.257    2949.496       3291      15906
                   mpg |         74     21.2973    5.785503         12         41
               foreign |         74    .2972973    .4601885          0          1
          This is exactly what I was looking to do. Much appreciated!

          Comment


          • #6
            This is exactly what I was looking for. When I run Nick Cox's solution, I get the following:
            Code:
            summarize price mpg foreign if e(sample)
            
                Variable |        Obs        Mean    Std. Dev.       Min        Max
            -------------+---------------------------------------------------------
                   price |         74    6165.257    2949.496       3291      15906
                     mpg |         74     21.2973    5.785503         12         41
                 foreign |         74    .2972973    .4601885          0          1
            Thank you all kindly! Much appreciate the help.

            Comment

            Working...
            X