2026-03-08

Introduction

In statistics, the p-value helps us determine whether our data provides strong evidence against a null hypothesis.

It is widely used in:

  • scientific research
  • medicine
  • machine learning
  • economics

The p-value helps answer the question:

Is the observed result statistically significant?

Hypothesis Testing Framework

In hypothesis testing we define two hypotheses:

Null hypothesis:

\[ H_0 \]

Alternative hypothesis:

\[ H_a \]

Example: Testing if the mean exam score is 70.

\[ H_0: \mu = 70 \]

\[ H_a: \mu \ne 70 \]

Definition of the P-value

The p-value is the probability of observing data at least as extreme as the sample result assuming the null hypothesis is true.

Mathematically:

\[ p = P(\text{observed data or more extreme} \mid H_0) \]

Decision rule:

  • If p ≤ 0.05 → Reject \(H_0\)
  • If p > 0.05 → Fail to reject \(H_0\)

Example Dataset

Suppose we collected exam scores from 30 students.

set.seed(123)
scores <- rnorm(30, mean = 74, sd = 8)
data <- data.frame(scores)
head(data)
##     scores
## 1 69.51619
## 2 72.15858
## 3 86.46967
## 4 74.56407
## 5 75.03430
## 6 87.72052

We will test whether the true mean score equals 70.

Distribution of the Data (ggplot)

ggplot(data, aes(x=scores)) +
  geom_histogram(fill="skyblue", bins=10) +
  labs(title="Distribution of Exam Scores", x="Score", y="Frequency")+
  theme_minimal()

This histogram shows the spread of the exam scores.

Visualization of Hypothesis Testing (ggplot)

This curve represents the sampling distribution assuming the null hypothesis is true.

Interactive Plot (Plotly)

This interactive graph allows zooming and exploring the distribution.

Calculating P-value in R

Example using a one-sample t-test:

t.test(scores, mu = 70)
## 
##  One Sample t-test
## 
## data:  scores
## t = 2.5286, df = 29, p-value = 0.01715
## alternative hypothesis: true mean is not equal to 70
## 95 percent confidence interval:
##  70.69259 76.55375
## sample estimates:
## mean of x 
##  73.62317

R automatically computes:

  • test statistic
  • p-value
  • confidence interval

Interpretation

Suppose R outputs:

p-value = 0.02

Since:

\[ p = 0.02 < 0.05 \]

We reject the null hypothesis.

Conclusion: There is statistical evidence that the mean score is different from 70.

Key Takeaways

Important points about p-values:

  • Measure evidence against the null hypothesis
  • Small p-value → strong evidence
  • Large p-value → weak evidence
  • Common significance level: 0.05

P-values are fundamental in modern data analysis and scientific research.