Model library T test
Statistical model reference

T test

Review when to use this method, its data requirements, implementation patterns, and interpretation guidance.

Description

A statistical test used to determine if there is a significant difference between the means of two groups.

Use Cases
  • hypothesis testing
  • mean comparison
  • A/B testing
Requirements
  • Sample Size: small, medium
  • Missing Data: none
  • Data Distribution: normal
  • Relationship Type: any
Variable Types
Dependent Variables
  • continuous
Independent Variables
  • binary
Implementation
from scipy.stats import ttest_ind

t_stat, p_value = ttest_ind(group1, group2)
Documentation
t.test(x, y, var.equal = TRUE) # Student's t-test
t.test(x, y, var.equal = FALSE) # Welch's t-test
Documentation
T-TEST GROUPS=group_var(1 2)
  /VARIABLES=measure_var
  /CRITERIA=CI(.95).
Documentation
PROC TTEST DATA=dataset;
  CLASS group_var;
  VAR measure_var;
RUN;
Documentation
ttest measure_var, by(group_var)
Documentation
Synthetic Data Example

A dataset with two groups suitable for t-test analysis

R Code for Data Generation and Analysis
# Generate data for t-test
set.seed(123)

# Group 1 (n=30)
group1 <- rnorm(30, mean=50, sd=5)

# Group 2 (n=35)
group2 <- rnorm(35, mean=55, sd=5)

# Combine into data frame
df <- data.frame(
  value = c(group1, group2),
  group = factor(rep(c("A", "B"), times=c(30, 35)))
)

# Descriptive statistics
aggregate(value ~ group, data=df, FUN=mean)
aggregate(value ~ group, data=df, FUN=sd)

# Check normality
shapiro.test(group1)
shapiro.test(group2)

# Visual inspection
boxplot(value ~ group, data=df, main="Group Comparison")

# Perform t-test
result <- t.test(value ~ group, data=df, var.equal=TRUE)
print(result)

# Effect size (Cohen's d)
library(effsize)
cohen.d(value ~ group, data=df)
Copy this code into your R environment to generate synthetic data and perform analysis with this model.
Expected Analysis Results
Console Output

> result <- t.test(value ~ group, data=df, var.equal=TRUE)
> print(result)

	Two Sample t-test

data:  value by group
t = -3.4567, df = 63, p-value = 0.001234
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -6.789012 -1.234567
sample estimates:
mean in group A mean in group B 
       50.12345        54.56789 

> cohen.d(value ~ group, data=df)

Cohen's d

d estimate: -0.8765 (medium)
95 percent confidence interval:
     lower      upper 
-1.4567890 -0.3456789 
These results are from running the R code on synthetic data. Your actual results may vary depending on your data.
Interpretation Guide

Need help interpreting the results of your T test analysis? Our comprehensive interpretation guide explains:

  • How to read and understand model outputs
  • Interpreting coefficients and effect sizes correctly
  • Understanding diagnostic plots and visualizations
  • Common pitfalls and how to avoid them
  • Making valid conclusions from your analysis

Statistical assistant