Announcement

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

  • summary table with t-tests

    I want to produce a LaTeX table which has means by group and then t-tests of the differences. Like this:
    mean (exp_a) mean exp_b diff p-value
    All
    MBC=0
    MBC=1
    LKZ=0
    LKZ=1
    MBC
    This feels like something that should be easy but I'm not finding it so. In this post Chuck Huber (StataCorp) outlines how to do this elegantly for tests between variables using table (command) (result) but that doesn't work if I want to include an if statement to test within a given variable. In this post https://www.statalist.org/forums/for...-test-to-latex @Andrew Musau outlines a solution using expand and esttab. But, this will be slow given my dataset is large and I need to because of the large number of comparisons I need to formulate and the number of reported observations will be wrong, etc. Is there any way to do this? I would especially like to find a way of doing it without using packages from ssc as I am working on a server which doesn't allow them to be quickly installed.

    I tried something ugly like this:

    Code:
    collect clear
    local fortable "exp_a =r(mu_1) exp_b=r(mu_2) Diff=(r(mu_2)-r(mu_1)) pvalue=r(p): ttest exp_a=exp_b"
    table (command) (result),  command(`fortable')
    preserve
    keep if mbc==0
    table  (command) (result) ,  command(`fortable')   append
    restore
    preserve
    keep if mbc==1
    table  (command) (result) ,  command(`fortable')   append
    restore
    and so on for a large number of groups. But this doesn't work. Thanks as always for any bright ideas. Stu.
    Last edited by Stuart Morrison; 13 May 2022, 12:35.

  • #2
    This can be done quite easily with asdocx, that can export to LaTeX, Word, Excel, and HTML. And installation of asdocx should not be an issue because it is not installed on the server, rather it is installed on the machine you are using. See this example:

    Code:
    sysuse auto, clear
    asdocx ttest price == turn, by(foreign) template(ttest1)
    
      
     
    ------------------------------------------------------------------------------
    Variables              Cat1       Cat2          dif.     t-value     p-value
    ------------------------------------------------------------------------------
    foreign                                                                            
        0              6072.423     41.442      6030.981      19.967       0.000
        1              6384.682     35.409      6349.273      16.259       0.000
    -------------------------------------------------------------------------------
    There are numerous other features of asdocx which might be of interest to you https://fintechprofessor.com/asdocx/

    proof.
    Code:
    . ttest price == turn if foreign == 0
    
    Paired t test
    ------------------------------------------------------------------------------
    Variable |     Obs        Mean    Std. Err.   Std. Dev.   [95% Conf. Interval]
    ---------+--------------------------------------------------------------------
       price |     104    6072.423    302.2182    3082.033    5473.045    6671.802
        turn |     104    41.44231    .3871602    3.948275    40.67447    42.21015
    ---------+--------------------------------------------------------------------
        diff |     104    6030.981    302.0502     3080.32    5431.935    6630.026
    ------------------------------------------------------------------------------
         mean(diff) = mean(price - turn)                              t =  19.9668
     Ho: mean(diff) = 0                              degrees of freedom =      103
    
     Ha: mean(diff) < 0           Ha: mean(diff) != 0           Ha: mean(diff) > 0
     Pr(T < t) = 1.0000         Pr(|T| > |t|) = 0.0000          Pr(T > t) = 0.0000
    
    . ttest price == turn if foreign == 1
    
    Paired t test
    ------------------------------------------------------------------------------
    Variable |     Obs        Mean    Std. Err.   Std. Dev.   [95% Conf. Interval]
    ---------+--------------------------------------------------------------------
       price |      44    6384.682    390.6454    2591.248     5596.87    7172.493
        turn |      44    35.40909    .2236498    1.483525    34.95806    35.86012
    ---------+--------------------------------------------------------------------
        diff |      44    6349.273    390.5099    2590.349    5561.735    7136.811
    ------------------------------------------------------------------------------
         mean(diff) = mean(price - turn)                              t =  16.2589
     Ho: mean(diff) = 0                              degrees of freedom =       43
    
     Ha: mean(diff) < 0           Ha: mean(diff) != 0           Ha: mean(diff) > 0
     Pr(T < t) = 1.0000         Pr(|T| > |t|) = 0.0000          Pr(T > t) = 0.0000
    Last edited by Attaullah Shah; 14 May 2022, 04:28.
    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


    • #3
      Thanks. Unfortunately, getting a subscription for the server I connect to for asdocx isn't possible so I am going to need to find another way.

      Comment


      • #4
        as the FAQ advises, just telling us something "did not work" is not very helpful; you might want to take a look at the 4th (of 7) blogs on the new -table- command; see ttps://blog.stata.com/2021/08/24/customizable-tables-in-stata-17-part-4-table-of-statistical-tests/

        Comment


        • #5
          You should provide a reproducible example. It should be possible to loop, storing the results in a matrix and appending matrices. Then output using estout (SSC). I don't have Stata 17 to suggest a table solution.

          Code:
          webuse fuel, clear
          ttest mpg1=mpg2
          mat A=  r(mu_1), r(mu_2), `=r(mu_1)- r(mu_2)', r(p)
          mat colnames A= Mean_1 Mean_2 Diff. P-value
          mat rownames A= Mileage
          esttab mat(A), mlab(none) substitute(_ " ")
          Res.:

          Code:
          . ttest mpg1=mpg2
          
          Paired t test
          ------------------------------------------------------------------------------
          Variable |     Obs        Mean    Std. Err.   Std. Dev.   [95% Conf. Interval]
          ---------+--------------------------------------------------------------------
              mpg1 |      12          21    .7881701    2.730301    19.26525    22.73475
              mpg2 |      12       22.75    .9384465    3.250874    20.68449    24.81551
          ---------+--------------------------------------------------------------------
              diff |      12       -1.75    .7797144     2.70101    -3.46614   -.0338602
          ------------------------------------------------------------------------------
               mean(diff) = mean(mpg1 - mpg2)                               t =  -2.2444
           Ho: mean(diff) = 0                              degrees of freedom =       11
          
           Ha: mean(diff) < 0           Ha: mean(diff) != 0           Ha: mean(diff) > 0
           Pr(T < t) = 0.0232         Pr(|T| > |t|) = 0.0463          Pr(T > t) = 0.9768
          
          .
          . mat A=  r(mu_1), r(mu_2), `=r(mu_1)- r(mu_2)', r(p)
          
          .
          . mat colnames A= Mean_1 Mean_2 Diff. P-value
          
          .
          . mat rownames A= Mileage
          
          .
          . esttab mat(A), mlab(none) substitute(_ " ")
          
          ----------------------------------------------------------------
                             Mean 1       Mean 2        Diff.      P-value
          ----------------------------------------------------------------
          Mileage                21        22.75        -1.75     .0463416
          ----------------------------------------------------------------
          
          .

          Comment


          • #6
            Andrew Musau Thank you!! Yes. I was able to loop over the list of comparisons and use esttab as you suggested. Rich Goldstein I apologise for not being more explicit in my original question that the reason the approach I tried (following indeed the blog post you linked to) doesn't work is that I get the following output. Searching in Stata for cmdset reveals nothing, and other posts on this forum just suggest to use collect clear which I have already done.
            Code:
            Your layout specification does not uniquely match any items. Dimension cmdset might help uniquely match items.
            . restore

            Comment


            • #7
              Just for reference, asdocx works well with Stata on a machine or a server. The installation files are stored on the machine you are working on. They have nothing to do with the server.
              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

              Working...
              X