One-Sample T-Test
Calculator
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:
Where:
- = sample mean
- = hypothesized population mean
- = sample standard deviation
- = sample size
Confidence Interval:
Where:
- or = critical t-value with degrees of freedom
- = significance level (e.g., 0.05 for 95% confidence)
- = sample mean
- = sample standard deviation
- = sample size
Key Assumptions
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 = 985 hours
- Sample standard deviation = 45 hours
- hours
- hours
Calculating t-statistic:
Constructing confidence
Constructing 95% confidence interval:
With degrees of freedom and significance level , 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: . 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:
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-β):
Where:
- = significance level
- = probability of Type II error
- = anticipated population mean
- = population standard deviation
Decision Rules
Reject H₀ if:
- Two-sided test:
- Left-tailed test:
- Right-tailed test:
- Or if
Reporting Results
Standard format for scientific reporting:
Code Examples
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")
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!