Announcement

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

  • Custom Table after t-test

    I have a loop command to run multiple ttests on my variables

    foreach var of varlist var1 var2 var3 var4 var5 var6 var7 var8 var9 {
    ttest `var', by (group)
    }


    I would like to get a custom table that looks like this after I output
    Group1
    Mean (SD)
    Group2
    Mean (SD)
    P
    VAR1 2.31 (1.51) 2.73 (1.83) 0.075
    VAR2
    VAR3
    VAR4
    VAR5
    VAR6
    VAR7

    I used the code below but it is not producing a table.
    collect style cell group1[mu_1] group2[mu_2] Difference[p], nformat(%5.2f)
    Last edited by May Blake; 22 Aug 2023, 18:02.

  • #2
    If you just want to see it, then this works. If you want to outreg or such, then something else. I never use collect, but I suspect that's an option.
    Code:
    sysuse auto, clear
    
    capture program drop dotests
    program dotests
        local hline = `"{hline 70}"'
        local F $5.3f
        di "`hline'"
        di              _col(19) "Group 1" _col(44) "Group 2" 
        di     "Variable"    _col(18) "Mean (SD)" _col(43) "Mean (SD)" _col(63) "P"
        di "`hline'"
        foreach var in mpg turn gear_ratio headroom trunk {
        qui ttest `var', by(foreign)
        di "`var'" _col(15) $F r(mu_1) " (" $F r(sd_1) ")" _col(40) $F r(mu_2)  " (" $F r(sd_2) ")" $F _col(60) r(p)
    }
        di "`hline'"
    end
    
    dotests

    Comment


    • #3
      HTML Code:
      https://www.statalist.org/forums/forum/general-stata-discussion/general/1439212-combining-results-from-separate-t-tests-into-one-table
      https://www.stata.com/manuals/tablesexample4.pdf

      Comment


      • #4
        Hi George, you are amazing! This gave me exactly what I wanted. Thank you so much.

        Comment


        • #5
          this is a little more flexible.

          Code:
          sysuse auto, clear
          
          capture program drop dotests
          program dotests
              syntax varlist (min=1)
              local hline = `"{hline 70}"'
              local F $5.3f
              di "`hline'"
              di              _col(19) "Group 1" _col(44) "Group 2" 
              di     "Variable"    _col(18) "Mean (SD)" _col(43) "Mean (SD)" _col(63) "P"
              di "`hline'"
              foreach var in  `varlist' {
                  qui ttest `var', by(foreign)
                  di "`var'" _col(15) $F r(mu_1) " (" $F r(sd_1) ")" _col(40) $F r(mu_2)  " (" $F r(sd_2) ")" $F _col(60) r(p)
          }
              di "`hline'"
          end
          
          dotests mpg weight weight trunk

          Comment


          • #6
            Thank you George, do you know how I can modify this for categorical variables to get N (%) rather than mean (SD)?

            Comment


            • #7
              I'd like to recommend using the dtable feature available in Stata 18. It's specifically designed for these kinds of tasks and can be quite handy. Additionally, if you're looking for a premium solution, consider using asdocx (available for $9.99). It's an enhanced version of asdoc that offers more sophisticated table creation capabilities. For the task at hand, the template(table1) in asdocx was designed specifically for it. Here are a few examples based on template1.

              Code:
              * Defaults case - mean and ci are reported
              * Categorical variables are prefixed by i.
              
              asdocx tab foreign mpg weight trunk i.rep78, template(table1) replace
              Click image for larger version

Name:	Confidence interval.PNG
Views:	1
Size:	66.2 KB
ID:	1724996


              Mean and standard deviation
              Code:
              * If mean and standard deviation were to be reported with continuous variables
              asdocx tab foreign mpg weight trunk i.rep78, template(table1) replace contin(mean sd)
              Click image for larger version

Name:	mean sd.PNG
Views:	1
Size:	69.9 KB
ID:	1724997

              If total column is not needed
              Code:
              * The magic of flexmat: drop column total
              flexmat dropcol, col(4)
              asdocx export
              Click image for larger version

Name:	nototal.PNG
Views:	1
Size:	59.1 KB
ID:	1724998


              To know more about asdocx, visit https://asdocx.com
              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


              • #8
                asdoc/asdocx is a great program, as are all of A Shah's.

                if you're doing a lot of rolling regressions, asreg is a life saver.

                Comment


                • #9
                  give me an example of what you want it to look like for the categorical variables.

                  Comment


                  • #10
                    Thank you George and Shah. I should probably just purchase the asdocx as I use this format quite a bit. George, the format that Shah gives above is what I am looking for in terms of the categorical variables.

                    Comment


                    • #11
                      cleaned up some issues and added the cat vars.

                      Code:
                      sysuse auto, clear
                      clonevar rep782 = rep78
                      
                      capture program drop dotests
                      program dotests
                          syntax [, CONTinuous(varlist) CATegorical(varlist)]
                          local hline = `"{hline 70}"'
                          global F %5.3f
                          global G %5.0f
                          
                          di "`hline'"
                          di    _col(19) "Group 1" _col(44) "Group 2" 
                          di     "Variable"    _col(18) "Mean (SD)" _col(43) "Mean (SD)" _col(63) "P"
                          di "`hline'"
                          foreach var in  `continuous' {
                              qui ttest `var', by(foreign)
                              di "`var'" _col(15) $F r(mu_1) " (" $F r(sd_1) ")" _col(40) $F r(mu_2)  " (" $F r(sd_2) ")" $F _col(60) r(p)
                      }
                          di "`hline'"
                          di "Categorical Variables"
                          di "`hline'"
                          foreach var in  `categorical' {
                              qui tab `var' foreign, chi2 col matcell(M)
                              di "`var', Chi2 Test" _col(60) $F r(p)
                              local V = `r(r)'
                              forv v = 1/`V' {
                                  di "`var'" _col(15) $G M[`v',1] " (" $F M[`v',1]/`r(N)' ")" _col(40) $G M[`v',2]   " (" $F M[`v',2]/`r(N)' ")" 
                              }
                      }
                      
                          di "`hline'"
                      end
                      
                      dotests , cont(mpg weight weight trunk) cat(rep78 rep782)

                      Comment


                      • #12
                        Thank you George! I came back to this post and this helped tremendously, thank you.

                        Comment

                        Working...
                        X