Announcement

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

  • Descriptives table using esttab: Adding test statistics in a new column?

    Hi

    I have a table that I create like this:

    Code:
    sysuse auto, clear
    
    // Turn categorical variable into a set of dummies to fit it into the table
    tabulate rep78, gen(repair)
    
    // Three columns of the table:
    eststo clear
    eststo: estpost tabstat price repair*, statistics(mean sd) columns(statistics)
    eststo: estpost tabstat price repair* if !foreign, statistics(mean sd) columns(statistics)
    eststo: estpost tabstat price repair* if  foreign, statistics(mean sd) columns(statistics)
    
    // Assemble table:
    esttab, cells(mean(fmt(2)) sd(fmt(2) par keep(price))) ///
            mtitle("Total" "Domestic" "Foreign") nonumbers collabels("") ///
            note(Note: SD in parentheses.)
    -esttab- is of course from SSC. The resulting table look like this:

    Code:
    ---------------------------------------------------
                        Total     Domestic      Foreign
                       Mean/%       Mean/%       Mean/%
    ---------------------------------------------------
    price             6165.26      6072.42      6384.68
                    (2949.50)    (3097.10)    (2621.92)
    repair1               .03          .04          .00
    repair2               .12          .17          .00
    repair3               .43          .56          .14
    repair4               .26          .19          .43
    repair5               .16          .04          .43
    ---------------------------------------------------
    N                      74           52           22
    ---------------------------------------------------
    Note: SD in parentheses.
    I'm quite pleased with this, but I'm also interested in whether the Domestic and Foreign groups are statistically different from one another:

    Code:
    ttest price, by(foreign)
    tabulate rep78 foreign, chi2
    How can I manipulate esttab into creating a table that looks more like this?

    Code:
    ----------------------------------------------------------------------
                        Total     Domestic      Foreign      Test
                       Mean/%       Mean/%       Mean/%
    ----------------------------------------------------------------------
    price             6165.26      6072.42      6384.68  t(72) = -.41,
                    (2949.50)    (3097.10)    (2621.92)   p = .68
    repair1               .03          .04          .00  chi2(4) = 27.6,
    repair2               .12          .17          .00   p = .00
    repair3               .43          .56          .14
    repair4               .26          .19          .43
    repair5               .16          .04          .43
    ---------------------------------------------------------------------
    N                      74           52           22
    ---------------------------------------------------------------------
    Note: SD in parentheses.

    Or if you can think of a solution not using -esttab-, I would also be interested in that -- feels a bit like I am re-inventing the wheel here, but I couldn't google a solution.


    Thanks so much
    Go
    Last edited by Gobinda Natak; 25 Feb 2023, 11:44. Reason: Source of -esttab- added

  • #2
    OK, I have now gotten this:

    Code:
    ----------------------------------------------------------------------------------
                             Tests                     Total     Domestic      Foreign
                                                      Mean/%       Mean/%       Mean/%
    ----------------------------------------------------------------------------------
    price           t(72) = -0.41, p = 0.68          6165.26      6072.42      6384.68
                                                   (2949.50)    (3097.10)    (2621.92)
    repair1        chi2(4) = 27.26, p = 0.00             .03          .04          .00
    repair2                                              .12          .17          .00
    repair3                                              .43          .56          .14
    repair4                                              .26          .19          .43
    repair5                                              .16          .04          .43
    ----------------------------------------------------------------------------------
    N                                                     74           52           22
    ----------------------------------------------------------------------------------
    Note: SD in parentheses.

    via this approach:

    Code:
    sysuse auto, clear
    
    // Turn categorical variable into a set of dummies to fit it into the table
    tabulate rep78, gen(repair)
    
    // Columns of the table:
    eststo clear
    eststo: estpost tabstat price repair*, statistics(mean sd) columns(statistics)
    
    qui ttest price, by(foreign)
    local  t: di %5.2f `r(t)'
    local  p: di %3.2f `r(p)'
    local df = `r(df_t)'
    local ttest `"t(`df') = `t', p = `p'"'
    
    eststo: estpost tabstat price repair* if !foreign, statistics(mean sd) columns(statistics)
    eststo: estpost tabstat price repair* if  foreign, statistics(mean sd) columns(statistics)
    
    qui tabulate rep78 foreign, chi2
    local chisq: di %5.2f `r(chi2)'
    local df = (`r(r)' - 1) * (`r(c)' - 1)
    local p: di %3.2f `r(p)'
    local chitest `"chi2(`df') = `chisq', p = `p'"'
    
    // Assemble table:
    esttab, cells(mean(fmt(2)) sd(fmt(2) par keep(price))) ///
            mtitle("Total" "Domestic" "Foreign") nonumbers collabels("Mean/%") ///
            note(Note: SD in parentheses.) nolz labcol2("`ttest'" "`chitest'", title(Tests) width(30))
    But is there possibly a way to re-sort the columns, so that the "Tests column is moved to the right-hand side?

    Thanks
    Go

    Comment

    Working...
    X