Announcement

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

  • Using esttab to combine tabstat with regression estimates

    Hello, I would appreciate any insights as to how to combine the results from tabstat with regression estimates (beta and se) using esttab.

    In particular, the following code yields the first two columns as desired:
    Code:
    sysuse auto, clear
    gen id=1
    eststo clear
    eststo m1 : estpost tabstat mpg weight if foreign==0 & rep78==3, by(id) statistics(mean semean) columns(statistics) nototal 
    eststo m2 : estpost tabstat mpg weight if foreign==1 & rep78!=3, by(id) statistics(mean semean) columns(statistics) nototal 
    esttab m1 m2 using $tables/test.tex, cells(mean(fmt(%12.1f)) semean(fmt(%12.2f) par(( ))))  ///
            label nonumbers unstack collabels(none) eqlabels(none) ///
            mlabels("Domestic" "Foreign") nomtitles nonotes replace booktabs
    Now I would like to add a 3rd column to the previous table with the beta coefficients and standard errors from the following two regressions (combined vertically under one column header "Difference"):
    Code:
    eststo clear        
    reg mpg foreign        
    eststo m3 : reg mpg foreign
    eststo m4 : reg weight foreign
    esttab m3 m4 using $tables/test2.tex, keep(foreign)  ///
            label nonumbers unstack collabels(none) eqlabels(none) ///
            mlabels("Diff-mpg" "Diff-weight") nomtitles nonotes replace booktabs
    Thank you very much in advance.





  • #2
    estout is from SSC (FAQ Advice #12). You can build a matrix similar to what estpost tabstat does and add it to the estimation results. The code below could be generalized, but here is the direct approach.

    Code:
    sysuse auto, clear
    gen id=1
    eststo clear
    eststo m2 : estpost tabstat mpg weight if foreign==0 & rep78==3, by(id) statistics(mean semean) columns(statistics) nototal
    eststo m1 : estpost tabstat mpg weight if foreign==1 & rep78!=3, by(id) statistics(mean semean) columns(statistics) nototal
    
    reg mpg foreign  
    mat mean= J(1, 2, _b[foreign])
    mat semean= J(1, 2, _se[foreign])
    mat colnames mean= mpg weight
    mat colnames semean= mpg weight
    
    eststo m3 : reg weight foreign
    mat mean[1, 2]= _b[foreign]
    mat semean[1, 2]= _se[foreign]
    
    estadd mat mean= mean: m3
    estadd mat semean= semean: m3
    
    
    esttab m1 m2 m3, cells(mean(fmt(%12.1f)) semean(fmt(%12.2f) par(( ))))  ///
            label nonumbers unstack collabels(none) eqlabels(none) ///
            mlabels("Domestic" "Foreign" "Difference") nomtitles nonotes replace
    Res.:

    Code:
    . esttab m1 m2 m3, cells(mean(fmt(%12.1f)) semean(fmt(%12.2f) par(( ))))  ///
    >         label nonumbers unstack collabels(none) eqlabels(none) ///
    >         mlabels("Domestic" "Foreign" "Difference") nomtitles nonotes replace
    
    -----------------------------------------------------------
                             Domestic      Foreign   Difference
    -----------------------------------------------------------
    Mileage (mpg)                25.0         19.0          4.9
                               (1.62)       (0.79)       (1.36)
    Weight (lbs.)              2364.2       3442.2      -1001.2
                             (102.05)     (124.20)     (160.29)
    -----------------------------------------------------------
    Observations                   19           27           74
    -----------------------------------------------------------
    Last edited by Andrew Musau; 06 Mar 2024, 03:25.

    Comment


    • #3
      Thank you so much Andrew Musau !! When I run the code, however, the "Difference" column is empty?

      Screenshot 2024-05-17 at 09.39.18.png

      Comment


      • #4
        Update estout and try again.

        Code:
        ssc install estout, replace

        Comment


        • #5
          Thanks a lot Andrew Musau !! It works. Very last question -- if I want to add significance stars to the "Difference" column, is there a way to do that? I tried adding "star" in the options, but that didn't seem to be the solution.

          Comment


          • #6
            Code:
            sysuse auto, clear
            gen id=1
            eststo clear
            eststo m2 : estpost tabstat mpg weight if foreign==0 & rep78==3, by(id) statistics(mean semean) columns(statistics) nototal
            eststo m1 : estpost tabstat mpg weight if foreign==1 & rep78!=3, by(id) statistics(mean semean) columns(statistics) nototal
            
            reg mpg foreign  
            mat mean= J(1, 2, _b[foreign])
            mat semean= J(1, 2, _se[foreign])
            mat pmean= J(1, 2, `=r(table)["pvalue", "foreign"]')
            mat colnames mean= mpg weight
            mat colnames semean= mpg weight
            mat colnames pmean= mpg weight
            
            eststo m3 : reg weight foreign
            mat mean[1, 2]= _b[foreign]
            mat semean[1, 2]= _se[foreign]
            mat pmean[1, 2]=r(table)["pvalue", "foreign"]
            
            
            estadd mat mean= mean: m3
            estadd mat semean= semean: m3
            estadd mat pmean= pmean: m3
            
            esttab m1 m2 m3, cells(mean(fmt(%12.1f) star pvalue(pmean)) semean(fmt(%12.2f) par(( ))))  ///
                    label nonumbers unstack collabels(none) eqlabels(none)  starl( * 0.10 ** 0.05 *** 0.01) ///
                    mlabels("Domestic" "Foreign" "Difference") nomtitles replace ///
                    addnote("Standard errors in parentheses" "* p<0.1, ** p<0.05, *** p<0.01") 
            Res.:

            Code:
            . esttab m1 m2 m3, cells(mean(fmt(%12.1f) star pvalue(pmean)) semean(fmt(%12.2f) par(( ))))  ///
            >         label nonumbers unstack collabels(none) eqlabels(none)  starl( * 0.10 ** 0.05 *** 0.01) ///
            >                 mlabels("Domestic" "Foreign" "Difference") nomtitles replace ///
            >                 addnote("Standard errors in parentheses" "* p<0.1, ** p<0.05, *** p<0.01") 
            
            --------------------------------------------------------------------
                                     Domestic         Foreign      Difference   
            --------------------------------------------------------------------
            Mileage (mpg)                25.0            19.0             4.9***
                                       (1.62)          (0.79)          (1.36)   
            Weight (lbs.)              2364.2          3442.2         -1001.2***
                                     (102.05)        (124.20)        (160.29)   
            --------------------------------------------------------------------
            Observations                   19              27              74   
            --------------------------------------------------------------------
            Standard errors in parentheses
            * p<0.1, ** p<0.05, *** p<0.01

            Comment


            • #7
              Thank you very much Andrew Musau !! I get the following error, but will try and play around to see where it is stemming from.

              Code:
              mat pmean= J(1, 2, `=r(table)["pvalue", "foreign"]')
              invalid syntax

              Comment


              • #8
                What version of Stata do you have? Note the requirement to state the version if it is not the current version. The matrix extraction method that I use in #6 was introduced in version 16. Before then, you had to save r(table) as a regular Stata matrix and reference the column and row numbers in this matrix, rather than their names. Or use additional functions to reference the names.

                Code:
                help colnumb()
                Code:
                help rownumb()
                Code:
                matrix rtable= r(table)
                mat pmean= J(1, 2, `=rtable[4, 1]')
                Code:
                mat pmean[1, 2]=rtable[4, 1]
                and so on.
                Last edited by Andrew Musau; 17 May 2024, 11:27.

                Comment

                Working...
                X