Announcement

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

  • T-test for the difference in the means of two subgroups in my data

    Hi everyone, I would like for your help.

    I have cross-sectional data which divided into treatment group and control group. In each group there are men and women. Now I want to check out that the groups are not different from each other in any parameter except to the treatment. So I want to calculate the means and to do t-test to see in they are statistically difference from each other, and than export the results to Excel and present them with asterisks. I attach screenshot from Lavy & Nussbaum (2023) for an example:
    Click image for larger version

Name:	1.jpg
Views:	1
Size:	109.5 KB
ID:	1776618


    So I know of curse that for means of variables I can write:
    Code:
    sum var1 if control == 1
    sum var1 if control == 0
    And so on, but I really need your help in understand how to do t-test on the means of the difference groups and how to export the results to Excel.

    I thank you already,

    Fitzgerald.
    Last edited by FitzGerald Blindman; 27 Apr 2025, 05:56.

  • #2
    You can use the mean command to obtain standard errors for the means. The following uses estout from SSC.

    Code:
    sysuse auto, clear
    eststo domestic: mean mpg weight turn displacement if !foreign
    eststo foreign: mean mpg weight turn displacement if foreign
    eststo diff: quietly estpost ttest mpg weight turn displacement, by(foreign) unequal
    
    esttab domestic foreign diff, label collab(none) ///
    cells("b(pattern(1 1 0) fmt(2) label(Mean)) b(pattern(0 0 1) fmt(2) label(Diff.) star)" ///
    se(par pattern(1 1 0) fmt(2)))  title("T-test of comparisons of means") ///
    nonumbers mlab("Domestic" "Foreign" "Difference", lhs("Variables")) ///
    starlevels(* 0.1 ** 0.05 *** 0.01) varwidth(25) ///
    addnote("Standard errors in parentheses." "* p<0.1, ** p<0.05, *** p<0.01")
    Res.:

    Code:
    . esttab domestic foreign diff, label collab(none) ///
    > cells("b(pattern(1 1 0) fmt(2) label(Mean)) b(pattern(0 0 1) fmt(2) label(Diff.) star)" ///
    > se(par pattern(1 1 0) fmt(2)))  title("T-test of comparisons of means") ///
    > nonumbers mlab("Domestic" "Foreign" "Difference", lhs("Variables")) ///
    > starlevels(* 0.1 ** 0.05 *** 0.01) varwidth(25) ///
    > addnote("Standard errors in parentheses." "* p<0.1, ** p<0.05, *** p<0.01")
    
    T-test of comparisons of means
    -------------------------------------------------------------------
    Variables                     Domestic      Foreign   Difference   
    -------------------------------------------------------------------
    Mileage (mpg)                    19.83        24.77        -4.95***
                                    (0.66)       (1.41)                
    Weight (lbs.)                  3317.12      2315.91      1001.21***
                                   (96.43)      (92.32)                
    Turn circle (ft.)                41.44        35.41         6.03***
                                    (0.55)       (0.32)                
    Displacement (cu. in.)          233.71       111.23       122.48***
                                   (11.82)       (5.30)                
    -------------------------------------------------------------------
    Observations                        52           22           74   
    -------------------------------------------------------------------
    Standard errors in parentheses.
    * p<0.1, ** p<0.05, *** p<0.01
    Export in CSV format and then open this in Excel.

    Comment


    • #3
      Also note that dtable was designed to make such descriptive statistics easy to obtain. Using the example in #2:

      Code:
      sysuse auto, clear
      dtable mpg weight turn disp, by(foreign, tests nototal) nformat(%3.0f N)
      Res.:

      Code:
      . dtable mpg weight turn disp, by(foreign, tests nototal) nformat(%3.0f N)
      note: using test regress across levels of foreign for mpg, weight, turn, and displacement.
      
      ---------------------------------------------------------------------
                                               Car origin                  
                                   Domestic            Foreign        Test 
      ---------------------------------------------------------------------
      N                               52 (70.3%)          22 (29.7%)       
      Mileage (mpg)               19.827 (4.743)      24.773 (6.611) <0.001
      Weight (lbs.)          3,317.115 (695.364) 2,315.909 (433.003) <0.001
      Turn circle (ft.)           41.442 (3.968)      35.409 (1.501) <0.001
      Displacement (cu. in.)    233.712 (85.263)    111.227 (24.881) <0.001
      ---------------------------------------------------------------------
      You could change the layout of the table either using available options or using collect.

      Code:
      help dtable
      help collect

      Comment

      Working...
      X