Independent Samples t-Test in Stata
Defaults, Output Labels and Effect Size
An independent samples t-test in Stata assumes equal variances unless told otherwise, unlike some other programs. Stata prints three p-values at once and leaves the standardized effect to a second command, so each step settles one value the report will quote.
IN SHORT
What ttest Estimates and What Stata Assumes by Default
An independent samples t-test estimates the difference between the means of a continuous outcome in two unrelated groups and how precisely the data fix it. To run an independent samples t-test in Stata, type ttest followed by the outcome variable and by() naming the grouping variable. The pooled-variance version runs unless unequal is added, which switches to Satterthwaite’s approximation for the degrees of freedom. Stata prints the group means, the difference with its confidence interval and three p-values, and the standardized effect comes from esize. OnlineSPSS, a specialist statistical consultancy for academic, clinical and professional research, carries out and reviews this analysis through an in-house PhD-level statistician.
In This Guide
ANALYSIS SUPPORT
Analysis Support for This Method
A difference in means is not the only defensible summary of two groups. With an ordinal or strongly skewed outcome, a rank-based comparison may answer the question more directly.
Independent Samples t-Test
Mean differences between two groups estimated with intervals, from the choice of variance assumption to the effect size and the sentence that reports them.
Mann-Whitney U Test
Two independent groups compared through their ranks, for ordered categories or for a skewed outcome where a difference in means says little about the typical participant.
BEFORE YOU RUN IT
What the Data Must Look Like for ttest
The command reads one outcome column and one grouping column, so the file layout and the variance choice are settled before anything is typed.
One Row per Participant Each participant appears once, with the outcome in one variable and the group in another. Scores held in two columns, one per group, need the unpaired syntax or a reshape.
Exactly Two Groups The ttest by() option in Stata needs a variable with exactly two values among the rows used. The groups are ordered by their values, and the difference is the first mean minus the second.
A Variance Decision Up Front Pooled variances need a reason fixed in advance, such as earlier evidence of similar spread. Equal group sizes make the test robust without justifying the assumption, so otherwise unequal is safer.
Outcome Within Each Group Look at the outcome by group with a box plot or histogram before testing. Extreme values and strong skew need a stated plan, since they move a mean most in small groups.
IN STATA
How to Run an Independent Samples t-Test in Stata
Both commands are part of official Stata, with esize included since Stata 13, so nothing needs installing, and a dialog opened from the menus prints the same command in the Results window.
Check the File Layout
One row per participant, with the outcome in one variable and a two-valued group code in another.
Check: Tabulate the grouping variable with the missing option. A code such as 9 for missing counts as a third group unless it is declared missing, and ttest then stops with an error.
Compare Spread Across Groups
Plot the outcome by group, for example with a box plot, and compare spread, skew and outliers.
Watch: Strong skew changes what a mean difference says. If a group is clearly skewed, state it in the text and ask whether the difference in means is the quantity your question needs.
Run ttest With the by() Option
Run ttest with by() for the pooled-variance version, or add unequal for Satterthwaite’s approximation.
Tip: If the variance choice is not settled, run both versions side by side as a sensitivity check, and report the one named in your methods, not the one with the smaller p-value.
Read the Output Together
Read the group means, the difference with its confidence interval, the degrees of freedom and the middle p-value together.
Tip: Read the confidence interval of the difference before the p-value. It shows, in the units of the outcome, which range of differences the data are compatible with.
Add the Standardized Difference
Request the standardized difference with esize twosample and name the measure used.
Tip: Name the measure in the text, because Cohen’s d and Hedges’s g are not identical. Hedges’s g includes a small-sample correction, so the two differ most in small groups.
Keep the Commands in a Do-File
Save the commands in a do-file so the output can be regenerated, and keep them in the order in which they were run.
Tip: Add a short comment above each command saying which question its output answers, so a collaborator can find the block behind each number in the report.
graph box outcome, over(group)
ttest outcome, by(group)
ttest outcome, by(group) unequal
esize twosample outcome, by(group) hedgesg
READING THE OUTPUT
Reading the Two Blocks of ttest Output
The table of group statistics comes first and the hypothesis block second, and the sign and the middle p-value are where a copied value most often goes astray.
| Output element | What it tells you | What to check |
|---|---|---|
| Header: Two-sample t test with equal variances | Which version ran; adding unequal changes the header | That the version named in the methods is the version that ran |
| Group rows: Obs, Mean, Std. err., Std. dev. | Group sizes, means and spread, with an interval for each mean | That Obs matches the intended n per group after missing values |
| diff row | The difference between the two means with its confidence interval | That the interval is read with the order of subtraction in mind |
| diff = mean(first) – mean(second) | The order of subtraction, set by the sorted values of the group variable | That the sentence states the direction in words |
| t and Degrees of freedom | The statistic and its degrees of freedom, labeled as Satterthwaite’s when unequal is used | That df is quoted as printed; a fractional value is expected with unequal |
| Ha: diff != 0 (middle Pr line) | The two-sided alternative and its p-value | That the middle value is reported unless a direction was stated in advance |
| Ha: diff < 0 and Ha: diff > 0 | One-sided alternatives with their own p-values | That a one-sided value is not picked after the sign of the difference is seen |
| esize output: Cohen’s d, Hedges’s g | Standardized differences with confidence intervals | That the measure and its standardizer are named in the text |
REPORTING
Reporting the Stata Result With an Effect Size
Two details leave the console wrongly more often than the rest: the variance version that ran and the order of subtraction.
Give the statistic with its degrees of freedom as printed, t(df) = ___, and p = ___, followed by the difference between group means in the units of the outcome, with its 95% CI [___, ___], and each group’s mean and standard deviation. Name in words the group with the higher mean, since the sign depends on the subtraction order Stata used. Add Hedges’s g = ___ with its 95% CI [___, ___] from esize and name the standardizer. Give the n per group and the handling of missing data: ttest leaves out rows with a missing outcome or group. Name the version that ran: Satterthwaite’s test when unequal was added, the pooled-variance test otherwise.
COMMON MISTAKES
Slips Between the ttest Output and the Report
The printed block holds everything needed to catch these six, provided the sentence in the manuscript is read against it line by line.
Default Taken for Granted ttest pools the variances unless told otherwise. A methods section that describes an unequal variances t-test in Stata, when the command carried no unequal option, misdescribes the analysis.
Wrong Tail Quoted The output prints three p-values side by side. A left or right tail quoted for an undirected hypothesis gives half the two-sided value when the tail matches the data, and the middle line is the one for a two-sided question.
Subtraction Order Misread The difference is the first group's mean minus the second's. A sentence saying the second group scored higher beside a positive printed difference reverses the finding.
Pooled df With an Adjusted t Quoting the total sample minus 2 beside a statistic from the unequal version mixes two tests. The degrees of freedom printed with that statistic belong in the sentence.
Effect Size Without Its Standardizer Cohen's d and Hedges's g use a pooled standard deviation, and esize can also give Glass's delta. A value without its named measure cannot be compared with other studies.
Missing Rows Unmentioned ttest drops rows with a missing outcome or group without comment, so the n per group in the sentence comes from the Obs column and the exclusions are stated.
Version, sign and effect size not yet verified in your draft? Independent Statistical Review examines an analysis run in Stata from the data step through to the reported result.
FREQUENTLY ASKED QUESTIONS
Questions About ttest Output in Stata
The output puts three p-values on one screen and leaves the direction of the subtraction to the reader, while the effect size comes from a second command.
Why does ttest print three p-values for one test?
Each belongs to a different alternative hypothesis: the left tail, the two-sided test and the right tail. The middle line, Pr(|T| > |t|), is the p-value for an undirected hypothesis and the one reported unless a direction was stated in advance. The outer lines are one-sided and are not chosen after the sign of the difference has been seen.
Which group does Stata subtract from which?
The difference is the mean of the first group minus the mean of the second, with groups ordered by the values of the grouping variable. With codes 0 and 1 the display reads diff = mean(0) – mean(1), so a positive difference means the group coded 0 scored higher. State the order in words and check the sign against the two means.
Can Stata give Hedges's g with an interval without extra packages?
Yes: esize twosample, part of official Stata since version 13, reports Cohen’s d and Hedges’s g with confidence intervals. Add the hedgesg option to show that measure, or all to see every estimate it offers, and name the one reported. When only summary statistics are available, the immediate command esizei accepts them directly.
What does a check of my do-file against the reported sentence involve?
An in-house PhD-level statistician at OnlineSPSS lays the commands, the printed ttest and esize blocks and your manuscript sentence side by side, looking at the variance version, the sign of the difference and the effect size named. The comparison can also be run on your data. It belongs to Results Reporting & Interpretation, and the conclusions about your field are yours.
How do you protect my research data and confidentiality?
Your data and project details are handled confidentially, and project information is not published. Researcher names and institutions are not used publicly without permission. An NDA is available where required and can be arranged before sensitive materials are shared. Read more about confidentiality and data security.
What happens when I request an assessment, and how long does it take?
You share your research question, design and relevant materials, such as the dataset or the output you have, and a statistically trained Research Coordinator reviews the request and responds the same day. The analysis, deliverables, timeline and one fixed project fee are then confirmed before any work begins, so the timeline follows from the agreed scope.
Still have a question? Find more answers in our full FAQ.
NEXT STEP
Is the Variance Choice
Stated in Your Methods?
The header line, the sign of diff and the esize table each settle one detail of the report. A draft can be read against them in that order, or a new one prepared from the saved output.
No Commitment Required
Strictly Confidential
Fixed Project Fee
In-House PhD-Level Statisticians
Same-Day Response
Serving Researchers Since 2014