Announcement

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

  • Adding two sets of significance levels for tables using estout

    Hello everyone,

    I’m using estout from SSC.
    Stata ver. 18


    I would like to create a table where I can have two sets of significance "stars" for two different p-values and use this table in a LaTeX environment.
    One group of significance "stars" would be for the differences between two categories for the same gender group (***), and another one for the differences between gender (+++).

    I've been searching for solutions but haven't have a lot of luck so far. I would appreciate it if anyone has any tips to reach this result.
    I've been using esttab to generate all of my tables.

    I'm attaching an example of how my code looks so far by using the bplong dataset.

    My goal would be to add another comparison between both genders with bp == 1 and between those with bp == 2.
    So, difference between male and female with bp == 1; and difference between male and female with bp == 2 and add the significance levels with +, ++, +++ while keeping the current ones (*, **, ***).


    Code:
    . sysuse bplong
    (Fictional blood-pressure data)
    
    . 
    . eststo m1: mean bp if sex == 0 & when == 1
    
    Mean estimation                             Number of obs = 60
    
    --------------------------------------------------------------
                 |       Mean   Std. err.     [95% conf. interval]
    -------------+------------------------------------------------
              bp |   159.2667   1.473469      156.3183    162.2151
    --------------------------------------------------------------
    
    . 
    . eststo m2: mean bp if sex == 0 & when == 2
    
    Mean estimation                             Number of obs = 60
    
    --------------------------------------------------------------
                 |       Mean   Std. err.     [95% conf. interval]
    -------------+------------------------------------------------
              bp |   155.5167   1.967891      151.5789    159.4544
    --------------------------------------------------------------
    
    . 
    . eststo m3: mean bp if sex == 1 & when == 1
    
    Mean estimation                             Number of obs = 60
    
    --------------------------------------------------------------
                 |       Mean   Std. err.     [95% conf. interval]
    -------------+------------------------------------------------
              bp |   153.6333    1.38596        150.86    156.4066
    --------------------------------------------------------------
    
    . 
    . eststo m4: mean bp if sex == 1 & when == 2
    
    Mean estimation                             Number of obs = 60
    
    --------------------------------------------------------------
                 |       Mean   Std. err.     [95% conf. interval]
    -------------+------------------------------------------------
              bp |      147.2   1.515979      144.1665    150.2335
    --------------------------------------------------------------
    
    . 
    . eststo m5: mean bp if sex == 0
    
    Mean estimation                            Number of obs = 120
    
    --------------------------------------------------------------
                 |       Mean   Std. err.     [95% conf. interval]
    -------------+------------------------------------------------
              bp |   157.3917   1.236031      154.9442    159.8391
    --------------------------------------------------------------
    
    . 
    . mat pval= r(table)["pvalue", 1...]
    
    . 
    . estadd mat pval=pval
    
    added matrix:
                   e(pval) :  1 x 1
    
    . 
    . eststo m6: mean bp if sex == 1
    
    Mean estimation                            Number of obs = 120
    
    --------------------------------------------------------------
                 |       Mean   Std. err.     [95% conf. interval]
    -------------+------------------------------------------------
              bp |   150.4167   1.064357      148.3091    152.5242
    --------------------------------------------------------------
    
    . 
    . mat pval= r(table)["pvalue", 1...]
    
    . 
    . estadd mat pval=pval
    
    added matrix:
                   e(pval) :  1 x 1
    
    . 
    . esttab , main(b) aux(sd)
    
    ------------------------------------------------------------------------------------------------------------
                          (1)             (2)             (3)             (4)             (5)             (6)   
                         Mean            Mean            Mean            Mean            Mean            Mean   
    ------------------------------------------------------------------------------------------------------------
    bp                  159.3***        155.5***        153.6***        147.2***        157.4***        150.4***
                      (11.41)         (15.24)         (10.74)         (11.74)         (13.54)         (11.66)   
    ------------------------------------------------------------------------------------------------------------
    N                      60              60              60              60             120             120   
    ------------------------------------------------------------------------------------------------------------
    b coefficients; sd in parentheses
    * p<0.05, ** p<0.01, *** p<0.001
    .


  • #2
    I do not understand your request. Do you have an example table that has this kind of starring? I would think that the reader would be confused by different symbols meaning the same thing. Usually, the column titles are sufficient to differentiate between samples.

    Comment


    • #3
      Hi Andrew,

      Thank you so much for your reply.
      Sorry for my late response.

      The type of table I would like to construct is similar to the table below (Wiswall, Zafar, 2021).

      I've been considering just adding another column for the p-value for the gender differences, since I understand I might not be able to achieve the same result with estout.
      I really appreciate any suggestions you might have regarding achieving a similar result.


      Click image for larger version

Name:	table.png
Views:	1
Size:	46.1 KB
ID:	1732411

      Comment


      • #4
        You can program it with a bit of work, but the format is nonstandard and probably created manually. I would recommend adding a third column for each group showing the differences with stars or the p-values of the differences and just one set of significance stars. Then in the note, the description would be "Significance stars indicate whether means or differences are significantly different from zero." "* p<0.1, ** p<0.05, *** p<0.01". You may also entirely omit the differences and only show the significance stars in the third columns.

        Code:
        sysuse auto, clear
        eststo domestic: mean mpg weight turn displacement if !foreign
        eststo foreign: mean mpg weight turn  displacement if foreign
        eststo diff: quietly estpost ttest mpg weight turn  displacement, by(foreign) unequal
        
        esttab domestic foreign diff, label collab(none) ///
        cells("b(pattern(1 1 0) fmt(2) label(Mean) star) b(pattern(0 0 1) fmt(2) label(Diff.) star)")  ///
        title("T-test of comparisons of means`=char(10)'Some additional information") ///
        nonumbers mlab("Domestic" "Foreign" "Difference", lhs("Variables")) ///
        starlevels(* 0.1 ** 0.05 *** 0.01) ///
        addnote("Significance stars indicate whether means or differences are significantly different from zero." "* p<0.1, ** p<0.05, *** p<0.01")
        Res.:

        Code:
        . esttab domestic foreign diff, label collab(none) ///
        > cells("b(pattern(1 1 0) fmt(2) label(Mean) star) b(pattern(0 0 1) fmt(2) label(Diff.) star)")  ///
        > title("T-test of comparisons of means`=char(10)'Some additional information") ///
        > nonumbers mlab("Domestic" "Foreign" "Difference", lhs("Variables")) ///
        > starlevels(* 0.1 ** 0.05 *** 0.01) ///
        > addnote("Significance stars indicate whether means or differences are significantly different from zero." "* p<0.1, ** p<0.05, *** p<0.01")
        
        T-test of comparisons of means
        Some additional information
        --------------------------------------------------------------------
        Variables                Domestic         Foreign      Difference  
        --------------------------------------------------------------------
        Mileage (mpg)               19.83***        24.77***        -4.95***
        Weight (lbs.)             3317.12***      2315.91***      1001.21***
        Turn Circle (ft.)           41.44***        35.41***         6.03***
        Displacement .. in.)       233.71***       111.23***       122.48***
        --------------------------------------------------------------------
        Observations                   52              22              74  
        --------------------------------------------------------------------
        Significance stars indicate whether means or differences are significantly different from zero.
        * p<0.1, ** p<0.05, *** p<0.01
        For regression coefficients, you can include stars for the coefficients, but for raw means, the hypothesis that the mean value is different from zero is not interesting. Therefore, as the focus will be on the difference in means in such applications, you can show stars only for the differences.

        Code:
        sysuse auto, clear
        eststo domestic: mean mpg weight turn displacement if !foreign
        eststo foreign: mean mpg weight turn displacement if foreign
        eststo diff: quietly estpost ttest mpg weight turn displacement, by(foreign) unequal
        
        esttab domestic foreign diff, label collab(none) ///
        cells("b(pattern(1 1 0) fmt(2) label(Mean)) b(pattern(0 0 1) fmt(2) label(Diff.) star)")  ///
        title("T-test of comparisons of means`=char(10)'Some additional information") ///
        nonumbers mlab("Domestic" "Foreign" "Difference", lhs("Variables")) ///
        starlevels(* 0.1 ** 0.05 *** 0.01) ///
        addnote("Significance stars shown only for differences." "* p<0.1, ** p<0.05, *** p<0.01")
        Res.:

        Code:
        .
        . esttab domestic foreign diff, label collab(none) ///
        > cells("b(pattern(1 1 0) fmt(2) label(Mean)) b(pattern(0 0 1) fmt(2) label(Diff.) star)")  ///
        > title("T-test of comparisons of means`=char(10)'Some additional information") ///
        > nonumbers mlab("Domestic" "Foreign" "Difference", lhs("Variables")) ///
        > starlevels(* 0.1 ** 0.05 *** 0.01) ///
        > addnote("Significance stars shown only for differences." "* p<0.1, ** p<0.05, *** p<0.01")
        
        T-test of comparisons of means
        Some additional information
        --------------------------------------------------------------
        Variables                Domestic      Foreign   Difference  
        --------------------------------------------------------------
        Mileage (mpg)               19.83        24.77        -4.95***
        Weight (lbs.)             3317.12      2315.91      1001.21***
        Turn Circle (ft.)           41.44        35.41         6.03***
        Displacement .. in.)       233.71       111.23       122.48***
        --------------------------------------------------------------
        Observations                   52           22           74  
        --------------------------------------------------------------
        Significance stars shown only for differences.
        * p<0.1, ** p<0.05, *** p<0.01
        Last edited by Andrew Musau; 02 Nov 2023, 04:46.

        Comment


        • #5
          Andrew,

          Thank you for the example. Yes, I agree with you that creating another column for it might be the best route.

          One final question: Would there be any way to only show the significance levels (***) for the 3rd column while suppressing its values? I would like to keep the mean and sd values for the 1st and 2nd columns, but not for the 3rd, where only the significance levels would appear.
          I know there is a "plain" command to be used with estout in order to generate a plain table with only symbols, but it seems like it can not be paired with a "normal" version of the esttab table.


          For my specific case, I would use the t-test for my second panel together with other values for mean and sd.
          Last edited by Bruna Barcellos; 03 Nov 2023, 02:56.

          Comment


          • #6
            Just adding a nonexistent statistic should do it. If you are displaying the sample sizes, you will have to blank this out for the difference. The spacing issue (missing coefficients) should resolve itself if exporting as a tex file or as a CSV file.

            Code:
            sysuse auto, clear
            eststo domestic: mean mpg weight turn displacement if !foreign
            eststo foreign: mean mpg weight turn displacement if foreign
            eststo diff: quietly estpost ttest mpg weight turn displacement, by(foreign) unequal
            estadd scalar N=.z, replace:diff
            
            esttab domestic foreign diff, label collab(none) ///
            cells("b(pattern(1 1 0) fmt(2) label(Mean)) diff(pattern(0 0 1) fmt(%1.0f) label(Diff.) star)")  ///
            title("T-test of comparisons of means`=char(10)'Some additional information") ///
            nonumbers mlab("Domestic" "Foreign" "", lhs("Variables")) ///
            starlevels(* 0.1 ** 0.05 *** 0.01) compress ///
            addnote("Significance stars represent differences in coefficients." "* p<0.1, ** p<0.05, *** p<0.01")
            Res.:

            Code:
            . esttab domestic foreign diff, label collab(none) ///
            > cells("b(pattern(1 1 0) fmt(2) label(Mean)) diff(pattern(0 0 1) fmt(%1.0f) label(Diff.) star)")  ///
            > title("T-test of comparisons of means`=char(10)'Some additional information") ///
            > nonumbers mlab("Domestic" "Foreign" "", lhs("Variables")) ///
            > starlevels(* 0.1 ** 0.05 *** 0.01) compress ///
            > addnote("Significance stars represent differences in coefficients." "* p<0.1, ** p<0.05, *** p<0.01")
            
            T-test of comparisons of means
            Some additional information
            -------------------------------------------------
            Variables         Domestic   Foreign             
            -------------------------------------------------
            Mileage (mpg)        19.83     24.77          ***
            Weight (lbs.)      3317.12   2315.91          ***
            Turn Circ..)         41.44     35.41          ***
            Displacem.. in.)    233.71    111.23          ***
            -------------------------------------------------
            Observations            52        22             
            -------------------------------------------------
            Significance stars represent differences in coefficients.
            * p<0.1, ** p<0.05, *** p<0.01
            Last edited by Andrew Musau; 03 Nov 2023, 15:32.

            Comment


            • #7
              Andrew,
              Good idea! As always, thank you for sharing your knowledge.
              Best regards!

              Comment


              • #8
                Hi, Andrew. If we are doing a ttest using
                Code:
                ttest varname1 == varname2, unpaired
                , estpost does not work. How should we then go about conducting the test and exporting the results using esttab?
                Last edited by Varsha Vaishnav; 29 Nov 2023, 11:18.

                Comment


                • #9
                  Change to a long layout:

                  Code:
                  sysuse auto, clear
                  ttest headroom= rep78, unpaired
                  frame put rep78 headroom, into(ttest)
                  frame ttest{
                      gen long obsno=_n
                      rename (rep78 headroom) Mean=
                      reshape long Mean, i(obsno) j(group) string
                      eststo test: estpost ttest Mean, by(group)
                      eststo rep78: mean Mean if group=="rep78"
                      eststo headroom: mean Mean if group=="headroom"
                      esttab headroom rep78 test, label /// 
                      cells("b(pattern(1 0 0) fmt(2) label(rep78)) b(pattern(0 1 0) fmt(3) label(headroom)) b(pattern(0 0 1) fmt(3) label(Diff.) star)") ///
                      title("T-test of comparisons of means`=char(10)'Some additional information") ///
                      starlevels(* 0.1 ** 0.05 *** 0.01) nonumb compress mlab(none)
                  }
                  frame drop ttest
                  Res.:

                  Code:
                  . ttest headroom= rep78, unpaired
                  
                  Two-sample t test with equal variances
                  ------------------------------------------------------------------------------
                  Variable |     Obs        Mean    Std. err.   Std. dev.   [95% conf. interval]
                  ---------+--------------------------------------------------------------------
                  headroom |      74    2.993243    .0983449    .8459948    2.797242    3.189244
                     rep78 |      69    3.405797    .1191738    .9899323    3.167989    3.643605
                  ---------+--------------------------------------------------------------------
                  Combined |     143    3.192308    .0784469    .9380889    3.037233    3.347382
                  ---------+--------------------------------------------------------------------
                      diff |           -.4125539    .1536669               -.7163428   -.1087649
                  ------------------------------------------------------------------------------
                      diff = mean(headroom) - mean(rep78)                           t =  -2.6847
                  H0: diff = 0                                     Degrees of freedom =      141
                  
                      Ha: diff < 0                 Ha: diff != 0                 Ha: diff > 0
                   Pr(T < t) = 0.0041         Pr(|T| > |t|) = 0.0081          Pr(T > t) = 0.9959
                  
                  
                  .     esttab headroom rep78 test, label /// 
                  >         cells("b(pattern(1 0 0) fmt(2) label(rep78)) b(pattern(0 1 0) fmt(3) label(headroom)) b(pattern(0 0 1) fmt(3) label(Diff.) star)") ///
                  >     title("T-test of comparisons of means`=char(10)'Some additional information") ///
                  >     starlevels(* 0.1 ** 0.05 *** 0.01) nonumb compress mlab(none)
                  
                  T-test of comparisons of means
                  Some additional information
                  -------------------------------------------------
                                       rep78  headroom     Diff.   
                  -------------------------------------------------
                  Mean                  2.99     3.406    -0.413***
                  -------------------------------------------------
                  Observations            74        69       143   
                  -------------------------------------------------

                  Comment


                  • #10
                    Hi, Andrew. Thank you for the reply. The code in my case goes like this:
                    Code:
                    ttest time_spent_h1=time_spent_w1 if multiple_spouse==0 & caste==1 & relative_edu==0, unpaired
                    . I have 4 and 3 categories of the variables caste and relative_edu, respectively. Do I have to repeat the process thrice or it can be done in one go? An example dataset:
                    Code:
                    * Example generated by -dataex-. To install: ssc install dataex
                    clear
                    input byte multiple_spouse float(time_spent_h1 time_spent_w1 caste relative_edu)
                    0 480 300 3 2
                    0 420   0 3 2
                    0   0 300 3 0
                    0 510   0 3 1
                    0 360 240 2 2
                    0   0   0 2 2
                    0   0   0 2 2
                    0 360 330 1 0
                    0 420   0 1 1
                    0 420   0 1 1
                    0 360   0 1 2
                    0 450   0 1 1
                    0   0   0 1 1
                    0   0   0 1 1
                    0 390 390 1 2
                    0 420   0 1 1
                    0 465 480 1 0
                    0 390   0 1 1
                    0 420   0 1 2
                    0 540 360 1 2
                    0 255   0 2 2
                    0 330   0 3 2
                    0 270   0 2 2
                    0 285   0 3 0
                    0   0   0 2 1
                    0 330   0 3 2
                    0 315   0 3 1
                    0 315   0 3 1
                    0 255   0 3 2
                    0 345   0 3 2
                    0   0   0 3 2
                    0 600   0 3 2
                    0 690   0 3 2
                    0 630   0 3 0
                    0   0   0 2 2
                    0   0   0 3 2
                    0 690   0 3 2
                    0 600   0 3 2
                    0 510   0 2 2
                    0   0 360 3 0
                    0 570   0 3 2
                    0   0   0 3 1
                    0 360   0 2 0
                    0   0   0 3 1
                    0 510   0 3 2
                    0 480   0 3 1
                    0 300   0 3 2
                    0 600   0 3 2
                    0 810   0 3 1
                    0   0   0 4 2
                    end
                    label values caste caste
                    label def caste 1 "ST", modify
                    label def caste 2 "SC", modify
                    label def caste 3 "OBC", modify
                    label def caste 4 "Others", modify
                    label values relative_edu lab_relative_edu
                    label def lab_relative_edu 0 "Husband less educated", modify
                    label def lab_relative_edu 1 "Husband equally educated", modify
                    label def lab_relative_edu 2 "Husband more educated", modify
                    Last edited by Varsha Vaishnav; 29 Nov 2023, 12:35.

                    Comment


                    • #11
                      In the frame put command, include the -if- restrictions to select the sample

                      Code:
                      frame put time_spent_h1 time_spent_w1 if multiple_spouse==0 & caste==1 & relative_edu==0, into(ttest)
                      then proceed as illustrated in #9 without thinking about these restrictions. Post if you have issues.

                      Comment


                      • #12
                        Thank you, Andrew.

                        Comment


                        • #13
                          Hello Andrew,

                          In #4, how can I add the number of observations column?

                          Comment


                          • #14
                            See https://www.statalist.org/forums/for...rs-with-esttab, but you have to filter out the parts related to appending models.

                            Comment


                            • #15
                              I tried to follow the codes in blue, but I'm getting an error message: too few variables specified. Please help.

                              Comment

                              Working...
                              X