Announcement

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

  • Table of ttest with means, number of observations, and differences with significance stars

    Hello,

    I'm doing a ttest and exporting the results as follows:

    Code:
    ttest female hhsize housing toilet if sector==2, by(fuel)
    
    cells("b(pattern(1 1 0) fmt(3) label(Mean)) b(pattern(0 0 1) fmt(3) label(Diff.) star)")  ///
    title("T-test of comparisons of means") nonum mlab("Solid fuel" "Clean fuel" "Difference") ///
    statrlevels(* 0.1 ** 0.05 *** 0.01)
    I also want to include columns showing the number of observations for each of the variables according to the grouping variable. How can I do that?



  • #2
    You haven't given us the commands you actually tried, as they would have given you an error message. It is important that when you give us code, you give us real code.

    Anyhow, you should probably look at dtable
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Hello,

      I was able to do it using asdoc as follows:

      Code:
      asdoc ttest female if sector==2, by(fuel) replace stat(obs mean dif p)
      asdoc ttest hhsize if sector==2, by(fuel) rowappend stat(obs mean dif p)
      asdoc ttest housing if sector==2, by(fuel) rowappend stat(obs mean dif p)
      asdoc ttest toilet if sector==2, by(fuel) rowappend stat(obs mean dif p)
      But I want the output in a different format, so I'll have a look at dtable as you suggested.

      Thank you.
      Last edited by Varsha Vaishnav; 22 May 2024, 07:21.

      Comment


      • #4
        Hello Maarten,

        I'm able to get what I want using the below codes. These are borrowed from Andrew's responses on other posts.

        Code:
        drop if sector==1
        eststo m1: mean hhsize housing if fuel, over(fuel)
        matrix N1 = [e(N),e(N)]
        mat colname N1 = [email protected] [email protected]
        estadd mat N1: m1
        preserve
        replace fuel= abs(fuel-1)
        eststo m2: mean hhsize housing if fuel, over(fuel)
        matrix N1 = [e(N),e(N)]
        mat colname N1 = [email protected] [email protected]
        estadd mat N1: m2
        restore
        local i 0
        mean hhsize housing, over(fuel)
        foreach var in hhsize housing {
            local ++i
            lincom _b[c.`var'@0.fuel] - _b[c.`var'@1.fuel]
            mat D`i'= `:di %3.2f `r(estimate)''
            mat colname D`i'= "c.`var'@1.fuel"
            mat pval`i'= `r(p)'
            mat colname pval`i'= "c.`var'@1.fuel"
        }
        mat D = D1,D2
        mat pval = pval1,pval2
        estadd mat D: m1
        estadd mat pval: m1
        esttab m2 m1 m1, noobs nonumb cells("b(pattern(1 1 0)) N1(pattern(1 1 0)) D(star pval(pval) pattern(0 0 1))") starl(* 0.1 ** 0.05 *** 0.01)
        I have a small doubt. Some of the variables have more than two categories. I'm creating a separate variable for each category and then doing the ttest. Is there any way to skip this step and proceed directly?

        Thanks.

        Comment


        • #5
          Varsha Vaishnav, to Maarten's point in #2, you are showing us code without a data example. Few people are willing to invest the time to create a reproducible example from that. From the thread you referenced, I created a reproducible example using the auto dataset. Either use that or include your own data example. I have not responded yet because you have not shown us what your current table looks like and explained what additions you need. Doing this will increase your chances of getting helpful and timely replies.

          Comment


          • #6
            Hello Andrew,

            Sorry about it.

            Data example:

            Code:
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input byte(female religion) float(hhsize housing) byte(toilet fuel)
            0 3  4 0 1 1
            1 3  4 0 1 1
            0 3  6 1 1 0
            1 3  6 1 1 0
            0 3  3 0 1 1
            0 3  6 1 1 0
            0 3  4 0 1 1
            1 3  4 0 1 1
            0 3  5 0 1 1
            1 3  6 0 1 1
            0 3  7 0 1 1
            1 3  7 0 1 1
            1 3  5 1 1 0
            1 3  5 1 1 0
            1 3  6 1 1 0
            0 3  9 0 1 0
            1 2  4 1 1 1
            1 3  3 0 1 0
            1 3  6 1 1 1
            0 3  5 1 1 1
            0 3  5 1 1 1
            1 3  5 1 1 1
            0 2  5 1 1 1
            0 2  5 1 1 1
            0 2  5 1 1 1
            0 3  6 0 1 0
            0 3  6 0 1 0
            0 3  5 0 1 0
            1 3  6 0 1 0
            1 3  6 0 1 0
            1 3  6 0 1 0
            0 3  6 0 1 0
            1 3  6 0 1 0
            0 3  8 0 1 0
            1 3  8 0 1 0
            0 3  7 0 1 0
            1 3  8 0 1 0
            0 3  8 0 1 0
            0 3  4 0 1 0
            1 3  4 0 1 0
            0 3 13 0 1 0
            0 3 10 0 1 0
            0 3 10 0 1 0
            1 3 10 0 1 1
            0 3 10 0 1 1
            0 3 10 0 1 0
            0 3  5 1 0 1
            1 3  5 0 1 0
            0 3  5 0 1 0
            0 3  4 1 0 1
            0 3  4 1 0 1
            1 3  5 1 1 1
            1 3  5 1 1 1
            1 3  6 1 1 1
            0 3  4 0 1 0
            0 3  4 0 1 0
            1 3  3 0 1 1
            1 3 10 0 1 0
            1 3  7 0 1 1
            1 3  7 0 1 1
            1 3  7 0 1 1
            1 3  8 0 1 0
            1 3  5 0 1 1
            1 3  5 0 1 1
            0 3  6 0 1 0
            1 3  6 0 1 0
            1 3  5 0 1 1
            0 1  5 0 1 1
            1 1  5 0 1 1
            1 1  5 0 1 1
            1 1  4 0 1 1
            1 1  4 0 1 1
            1 1  5 1 0 0
            0 1  5 1 0 0
            1 1  5 0 1 1
            end
            label values female female
            label def female 0 "Male", modify
            label def female 1 "Female", modify
            label values religion religion
            label def religion 1 "Hindu", modify
            label def religion 2 "Muslim", modify
            label def religion 3 "Other", modify
            label values fuel cooking_fuels
            label def cooking_fuels 0 "Solid fuels", modify
            label def cooking_fuels 1 "Clean fuels", modify
            For some variables, like religion in the data example, there are many categories and I want to test the mean difference for each of the category by the grouping variable. I'm generating a separate variable for each of the category and then doing the test. Is there any way to skip this step and carry out the test directly?

            Thank you.

            Comment


            • #7
              You can use a loop of this form:

              Code:
              sysuse auto, clear
              mean mpg, over(rep78)
              
              qui levelsof rep78, local(levels)
              foreach l of local levels{
                  foreach m of local levels{
                      if `l'<`m'{
                          lincom _b[c.mpg@`l'.rep78] - _b[c.mpg@`m'.rep78]
                      }
                  }
              }
              Res.:

              Code:
              . qui levelsof rep78, local(levels)
              
              . 
              . foreach l of local levels{
                2. 
              .     foreach m of local levels{
                3. 
              .         if `l'<`m'{
                4. 
              .             lincom _b[c.mpg@`l'.rep78] - _b[c.mpg@`m'.rep78]
                5. 
              .         }
                6. 
              .     }
                7. 
              . }
              
               ( 1)  [email protected] - [email protected] = 0
              
              ------------------------------------------------------------------------------
                      Mean | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
              -------------+----------------------------------------------------------------
                       (1) |      1.875   3.281101     0.57   0.570    -4.672335    8.422335
              ------------------------------------------------------------------------------
              
               ( 1)  [email protected] - [email protected] = 0
              
              ------------------------------------------------------------------------------
                      Mean | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
              -------------+----------------------------------------------------------------
                       (1) |   1.566667   3.093814     0.51   0.614    -4.606943    7.740277
              ------------------------------------------------------------------------------
              
               ( 1)  [email protected] - [email protected] = 0
              
              ------------------------------------------------------------------------------
                      Mean | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
              -------------+----------------------------------------------------------------
                       (1) |  -.6666667   3.217599    -0.21   0.836    -7.087285    5.753952
              ------------------------------------------------------------------------------
              
               ( 1)  [email protected] - [email protected] = 0
              
              ------------------------------------------------------------------------------
                      Mean | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
              -------------+----------------------------------------------------------------
                       (1) |  -6.363636    3.99152    -1.59   0.116    -14.32859    1.601318
              ------------------------------------------------------------------------------
              
               ( 1)  [email protected] - [email protected] = 0
              
              ------------------------------------------------------------------------------
                      Mean | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
              -------------+----------------------------------------------------------------
                       (1) |  -.3083333   1.528827    -0.20   0.841    -3.359059    2.742393
              ------------------------------------------------------------------------------
              
               ( 1)  [email protected] - [email protected] = 0
              
              ------------------------------------------------------------------------------
                      Mean | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
              -------------+----------------------------------------------------------------
                       (1) |  -2.541667   1.765946    -1.44   0.155    -6.065558    .9822242
              ------------------------------------------------------------------------------
              
               ( 1)  [email protected] - [email protected] = 0
              
              ------------------------------------------------------------------------------
                      Mean | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
              -------------+----------------------------------------------------------------
                       (1) |  -8.238636   2.949213    -2.79   0.007     -14.1237   -2.353574
              ------------------------------------------------------------------------------
              
               ( 1)  [email protected] - [email protected] = 0
              
              ------------------------------------------------------------------------------
                      Mean | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
              -------------+----------------------------------------------------------------
                       (1) |  -2.233333   1.387309    -1.61   0.112    -5.001666    .5349992
              ------------------------------------------------------------------------------
              
               ( 1)  [email protected] - [email protected] = 0
              
              ------------------------------------------------------------------------------
                      Mean | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
              -------------+----------------------------------------------------------------
                       (1) |  -7.930303   2.739328    -2.89   0.005    -13.39655   -2.464059
              ------------------------------------------------------------------------------
              
               ( 1)  [email protected] - [email protected] = 0
              
              ------------------------------------------------------------------------------
                      Mean | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
              -------------+----------------------------------------------------------------
                       (1) |   -5.69697   2.878398    -1.98   0.052    -11.44072    .0467832
              ------------------------------------------------------------------------------
              
              .
              Last edited by Andrew Musau; 23 May 2024, 08:39.

              Comment


              • #8
                Hello, Andrew

                I was not clear with my question, I guess. Just an example to explain myself better:
                The variable 'religion' in the data example has three categories: Hindu, Muslim, and Others. I want to perform a t-test for each category by grouping variable fuel, which is binary. I'm generating separate variables for Hindu, Muslim, and Others using
                Code:
                tab religion, gen(rel)
                , and then performing the test. I was asking if we can perform the test without generating the variables. I hope I'm making myself clear this time.

                Thank you.

                Comment


                • #9
                  "rep78" is the categorical variable in my example and "mpg" is the continuous variable. You just substitute these with your corresponding variables.

                  Comment


                  • #10
                    Or if your question is how to use the ttest command to run the tests rather than lincom following the mean command, then:

                    Code:
                    sysuse auto, clear
                    qui levelsof rep78, local(levels)
                    foreach l of local levels{
                        foreach m of local levels{
                            if `l'<`m'{
                                ttest mpg if inlist(rep78, `l', `m'), by(rep78) 
                            }
                        }
                    }
                    Res.:

                    Code:
                    . qui levelsof rep78, local(levels)
                    
                    . 
                    . foreach l of local levels{
                      2. 
                    .     foreach m of local levels{
                      3. 
                    .         if `l'<`m'{
                      4. 
                    .             ttest mpg if inlist(rep78, `l', `m'), by(rep78) 
                      5. 
                    .         }
                      6. 
                    .     }
                      7. 
                    . }
                    
                    Two-sample t test with equal variances
                    ------------------------------------------------------------------------------
                       Group |     Obs        Mean    Std. err.   Std. dev.   [95% conf. interval]
                    ---------+--------------------------------------------------------------------
                           1 |       2          21           3    4.242641   -17.11861    59.11861
                           2 |       8      19.125    1.328768    3.758324    15.98296    22.26704
                    ---------+--------------------------------------------------------------------
                    Combined |      10        19.5    1.166667    3.689324    16.86082    22.13918
                    ---------+--------------------------------------------------------------------
                        diff |               1.875    3.021731               -5.093125    8.843125
                    ------------------------------------------------------------------------------
                        diff = mean(1) - mean(2)                                      t =   0.6205
                    H0: diff = 0                                     Degrees of freedom =        8
                    
                        Ha: diff < 0                 Ha: diff != 0                 Ha: diff > 0
                     Pr(T < t) = 0.7239         Pr(|T| > |t|) = 0.5522          Pr(T > t) = 0.2761
                    
                    Two-sample t test with equal variances
                    ------------------------------------------------------------------------------
                       Group |     Obs        Mean    Std. err.   Std. dev.   [95% conf. interval]
                    ---------+--------------------------------------------------------------------
                           1 |       2          21           3    4.242641   -17.11861    59.11861
                           3 |      30    19.43333    .7560991    4.141325    17.88694    20.97973
                    ---------+--------------------------------------------------------------------
                    Combined |      32    19.53125    .7239904    4.095508    18.05466    21.00784
                    ---------+--------------------------------------------------------------------
                        diff |            1.566667    3.026892               -4.615071    7.748404
                    ------------------------------------------------------------------------------
                        diff = mean(1) - mean(3)                                      t =   0.5176
                    H0: diff = 0                                     Degrees of freedom =       30
                    
                        Ha: diff < 0                 Ha: diff != 0                 Ha: diff > 0
                     Pr(T < t) = 0.6957         Pr(|T| > |t|) = 0.6085          Pr(T > t) = 0.3043
                    
                    Two-sample t test with equal variances
                    ------------------------------------------------------------------------------
                       Group |     Obs        Mean    Std. err.   Std. dev.   [95% conf. interval]
                    ---------+--------------------------------------------------------------------
                           1 |       2          21           3    4.242641   -17.11861    59.11861
                           4 |      18    21.66667     1.16316     4.93487    19.21261    24.12072
                    ---------+--------------------------------------------------------------------
                    Combined |      20        21.6    1.067215     4.77273    19.36629    23.83371
                    ---------+--------------------------------------------------------------------
                        diff |           -.6666667    3.651484               -8.338149    7.004816
                    ------------------------------------------------------------------------------
                        diff = mean(1) - mean(4)                                      t =  -0.1826
                    H0: diff = 0                                     Degrees of freedom =       18
                    
                        Ha: diff < 0                 Ha: diff != 0                 Ha: diff > 0
                     Pr(T < t) = 0.4286         Pr(|T| > |t|) = 0.8572          Pr(T > t) = 0.5714
                    
                    Two-sample t test with equal variances
                    ------------------------------------------------------------------------------
                       Group |     Obs        Mean    Std. err.   Std. dev.   [95% conf. interval]
                    ---------+--------------------------------------------------------------------
                           1 |       2          21           3    4.242641   -17.11861    59.11861
                           5 |      11    27.36364    2.632913    8.732385    21.49714    33.23013
                    ---------+--------------------------------------------------------------------
                    Combined |      13    26.38462    2.332981    8.411683    21.30149    31.46774
                    ---------+--------------------------------------------------------------------
                        diff |           -6.363636    6.475348               -20.61578    7.888508
                    ------------------------------------------------------------------------------
                        diff = mean(1) - mean(5)                                      t =  -0.9827
                    H0: diff = 0                                     Degrees of freedom =       11
                    
                        Ha: diff < 0                 Ha: diff != 0                 Ha: diff > 0
                     Pr(T < t) = 0.1734         Pr(|T| > |t|) = 0.3469          Pr(T > t) = 0.8266
                    
                    Two-sample t test with equal variances
                    ------------------------------------------------------------------------------
                       Group |     Obs        Mean    Std. err.   Std. dev.   [95% conf. interval]
                    ---------+--------------------------------------------------------------------
                           2 |       8      19.125    1.328768    3.758324    15.98296    22.26704
                           3 |      30    19.43333    .7560991    4.141325    17.88694    20.97973
                    ---------+--------------------------------------------------------------------
                    Combined |      38    19.36842     .651534    4.016325    18.04829    20.68855
                    ---------+--------------------------------------------------------------------
                        diff |           -.3083333     1.61937               -3.592568    2.975901
                    ------------------------------------------------------------------------------
                        diff = mean(2) - mean(3)                                      t =  -0.1904
                    H0: diff = 0                                     Degrees of freedom =       36
                    
                        Ha: diff < 0                 Ha: diff != 0                 Ha: diff > 0
                     Pr(T < t) = 0.4250         Pr(|T| > |t|) = 0.8501          Pr(T > t) = 0.5750
                    
                    Two-sample t test with equal variances
                    ------------------------------------------------------------------------------
                       Group |     Obs        Mean    Std. err.   Std. dev.   [95% conf. interval]
                    ---------+--------------------------------------------------------------------
                           2 |       8      19.125    1.328768    3.758324    15.98296    22.26704
                           4 |      18    21.66667     1.16316     4.93487    19.21261    24.12072
                    ---------+--------------------------------------------------------------------
                    Combined |      26    20.88462    .9187398    4.684672    18.99244     22.7768
                    ---------+--------------------------------------------------------------------
                        diff |           -2.541667     1.96429               -6.595762    1.512428
                    ------------------------------------------------------------------------------
                        diff = mean(2) - mean(4)                                      t =  -1.2939
                    H0: diff = 0                                     Degrees of freedom =       24
                    
                        Ha: diff < 0                 Ha: diff != 0                 Ha: diff > 0
                     Pr(T < t) = 0.1040         Pr(|T| > |t|) = 0.2080          Pr(T > t) = 0.8960
                    
                    Two-sample t test with equal variances
                    ------------------------------------------------------------------------------
                       Group |     Obs        Mean    Std. err.   Std. dev.   [95% conf. interval]
                    ---------+--------------------------------------------------------------------
                           2 |       8      19.125    1.328768    3.758324    15.98296    22.26704
                           5 |      11    27.36364    2.632913    8.732385    21.49714    33.23013
                    ---------+--------------------------------------------------------------------
                    Combined |      19    23.89474    1.854179     8.08218    19.99925    27.79022
                    ---------+--------------------------------------------------------------------
                        diff |           -8.238636    3.307643               -15.21715    -1.26012
                    ------------------------------------------------------------------------------
                        diff = mean(2) - mean(5)                                      t =  -2.4908
                    H0: diff = 0                                     Degrees of freedom =       17
                    
                        Ha: diff < 0                 Ha: diff != 0                 Ha: diff > 0
                     Pr(T < t) = 0.0117         Pr(|T| > |t|) = 0.0234          Pr(T > t) = 0.9883
                    
                    Two-sample t test with equal variances
                    ------------------------------------------------------------------------------
                       Group |     Obs        Mean    Std. err.   Std. dev.   [95% conf. interval]
                    ---------+--------------------------------------------------------------------
                           3 |      30    19.43333    .7560991    4.141325    17.88694    20.97973
                           4 |      18    21.66667     1.16316     4.93487    19.21261    24.12072
                    ---------+--------------------------------------------------------------------
                    Combined |      48    20.27083    .6548642    4.537032    18.95342    21.58825
                    ---------+--------------------------------------------------------------------
                        diff |           -2.233333    1.327063               -4.904572     .437905
                    ------------------------------------------------------------------------------
                        diff = mean(3) - mean(4)                                      t =  -1.6829
                    H0: diff = 0                                     Degrees of freedom =       46
                    
                        Ha: diff < 0                 Ha: diff != 0                 Ha: diff > 0
                     Pr(T < t) = 0.0496         Pr(|T| > |t|) = 0.0992          Pr(T > t) = 0.9504
                    
                    Two-sample t test with equal variances
                    ------------------------------------------------------------------------------
                       Group |     Obs        Mean    Std. err.   Std. dev.   [95% conf. interval]
                    ---------+--------------------------------------------------------------------
                           3 |      30    19.43333    .7560991    4.141325    17.88694    20.97973
                           5 |      11    27.36364    2.632913    8.732385    21.49714    33.23013
                    ---------+--------------------------------------------------------------------
                    Combined |      41    21.56098    1.037733     6.64473    19.46364    23.65831
                    ---------+--------------------------------------------------------------------
                        diff |           -7.930303    2.003424               -11.98261   -3.877996
                    ------------------------------------------------------------------------------
                        diff = mean(3) - mean(5)                                      t =  -3.9584
                    H0: diff = 0                                     Degrees of freedom =       39
                    
                        Ha: diff < 0                 Ha: diff != 0                 Ha: diff > 0
                     Pr(T < t) = 0.0002         Pr(|T| > |t|) = 0.0003          Pr(T > t) = 0.9998
                    
                    Two-sample t test with equal variances
                    ------------------------------------------------------------------------------
                       Group |     Obs        Mean    Std. err.   Std. dev.   [95% conf. interval]
                    ---------+--------------------------------------------------------------------
                           4 |      18    21.66667     1.16316     4.93487    19.21261    24.12072
                           5 |      11    27.36364    2.632913    8.732385    21.49714    33.23013
                    ---------+--------------------------------------------------------------------
                    Combined |      29    23.82759    1.312191    7.066364    21.13969    26.51549
                    ---------+--------------------------------------------------------------------
                        diff |            -5.69697    2.526323               -10.88056   -.5133831
                    ------------------------------------------------------------------------------
                        diff = mean(4) - mean(5)                                      t =  -2.2550
                    H0: diff = 0                                     Degrees of freedom =       27
                    
                        Ha: diff < 0                 Ha: diff != 0                 Ha: diff > 0
                     Pr(T < t) = 0.0162         Pr(|T| > |t|) = 0.0324          Pr(T > t) = 0.9838
                    
                    .

                    Comment


                    • #11
                      Thank you, Andrew.

                      Comment

                      Working...
                      X