Announcement

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

  • Using estpost/estout/esttab to format summary stats

    I posted the following question on Stackoverflow, but have been unsuccesful in getting a solution:

    http://stackoverflow.com/questions/2...-table-command

    I have the following data and would like to produce some summary statistics in a three-way crosstabulation. Stata outputs the selected stats in rows, rather than in columns. I would also like to add further statistics like ttests and ranksum tests to each group. For regression output, I have been using eststo and esttab which works well. I haven't been successful in doing the same with statistics produced by the tabstat, tabulate, summarize or table commands.

    Code:
    clear
    input eventtime prefflag winner stakechange
    1    1    1    10
    1    2    1    5
    2    1    0    50
    2    2    0    31
    2    1    1    51
    2    2    1    20
    1    1    0    10
    2    2    1    10
    2    1    0    5
    1    2    0    8
    end
    table eventtime winner , contents(mean stakechange median stakechange n stakechange)

    The following command give the same results, but in a different format:
    Code:
    bysort eventtime winner: tabstat stakechange, stat(mean median n) columns(statistics)
    I've been trying to use that to perform the tabstat on each group in a loop, as follows:

    Code:
    egen grou = group(eventtime winner prefflag)
    forvalues i = 1/8 {
        estpost tabstat stakechange if grou == `i', stat(mean median n) columns(statistics) 
    }
    esttab, cells("count mean p50") noobs nomtitles nonum
    This is only outputting the result of the final estpost command, however. Is there another way I should be doing this?

  • #2
    estout (and esttab) can tabulate matrices, so one possibility would be to create a matrix that mimics the table structure you want and then fill it with the statistics you want. (Using, for instance, repeated calls to -summarize-).

    Comment


    • #3
      Is this what you mean? I have to add a few more observations to the sample data:


      Code:
      clear
      input eventtime prefflag winner stakechange
      1    1    1    10
      1    2    1    5
      2    1    0    50
      2    2    0    31
      2    1    1    51
      2    2    1    20
      1    1    0    10
      2    2    1    10
      2    1    0    5
      3    2    0    8
      4    2    0    8
      5    2    0    8
      5    2    1    8
      3    1    1    8
      4    1    1    8
      5    1    1    8
      5    1    1    8
      end
      Code:
      eststo clear
      matrix res = J(4,12,.)
      local acrossvars = 6
      local rown ""
      foreach eventtime  of numlist 2 3 4 5 {
          foreach num of numlist 1 0  {
                  local i = `eventtime'
                  qui ttest stakechange = 1 if eventtime == `eventtime' & winner == `num'
                  matrix res[(`eventtime'-1),(`num' * `acrossvars')+1] = r(mu_1)
                  matrix res[(`eventtime'-1),(`num' * `acrossvars')+2] = r(p)
                  matrix res[(`eventtime'-1),(`num' * `acrossvars')+3] = r(N_1)
                  qui summarize stakechange if eventtime == `eventtime' & winner == `num', detail
                  matrix res[(`eventtime'-1),(`num' * `acrossvars')+4] = r(p25)
                  matrix res[(`eventtime'-1),(`num' * `acrossvars')+5] = r(p50)
                  matrix res[(`eventtime'-1),(`num' * `acrossvars')+6] = r(p75)
                  if (`num' ==1) local rown "`rown' `eventtime'"
          }
      }
      matrix rownames res = `rown'
      matrix colnames res = Mean Prob Count P25 P50 P75 Mean Prob Count P25 P50 P75
      matlist res, underscore cspec(o4&o2 %10s & ///
      %8.4g & %8.3g & %8.0g & %8.3g & %8.3g & %8.3g & ///
      %8.4g & %8.3g & %8.0g & %8.3g & %8.3g & %8.3g ///
      o2& ) rspec(&&&&&&) ///
      rowtitle ("Eventtime")
      I want to output this matrix to Latex with the mean (in col2) marked with an asterix indicating the pvalue (in col3). How can I apply a star format to the 'Prob' column and concatenate it to the Mean column?

      Also, this seems a very inelegant way of getting a two-way crosstab displaying the mean, P25, median, P75 and count and adding the result of a ttest. Is there any other way of doing this with less code?

      The following code is more parsimonious and outputs the mean, P25, median, P75 and count in a two-way crosstab, but I can't find a way of either adding the result of the ttest or applying astericks to the mean to indicated the pvalue of the result of the ttest.

      Code:
      eststo clear
      bysort winner: eststo: estpost tabstat stakechange, by(eventtime) statistics(mean p25 p50 p75 count) columns(statistics) listwise nototal
      bysort winner eventtime: ttest stakechange = 1
      esttab, replace noisily  cells("mean(fmt(2))  count(fmt(0)) p25(fmt(2)) p50(fmt(2)) p75(fmt(2))") noobs nomtitle nonumber
      esttab using "test.tex", replace noisily  cells("mean(fmt(2))  count(fmt(0)) p25(fmt(2)) p50(fmt(2)) p75(fmt(2))") noobs nomtitle nonumber

      Last edited by tobriain; 27 May 2014, 09:51.

      Comment


      • #4
        You could group eventtime and winner into one composite variable with egen, then in tabstat use the by() option instead.
        Code:
        egen compvar = group(eventtime winner), label
        
        eststo res: estpost tabstat stakechange, by(compvar) ///
            statistics(mean count p25 p50 p75) columns(statistics) listwise
        esttab res, cells("mean count p25 p50 p75") ///
            noobs nonote nomtitle nonumber ///
            collabels(, lhs("E W")) addnotes("E = Eventtime; W = Winner.")
        which should produce this
        Code:
        -----------------------------------------------------------------------------
        E W                  mean        count          p25          p50          p75
        -----------------------------------------------------------------------------
        1 0                     9            2            8            9           10
        1 1                   7.5            2            5          7.5           10
        2 0              28.66667            3            5           31           50
        2 1                    27            3           10           20           51
        Total                  20           10            8           10           31
        -----------------------------------------------------------------------------
        E = Eventtime; W = Winner.


        To add the p-values from the t-tests, you could store the p-values in a matrix, then use estadd with the matrix subcommand.
        Code:
        * store the p-values in matrix pv
        matrix pv = e(mean) // just to get the column names right
        matrix rownames pv = pvalues // give it a row name
        
        local i = 1
        levelsof(compvar), local(lev)
        foreach lvl of local lev {
            quietly ttest stakechange = 1 if compvar == `lvl'
            matrix pv[1, `i'] = `r(p)'
            local ++i
        }
        quietly ttest stakechange = 1
        matrix pv[1, `i'] = `r(p)'
        
        * now add pv to existing results
        estadd matrix pv
        esttab res, cells("mean pv count p25 p50 p75") ///
            noobs nonote nomtitle nonumber ///
            collabels(, lhs("E W")) addnotes("E = Eventtime; W = Winner.")
        which should produce the following
        Code:
        ------------------------------------------------------------------------------------------
        E W                  mean           pv        count          p25          p50          p75
        ------------------------------------------------------------------------------------------
        1 0                     9     .0791668            2            8            9           10
        1 1                   7.5     .2337501            2            5          7.5           10
        2 0              28.66667      .167959            3            5           31           50
        2 1                    27     .1697428            3           10           20           51
        Total                  20     .0083746           10            8           10           31
        ------------------------------------------------------------------------------------------
        E = Eventtime; W = Winner.
        If you want the p-values from the t-tests to appear underneath the means and inside parentheses, change the cells() option to cells("mean count p25 p50 p75" "pv(par)").

        Regarding the asterisks, I don't think there is an easy way to do that. I believe the star option relies on e(b) and e(V), but to manipulate them one would need to write a program with the eclass option to return results in e(). b and V seem to be reserved names in e() and cannot simply be estadd'ed. The "*" being non-numeric cannot be stored in a matrix either. There is the labcol2() option in estout that allows you to print strings in the second column, though.


        The code above is adapted from:
        http://repec.org/bocode/e/estout/est...tml#estpost104 - Ben Jann's original examples on using estpost tabstat with the by() option
        http://www.stata.com/statalist/archi.../msg00095.html - Nick Cox suggested the -egen, label- as the best way to group categorical variables

        Comment


        • #5
          Dear Stata users,

          I am running the following code:

          forval i=1/10 {
          regress var_MA22Return`i', vce(bootstrap, reps(500))
          eststo
          }

          esttab using d:\example.csv, cells("z(star label(t-stat.))")

          That's how table looks like in excel:

          Click image for larger version

Name:	esttab_out.GIF
Views:	2
Size:	6.1 KB
ID:	1316266


          My desired output should look like that table:

          Click image for larger version

Name:	esttab_desired.GIF
Views:	1
Size:	4.4 KB
ID:	1316267



          What do I do to achieve the desired table?

          Thank you.
          Attached Files

          Comment

          Working...
          X