Announcement

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

  • Number of observations for each subgroup at bottom of table and then export to LaTeX

    I am running two separate t-tests and have created one table for the results which I want to export to LaTeX. I require the total number of observations for each subgroup (so four in total) at the bottom of the table. None of the subgroups have missing data. Any help would be much appreciated. Thanks.

    Code:
    sysuse auto, clear
    
    local vars "turn mpg weight"
    
    gen price_d = (price>5000)
    
    est clear
    eststo ttest_1: estpost ttest `vars' if price_d==1, by(foreign)
    eststo ttest_2: estpost ttest `vars' if price_d==0, by(foreign)
    
    esttab ttest_1 ttest_2 using "auto.tex", replace booktabs label alignment(S S) ///
        cells("mu_1(fmt(3)) mu_2(fmt(3)star)") collabels("Expensive" "Cheap") ///
        mgroups("Domestic" "Foreign", pattern(1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
        star(* 0.10 ** 0.05 *** 0.01) ///    
        noobs nomtitles nonum prehead({\begin{tabular}{l*{2}{cc}}\toprule)

  • #2
    I forgot to mention that I am using Stata 17.

    Comment


    • #3
      estout is from the Stata Journal/ SSC, as you are asked to explain (FAQ Advice #12). You want to output the results of the test separately for each group, otherwise you cannot have 4 statistics displayed side-by-side as you have two models. In any case, in the current form, use estadd to add the sample sizes to estimation scalars.

      Code:
      sysuse auto, clear
      
      local vars "turn mpg weight"
      
      gen price_d = (price>5000)
      
      est clear
      eststo ttest_1: estpost ttest `vars' if price_d==1, by(foreign)
      estadd scalar N_domestic =e(N_1)[1,1]
      estadd scalar N_foreign =e(N_2)[1,1]
      eststo ttset_1
      
      eststo ttest_2: estpost ttest `vars' if price_d==0, by(foreign)
      estadd scalar N_domestic =e(N_1)[1,1]
      estadd scalar N_foreign =e(N_2)[1,1]
      eststo ttset_2
      esttab ttest_1 ttest_2, cells("mu_1(fmt(3)) mu_2(fmt(3)star)") scalars(N_domestic N_foreign) sfmt(0 0) noobs
      Res.:

      Code:
      ----------------------------------------------------------------------
                            (1)                          (2)                
                                                                            
                           mu_1         mu_2            mu_1         mu_2  
      ----------------------------------------------------------------------
      turn               43.870       36.000***       39.517       34.375***
      mpg                16.913       22.429**        22.138       28.875***
      weight           3817.826     2503.571***     2920.000     1987.500***
      ----------------------------------------------------------------------
      N_domestic             23                           29                
      N_foreign              14                            8                
      ----------------------------------------------------------------------

      Comment


      • #4
        Thanks Andrew. Apologies for missing FAQ #12.

        Based on your example, is it possible to have only one row at the bottom containing the number of observations (called "N") and have "14" alongside (in the mu_2 column for (1)) and "8" alongside (in the mu_2 column for (2))? This 2016 Statalist post may contain the solution as it has some code by Nils but the website from which the data is obtained does not seem to exist anymore and I am finding it difficult to replicate.
        HTML Code:
        https://www.statalist.org/forums/forum/general-stata-discussion/general/1331109-number-of-observations-and-esttab

        Comment


        • #5
          As I said in #3, if you want 4 columns (or 1 row), then estimate 4 models. What esttab is doing is just unstacking the estimation results, but this does not apply to scalars. There is nothing special about the code that you link, it does more or less what the code in #3 is doing, i.e. pick the number of observations from an estimation matrix and set it as a scalar. For your example, you want something like

          Code:
          sysuse auto, clear
          
          local vars "turn mpg weight"
          
          gen price_d = (price>5000)
          
          est clear
          
          eststo: mean turn mpg weight if price_d & !foreign
          eststo: mean turn mpg weight if price_d & foreign
          eststo: mean turn mpg weight if !price_d & !foreign
          eststo: mean turn mpg weight if !price_d & foreign
          esttab est*, not
          Res.:

          Code:
          . esttab est*, not
          
          ----------------------------------------------------------------------------
                                (1)             (2)             (3)             (4)   
                               Mean            Mean            Mean            Mean   
          ----------------------------------------------------------------------------
          turn                43.87***           36***        39.52***        34.38***
          mpg                 16.91***        22.43***        22.14***        28.88***
          weight             3817.8***       2503.6***         2920***       1987.5***
          ----------------------------------------------------------------------------
          N                      23              14              29               8   
          ----------------------------------------------------------------------------
          * p<0.05, ** p<0.01, *** p<0.001

          Comment


          • #6
            Thanks Andrew.

            Comment

            Working...
            X