EZ Statistics

One-Sample T-Test

Calculator

2. Select Columns & Options

Learn More

One-Sample T-Test

Definition

One-Sample T-Test is a statistical test used to determine whether a sample mean significantly differs from a hypothesized population mean. It's particularly useful when working with small sample sizes and unknown population standard deviation.

Formula

Test Statistic:

t=xˉμ0s/nt = \frac{\bar{x} - \mu_0}{s / \sqrt{n}}

Where:

  • xˉ\bar x = sample mean
  • μ0\mu_0 = hypothesized population mean
  • ss = sample standard deviation
  • nn = sample size

Confidence Interval:

Two-sided: xˉ±tα/2,n1sn\text{Two-sided: }\bar{x} \pm t_{\alpha/2,n-1} \cdot \frac{s}{\sqrt{n}}One-sided: xˉ±tα,n1sn\text{One-sided: }\bar{x} \pm t_{\alpha,n-1} \cdot \frac{s}{\sqrt{n}}

Where:

  • tα/2,n1t_{\alpha/2,n-1} or tα,n1t_{\alpha,n-1} = critical t-value with n1n-1 degrees of freedom
  • α\alpha = significance level (e.g., 0.05 for 95% confidence)
  • xˉ\bar x = sample mean
  • ss = sample standard deviation
  • nn = sample size

Key Assumptions

Random Sampling: Data should be randomly selected from the population
Independence: Observations should be independent of each other
Normality: Data should be approximately normally distributed (can be relaxed for n > 30)
No Extreme Outliers: Data should be free of extreme outliers

Common Pitfalls

  • Not checking assumptions before conducting the test
  • Using the test with very small samples (n < 5) without checking normality
  • Interpreting statistical significance as practical significance

Practical Example

A manufacturer claims their light bulbs last 1000 hours on average. To test this claim:

  • Sample of 36 bulbs tested
  • Sample mean xˉ\bar x = 985 hours
  • Sample standard deviation ss = 45 hours
  • H0:μ=1000H_0: \mu = 1000 hours
  • Ha:μ1000H_a: \mu \neq 1000 hours

Calculating t-statistic:

tα/2,n1=985100045/36=2.00t_{\alpha/2,n-1} = \frac{985 - 1000}{45/\sqrt{36}} = -2.00

Constructing confidence

Constructing 95% confidence interval:

CI=xˉ±tα/2,n1sn=985±2.04536=[970,1000]CI = \bar x \pm t_{\alpha/2,n-1}\frac{s}{\sqrt{n}} = 985 \pm 2.0\frac{45}{\sqrt{36}} = [970, 1000]

With degrees of freedom df=35df = 35 and significance level α=0.05\alpha = 0.05, we can analyze this result in two ways. First, using the Student's t Distribution Table, we find the critical value for a two-tailed test is ±2.3. Since our calculated t-statistic (-2.00) falls within these critical values, we fail to reject the null hypothesis. Alternatively, using our Student's t Distribution Calculator, we can calculate the two-tailed p-value: 2×0.0267=0.05342 \times 0.0267 = 0.0534. Since this p-value exceeds our significance level of 0.05, we again fail to reject the null hypothesis. Based on this analysis, we conclude there is insufficient evidence to suggest that the true average lifespan of the light bulbs differs from the claimed 1000 hours. Furthermore, we can state with 95% confidence that the true population mean lifespan falls between 970 and 1000 hours.

Effect Size

Cohen's d measures the standardized difference between the sample mean and hypothesized value:

d=xˉμ0sd = \frac{|\bar{x} - \mu_0|}{s}

Interpretation guidelines:

  • Small effect: |d| ≈ 0.2
  • Medium effect: |d| ≈ 0.5
  • Large effect: |d| ≈ 0.8

Power Analysis

To determine required sample size (n) for desired power (1-β):

n=2(z1α/2+z1β)2(μaμ0σ)2n = \frac{2(z_{1-\alpha/2} + z_{1-\beta})^2}{(\frac{\mu_a-\mu_0}{\sigma})^2}

Where:

  • α\alpha = significance level
  • β\beta = probability of Type II error
  • μa\mu_a = anticipated population mean
  • σ\sigma = population standard deviation

Decision Rules

Reject H₀ if:

  • Two-sided test: t>tα/2,n1|t| > t_{\alpha/2,n-1}
  • Left-tailed test: t<tα,n1t < -t_{\alpha,n-1}
  • Right-tailed test: t>tα,n1t > t_{\alpha,n-1}
  • Or if p-value<αp\text{-value} < \alpha

Reporting Results

Standard format for scientific reporting:

"A one-sample t-test was conducted to compare [variable] to [hypothesized value]. Results indicated that [variable] (M = [mean], SD = [std dev]) was [not] significantly different from [hypothesized value], t([df]) = [t-value], p = [p-value], d = [Cohen's d]. The 95% CI ranged from [lower] to [upper]."

Code Examples

R
1library(tidyverse)
2set.seed(42) 
3sample_data <- tibble(
4  value = rnorm(30, mean = 98, sd = 5) # 30 observations, mean=98, sd=5
5)
6
7# Hypothesized mean
8mu0 <- 100
9
10# Basic summary statistics
11summary_stats <- sample_data %>%
12  summarise(
13    n = n(),
14    mean = mean(value),
15    sd = sd(value),
16    se = sd/sqrt(n)
17  )
18
19# One-sample t-test
20t_test_result <- t.test(sample_data$value, mu = mu0)
21
22# Effect size (Cohen's d)
23cohens_d <- (mean(sample_data$value) - mu0) / sd(sample_data$value)
24
25# Calculate confidence interval
26ci <- t_test_result$conf.int
27
28# Print results
29print(t_test_result)
30print(str_glue("Effect size (Cohen's d):", cohens_d))
31
32# Visualize the data
33library(ggplot2)
34ggplot(sample_data, aes(x = value)) +
35  geom_histogram(aes(y = ..density..), bins = 10) +
36  geom_density() +
37  geom_vline(xintercept = mu0, color = "red", linetype = "dashed") +
38  geom_vline(xintercept = mean(sample_data$value), color = "blue") +
39  theme_minimal() +
40  labs(title = "Sample Distribution with Hypothesized Mean",
41       subtitle = "Blue: Sample Mean, Red dashed: Hypothesized Mean")
Python
1import numpy as np
2import scipy.stats as stats
3import matplotlib.pyplot as plt
4import seaborn as sns
5from statsmodels.stats.power import TTestPower
6
7# Generate sample data
8np.random.seed(42) 
9sample_data = np.random.normal(loc=98, scale=5, size=30)  # 30 observations, mean=98, sd=5
10
11# Hypothesized mean
12mu0 = 100
13
14# Basic summary statistics
15n = len(sample_data)
16sample_mean = np.mean(sample_data)
17sample_sd = np.std(sample_data, ddof=1)
18se = sample_sd / np.sqrt(n)
19
20# Perform one-sample t-test
21t_stat, p_value = stats.ttest_1samp(sample_data, mu0)
22
23# Calculate Cohen's d effect size
24cohens_d = (sample_mean - mu0) / sample_sd
25
26# Calculate confidence interval (95%)
27ci = stats.t.interval(confidence=0.95, 
28                     df=n-1, 
29                     loc=sample_mean, 
30                     scale=se)
31
32# Print results
33print(f"Sample Statistics:")
34print(f"Mean: {sample_mean:.2f}")
35print(f"Standard Deviation: {sample_sd:.2f}")
36print(f"Standard Error: {se:.2f}")
37print(f"nT-Test Results:")
38print(f"t-statistic: {t_stat:.2f}")
39print(f"p-value: {p_value:.4f}")
40print(f"95% CI: [{ci[0]:.2f}, {ci[1]:.2f}]")
41print(f"Effect size (Cohen's d): {cohens_d:.2f}")
42
43# Visualize the data
44plt.figure(figsize=(10, 6))
45sns.histplot(sample_data, stat='density', alpha=0.5)
46sns.kdeplot(sample_data)
47plt.axvline(mu0, color='red', linestyle='--', label='Hypothesized Mean')
48plt.axvline(sample_mean, color='blue', label='Sample Mean')
49plt.title('Sample Distribution with Hypothesized Mean')
50plt.legend()
51plt.show()
52
53# Power analysis
54analysis = TTestPower()
55power = analysis.power(effect_size=cohens_d, 
56                      nobs=n, 
57                      alpha=0.05)
58print(f"Power Analysis:")
59print(f"Statistical Power: {power:.3f}")

Alternative Tests

Consider these alternatives when assumptions are violated:

  • Wilcoxon Signed-Rank Test: When normality is violated
  • Z-Test: When population standard deviation is known
  • Bootstrap Methods: When sample size is small and normality is questionable

Related Calculators

Z-Score Calculator

Two-Sample T-Test

One-Way ANOVA

Wilcoxon Signed-Rank Test

Help us improve

Found an error or have a suggestion? Let us know!