Hypothesis Testing | Dataplexa

Hypothesis Testing in R

Hypothesis testing is a statistical method used to make decisions using data.

It helps us determine whether an assumption about a population is likely to be true based on sample data.


What Is a Hypothesis?

A hypothesis is an assumption or claim that we want to test using data.

In statistics, we usually test two opposite hypotheses to make a decision.


Types of Hypotheses

There are two main types of hypotheses used in testing:

  • Null Hypothesis (H₀) – Assumes no effect or no difference
  • Alternative Hypothesis (H₁) – Assumes there is an effect or difference

The goal is to decide whether we have enough evidence to reject the null hypothesis.


Understanding the p-value

The p-value measures how likely the observed data is under the null hypothesis.

A small p-value indicates strong evidence against the null hypothesis.

  • If p-value ≤ 0.05 → Reject H₀
  • If p-value > 0.05 → Fail to reject H₀

One-Sample t-Test

A one-sample t-test checks whether the mean of a sample is significantly different from a known or assumed value.

This test is commonly used when comparing sample data to a standard or benchmark.

scores <- c(72, 75, 78, 80, 74)
t.test(scores, mu = 75)

Two-Sample t-Test

A two-sample t-test compares the means of two independent groups.

It helps determine whether the difference between group averages is statistically significant.

group1 <- c(65, 70, 75, 80)
group2 <- c(68, 72, 77, 82)

t.test(group1, group2)

Paired t-Test

A paired t-test is used when the same subjects are measured twice.

It compares values before and after a change or intervention.

before <- c(60, 65, 70)
after <- c(68, 72, 75)

t.test(before, after, paired = TRUE)

Confidence Level

The confidence level represents how certain we are about our conclusion.

A 95% confidence level means we are 95% confident that the result is not due to random chance.


Interpreting Test Results

When interpreting hypothesis test results, focus on:

  • p-value
  • Confidence interval
  • Direction of difference

These together help make a correct statistical decision.


Why Hypothesis Testing Matters

  • Validates assumptions with data
  • Supports decision-making
  • Reduces guesswork
  • Foundation for data science and research

📝 Practice Exercises


Exercise 1

Create a numeric vector and perform a one-sample t-test.

Exercise 2

Compare two groups using a two-sample t-test.

Exercise 3

Run a paired t-test using before and after data.

Exercise 4

Interpret the p-value from a t-test result.


✅ Practice Answers


Answer 1

data <- c(50, 55, 60, 65)
t.test(data, mu = 58)

Answer 2

a <- c(10, 20, 30)
b <- c(15, 25, 35)

t.test(a, b)

Answer 3

pre <- c(100, 105, 110)
post <- c(108, 112, 115)

t.test(pre, post, paired = TRUE)

Answer 4

If the p-value is less than 0.05, we reject the null hypothesis. If it is greater than 0.05, we do not reject the null hypothesis.


What’s Next?

In the next lesson, you will learn about Correlation and Regression, which helps measure relationships between variables.