Stata For Beginners : Running An Independent T-test

✦ ✦ ✦
Descriptive alt text here

Once you’ve got the hang of loading in your data set into Stata, you’ll want to actually start running tests. T-tests are one of the most common tests used in psychology statistics.

What even is a T-test?

It’s essentially a statistical test used to determine whether the average (mean) scores of two groups are significantly different from each other.

It helps researchers decide whether an observed difference is likely due to a real effect or just random variation in the data.

If you need a complete glossary of commands you can download the Stata Commands Cheat Sheet for Psychology Students.

Now let’s look at how to actually run one in Stata…

Step 1: Check your variables

To run an independent T-test, you need:

  • DV = numeric continuous variable
  • IV = categorical group variable (2 groups only)

Check variable list

describe

This shows:

  • variable names
  • types
  • labels

You want:

  • DV = numeric
  • group = numeric (coded like 0/1 or 1/2)
Worked Example

You can see in the image below I’ve used the describe command to check my variables. Let’s say I wanted to run an independent T-test on infant intellect scores in males compared to females.

My 2 IV groups would be male and female.

And my DV would be their intellect rating scores.

I can see in the Data Editor that my DV is continuous numerical and my IV groups have been coded into numbers (1= female, 2= male). We’re good to go.

Step 2: Check group coding

Now you want to double check how your group variable is coded:

tabulate groupvar

Example output:

Group | Freq
1 | 30
2 | 32

If you see more than 2 categories → not a t-test anymore.

Worked Example

See? Only 2 categories in my Gender variable for this data set. We can run a T-test.

Step 3: Check descriptive statistics

Always inspect means first:

by groupvar, sort: summarize dv

This gives:

  • mean
  • SD
  • N
  • min/max

You want to confirm:

  • no weird values
  • no obvious data entry errors
Worked Example

Looking gooooddd

Step 4: Check normality (assumption)

t-tests assume roughly normal distribution. Normal distribution just means most of the data falls symmetrically around the mean in the middle. It should look like a nice bell shaped curve.

Visual check:

histogram dv, normal

Optional per group:

by groupvar: histogram dv, normal

Or:

swilk dv

(Shapiro–Wilk test)

Important Note:
With medium/large samples, t-tests might not have perfect normality. With large samples, Shapiro–Wilk often flags minor deviations that don’t invalidate a t-test.

You’re checking for extreme skew or outliers, not perfection.

Worked Example

I used histogram Intellect_Rating, normal. As you can see, this histogram shows that the data looks approximately normal!

Important note: t-tests don’t require perfect bell-shaped data. They still work well when the data is slightly skewed, which is common in real psychological measures.

Step 5: Check equal variances (assumption)

Stata tests this automatically, but you can preview:

robvar dv, by(groupvar)
Worked Example

It looks scary, but it’s not.

Basically if p > 0.05 = equal variances

Proceed with test.

Idk if anyone will ever see this, but I’m hungry.

Step 6: Run the independent samples t-test

Finally, we have reached the statistical promised land. We may now run the actual test.

Core command:

ttest dv, by(groupvar)

Stata output includes:

  • group means
  • difference in means
  • t value
  • degrees of freedom
  • p value
  • confidence interval
Worked Example

The first thing you might be asking is: which p value do I use?

The middle one (the two-tailed p value).

Now we go onto reading the results. I can see from my T-test that the p value is pretty big (p = .97) and the test score is tinyyy (t= .038). What does this mean?

The test score shows us how big the difference is between the two groups. A small score = barely any difference between the groups.

The p value tells us how statistically significant any difference is. It tells us whether we can reject the null hypothesis. Remember the null hypothesis is always: no difference.

If you can’t reject it?

No statistically significant difference was found

Using the common alpha level of .05, to reject the null hypothesis, your p score needs to be <0.05.

My p score is wayyy bigger than 0.05. Therefore, I can’t reject the null hypothesis.

Basically, there was no statistically significant difference in intelligence ratings between males and females.

Step 7: Interpret output

Key parts to report:

  • Mean group 1
  • Mean group 2
  • t
  • df
  • p
  • CI

Example APA style:

t(60) = 2.45, p = .017

Worked Example

For the example test I ran, I’d write it like:

An independent-samples t test was conducted to examine whether intellect ratings differed between the two gender groups. The mean rating for Group 1 (M = 4.74, SD = 1.73) did not significantly differ from the mean rating for Group 2 (M = 4.71, SD = 2.10), t(37) = 0.04, p = .97, 95% CI [−1.53, 1.59].

If you want an easy way to generate the formatting you can use the APA 7th Edition Results Generator. It won’t interpret the results for you, but it does help format your raw data into an APA7th style results paragraph.

Paired samples t-test (within subjects)

If same participants measured twice:

Example variables:

  • pre
  • post

Command:

ttest pre == post

Output gives:

  • mean difference
  • t
  • df
  • p

Finally, Save a clean output

To avoid scrolling chaos:

log using ttest.log, replace

Run test → Stata records output

Then close:

log close

And that’s how you run a T-test in Stata!

Hi, I’m Daisy!

I created The Psych Diaries to make studying psychology a little less overwhelming. Here you’ll find study guides, Stata tutorials, psychology resources, and everything I’m learning along the way!

About Me →

Popular Guides


🤍 Thanks for stopping by. I hope you find something here that helps!

Discover more from The Psych Diaries

Subscribe now to keep reading and get access to the full archive.

Continue reading