Announcement

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

  • A table with mean, SD, difference, and t-stat

    Hello everyone,

    I want to produce a table that looks something like this:
    Variable Name Period 1 Period 2 Difference b/w Period 1 & 2
    X (Mean) (Mean) (Difference in Mean)
    (SD) (SD) [t-stat of the difference in mean]
    Y (Mean) (Mean) (Difference in Mean)
    (SD) (SD) [t-stat of the difference in mean]
    I know the long way to producing this. Do summary stats, take the output of summary stats to get mean and SD, then use a t-test of the difference, etc. And then copy-paste all the values one by one.

    Do you have suggestions for a relatively shorter/easier way of doing this in STATA?

    Thanks!

  • #2
    Ahmed:
    see -table hypothesis tests- entry, Stata .pdf manual.
    Kind regards,
    Carlo
    (StataNow 18.5)

    Comment


    • #3
      Carlo Lazzaro I can see no examples on the Stata blog or the help manuals that do something close to what Ahmed wants.

      I'm a complete novice at the new -collect- system, but I found this completely non-trivial to program:

      Code:
      clear all
      sysuse auto, clear
      
      local vars price mpg headroom trunk
      
      local myresults "Domestic = r(mu_1) Foreign = r(mu_2) Difference = (r(mu_2)-r(mu_1)) sd1 = r(sd_1) sd2 = r(sd_2) tval = -r(t)"
      
      #delimit ;
      table (command) (result), 
          command(`myresults' : ttest price, by(foreign))
          command(`myresults' : ttest mpg, by(foreign))
          command(`myresults' : ttest headroom, by(foreign))
          command(`myresults' : ttest trunk, by(foreign))
          ;
      
          collect addtags stats[mean], fortags(result[Domestic Foreign Difference]) ;
          collect addtags stats[sd_tval], fortags(result[sd1 sd2 tval]) ;
          collect addtags vars[1], fortags(result[Domestic sd1]) ;
          collect addtags vars[2], fortags(result[Foreign sd2]) ;
          collect addtags vars[3], fortags(result[Difference tval]) ;
      
          collect label levels command 
              1 "Price" 
              2 "Mileage" 
              3 "Head room" 
              4 "Trunk space", modify
              ;
      #delimit cr
      
      collect layout (command#stats) (vars)
      collect style cell stats , nformat(%6.2f)
      collect style cell stats[sd_tval], sformat((%s))
      collect label levels vars 1 "Domestic" 2 "Foreign" 3 "Difference", modify
      collect style header vars, title(hide) 
      collect style header stats, title(hide) level(hide)
      collect preview    
      
      
      -------------------------------------------------
                  |   Domestic     Foreign   Difference
      ------------+------------------------------------
      Price       |    6072.42     6384.68       312.26
                  |  (3097.10)   (2621.92)       (0.41)
      Mileage     |      19.83       24.77         4.95
                  |     (4.74)      (6.61)       (3.63)
      Head room   |       3.15        2.61        -0.54
                  |     (0.92)      (0.49)      (-2.61)
      Trunk space |      14.75       11.41        -3.34
                  |     (4.31)      (3.22)      (-3.27)
      -------------------------------------------------
      If someone else has easier ways of accomplishing this using the new -collect- system, I would love to learn!

      Comment


      • #4
        Hemanshu:
        happy that you've found an easy way out.
        Kind regards,
        Carlo
        (StataNow 18.5)

        Comment


        • #5
          Thanks for the help. I used the same to generate the table. I have copied down my code. But the problem is the order of variables in the table is coming out to be different than what is there in my code.

          Here is the code:

          local myresults "JC_work = r(mu_1) JC_nowork = r(mu_2) Diff = (r(mu_2)-r(mu_1)) pvalue = r(p)"
          display "`myresults'"

          #delimit ;
          table (command) (result),
          command(`myresults' : ttest any_work_week, by(group))
          command(`myresults' : ttest weekly_earnings, by(group))
          command(`myresults' : ttest hrs_work_week, by(group))
          command(`myresults' : ttest d1_month, by(group))
          command(`myresults' : ttest d2_month, by(group))
          command(`myresults' : ttest d3_month, by(group))
          command(`myresults' : ttest female, by(group))
          command(`myresults' : ttest hhsize, by(group))
          command(`myresults' : ttest hhsize_15plus, by(group))
          command(`myresults' : ttest marital_status1, by(group))
          command(`myresults' : ttest marital_status2, by(group))
          command(`myresults' : ttest marital_status3, by(group))
          command(`myresults' : ttest marital_status4, by(group))
          command(`myresults' : ttest religion1, by(group))
          command(`myresults' : ttest religion2, by(group))
          command(`myresults' : ttest religion3, by(group))
          command(`myresults' : ttest social_group4, by(group))
          command(`myresults' : ttest social_group3, by(group))
          command(`myresults' : ttest social_group2, by(group))
          command(`myresults' : ttest social_group1, by(group));
          Click image for larger version

Name:	image.png
Views:	1
Size:	73.0 KB
ID:	1719194

          Comment


          • #6
            You can also use asdoc (from SSC) with the rowappend option.
            Code:
            sysuse auto
             asdoc ttest price, by(foreign) replace tzok
             asdoc ttest mpg, by(foreign) rowappend tzok
             asdoc ttest headroom, by(foreign) rowappend tzok
             asdoc ttest trunk, by(foreign) rowappend tzok
            Click image for larger version

Name:	Capture.JPG
Views:	1
Size:	64.5 KB
ID:	1719203

            asdocx provides more options, such as deleting any column from the table, or exporting the table to Excel, Word, LaTeX, or HTML.
            Regards
            --------------------------------------------------
            Attaullah Shah, PhD.
            Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
            FinTechProfessor.com
            https://asdocx.com
            Check out my asdoc program, which sends outputs to MS Word.
            For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

            Comment


            • #7
              Thank you, everyone, for posting easier solutions. Back then, I had to do the good old-fashioned copy-paste method. Next time, I hope using any of the examples above will be easier.

              Comment


              • #8
                Hi Ahmed, Can you share your code that you used to extract that table?

                Comment

                Working...
                X