Announcement

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

  • Creating a table showing descriptive statistics AND ttest results

    Hello,
    I am using Stata 17/BE for Mac and I am struggling to create a specific table that contains means, mean diffrences and ttests results.

    For different school level, I am comparing grade means between two groups for each subject. The dummy variable a5 determines the group the subjects are in. The string variable “cours” contains three school subjects : math, fran and ang.

    I can do the first part with (example):

    tabulate course a5 if level==40, summarize(grade)

    I know how to test means using (example):

    ttest grade if level==40 & course=="math", by(a5)

    But I can’t figure out how to put what I want in just one table… Here is the kind of table I would like to have for each level:
    course a5=0 a5=1 DIFFERENCE Ttest result (Pr(|T| > |t|)
    math Mean(0)
    (std. dev.)
    Mean(1)
    (std. dev.)
    Mean(0) – mean(1) ex: 0,50
    fran Mean(0)
    (std. dev.)
    Mean(1)
    (std. dev.)
    Mean(0) – mean(1) ex: 0,005***
    ang Mean(0)
    (std. dev.)
    Mean(1)
    (std. dev.)
    Mean(0) – mean(1) ex: 0,09*
    Could someone help me please?

    Thank you very much,

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long id str4 course int level byte grade float a5
    8206906 "math" 60 52 1
    8206989 "ang"  40 66 1
    8206989 "ang"  60 37 1
    8206989 "fran" 40 76 1
    8206989 "fran" 60 51 1
    8206989 "math" 40 70 1
    8206989 "math" 60 48 1
    8212722 "ang"  40 49 0
    8212722 "ang"  60 57 0
    8212722 "fran" 40 65 0
    8212722 "fran" 60 73 0
    8212722 "math" 40 46 0
    8212722 "math" 60 55 0
    8213951 "ang"  40 66 1
    8213951 "ang"  60 74 1
    8213951 "fran" 40 46 1
    8213951 "fran" 60 70 1
    8213951 "math" 40 46 1
    8213951 "math" 60 62 1
    8215246 "ang"  40 71 1
    8215246 "ang"  60 67 1
    end
    Last edited by Nicolas Charette; 30 Aug 2022, 13:29.

  • #2
    not sure I understand exactly but this looks very similar to a project I recently did (but I have more tests than you appear to) so here is the relevant part of the do file:
    Code:
    local results "Number=r(N_1) Admission=r(mu_1) Discharge=r(mu_2) Diff=(r(mu_2)-r(mu_1)) pvalue=r(p)"
    
    table (command) (result), ///
    command(`results' : ttest admeat=dceat) ///
    command(`results' : ttest admoral=dcoral) ///
    command(`results' : ttest admtoliet=dctoliet) ///
    command(`results' : ttest admshower=dcshower) ///
    command(`results' : ttest admubdress=dcubdress) ///
    command(`results' : ttest admlbdress=dclbdress) ///
    command(`results' : ttest admfoot=dcfoot) ///
    command(`results' : ttest admottotal=dcottotal) ///
    command(`results' : ttest admroll=dcroll) ///
    command(`results' : ttest admsitlay=dcsitlay) ///
    command(`results' : ttest admlaysit=dclaysit) ///
    command(`results' : ttest admsitstand=dcsitstand) ///
    command(`results' : ttest admbedchair=dcbedchair) ///
    command(`results' : ttest admtoliettx=dctoliettx) ///
    command(`results' : ttest admptpartial=dcptpartial) ///
    command(`results' : ttest admcar=dccar) ///
    command(`results' : ttest adm10ft=dc10ft) ///
    command(`results' : ttest adm50ft=dc50ft) ///
    command(`results' : ttest adm150ft=dc150ft) ///
    command(`results' : ttest adm10unever=dc10unever) ///
    command(`results' : ttest admcurb=dccurb) ///
    command(`results' : ttest adm4step=dc4step) ///
    command(`results' : ttest adm12=dc12) ///
    command(`results' : ttest admobject=dcobject) ///
    command(`results' : ttest admpttotal=dcpttotal)
    
    collect label list command, all
    
    collect label levels command 1 "Eating" 10 "Sit to lying" 11 "Lying to sitting" 12 "Sit to stand" ///
    13 "Bed to chair" 14 "Toilet transfers" 15 "PT Partial Summary" 16 "Car transfer" 17 "10 feet" ///
    18 "50 feet" 19 "150 feet" 2 "Oral" 20 "Uneven" ///
    21 "Curb" 22 "4 step" 23 "12 step" 24 "Object" 25 "PT Total Summary" 3 "Toileting" ///
    4 "Shower" 5 "UB dressing" 6 "LB dressing" 7 "On/off footwear" ///
    8 "OT summary" 9 "Rolling", modify
    
    collect preview
    
    collect style cell result[Admission Discharge Diff], nformat(%6.2f)
    collect style cell result[pvalue], nformat(%6.4f)
    
    noi collect preview
    
    collect layout
    
    collect layout (command) (result)
    
    collect levelsof command
    
    collect label list command
    
    noi collect layout (command[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25]) (result)

    Comment


    • #3
      Code:
      frame create results str4 course float(mean0 sd0 mean1 sd1 mean_diff p_value)
      
      levelsof course, local(courses)
      foreach c of local courses {
          ttest grade if course == "`c'", by(a5)
          frame post results ("`c'") (`r(mu_1)') (`r(sd_1)') (`r(mu_2)') (`r(sd_2)') ///
              (`r(mu_2)'-`r(mu_1)') (`r(p)')
      }
      
      frame change results
      forvalues i = 0/1 {
          egen mean_sd_`i' = concat(mean`i' sd`i'), punct(",") format(%2.1f)
          replace mean_sd_`i' = subinstr(mean_sd_`i', ",", " (", 1) + ")"
      }
      format p_value %03.2f
      
      list course mean_sd_0 mean_sd_1 mean_diff p_value, noobs clean
      Note: I have not added significance stars. I consider them an abomination and think their use should be banned, if not considered a felony. ;-)

      Added: Crossed with #2
      Last edited by Clyde Schechter; 30 Aug 2022, 13:01.

      Comment


      • #4
        Thank you Clyde,

        I will digest all that. I didn't know anything about frames, so I will read about it.

        Originally posted by Clyde Schechter View Post
        [code]


        Note: I have not added significance stars. I consider them an abomination and think their use should be banned, if not considered a felony. ;-)
        I will also take note of the stars taboo! I guess it shows how much I am new to this!

        Comment


        • #5
          Originally posted by Rich Goldstein View Post
          not sure I understand exactly but this looks very similar to a project I recently did (but I have more tests than you appear to) so here is the relevant part of the do file:
          Thank you Rich! I will also look into this. So many new commands for me... I will get back to you guys.

          Cheers,

          Nicolas

          Comment


          • #6
            Nicholas, here is some code to do this with the -collect- system (just more customised to your requirement than #2)

            Code:
            sysuse auto, clear
            collect clear
            
            local results "diff = (r(mu_1)-r(mu_2)) r(mu_1) r(mu_2) r(sd_1) r(sd_2) r(p)"
            
            #delimit ;
                table 
                    (command) (result), 
                    command(`results' : ttest price, by(foreign))
                    command(`results' : ttest mpg, by(foreign))
                    command(`results' : ttest headroom, by(foreign))
                    command(`results' : ttest trunk, by(foreign))
                ;
            
                collect label levels command 
                    1 "Price" 
                    2 "Mileage" 
                    3 "Head room" 
                    4 "Trunk space", modify
                    ;
                
                collect addtags myrows[first], fortags(result[mu_1 mu_2 diff p]) ;
                collect addtags myrows[second], fortags(result[sd_1 sd_2]) ;
                collect addtags mycols[1], fortags(result[mu_1 sd_1]) ;
                collect addtags mycols[2], fortags(result[mu_2 sd_2]) ;
                collect addtags mycols[3], fortags(result[diff]) ;
                collect addtags mycols[4], fortags(result[p]) ;
            
                collect label levels mycols 
                    1 "Domestic" 
                    2 "Foreign" 
                    3 "Difference"
                    4 "Result"
                    , modify
                    ;
            #delimit cr
            
            collect layout (command#myrows) (mycols)
            collect style cell myrows , nformat(%6.2f)
            collect style cell myrows[second], sformat((%s))
            collect style header mycols, title(hide) 
            collect style header myrows, title(hide) level(hide)
            noi collect preview
            This produces something close to what you asked for:
            Code:
            ----------------------------------------------------------
                        |   Domestic     Foreign   Difference   Result
            ------------+---------------------------------------------
            Price       |    6072.42     6384.68      -312.26     0.68
                        |  (3097.10)   (2621.92)                      
            Mileage     |      19.83       24.77        -4.95     0.00
                        |     (4.74)      (6.61)                      
            Head room   |       3.15        2.61         0.54     0.01
                        |     (0.92)      (0.49)                      
            Trunk space |      14.75       11.41         3.34     0.00
                        |     (4.31)      (3.22)                      
            ----------------------------------------------------------

            Comment


            • #7
              Thank you Hemanshu Kumar! This works and it is exactly what I wanted!

              Comment

              Working...
              X