Statistical Tests in R

📘 Introduction

Statistical tests are mathematical procedures used to determine whether observed data supports a particular hypothesis. They help researchers and analysts decide whether differences, relationships, or patterns found in a dataset are statistically significant or simply due to random chance. R provides a wide range of built-in functions for performing statistical tests efficiently.

Information

Statistical tests are widely used in scientific research, business analytics, healthcare, finance, quality control, and machine learning to make evidence-based decisions.

đŸŽ¯ Why Learn Statistical Tests?

  • Test research hypotheses.
  • Compare groups and populations.
  • Identify significant relationships.
  • Support data-driven decision-making.
  • Validate analytical results.

📚 Types of Statistical Tests

Statistical Tests
Parametric Tests
Non-Parametric Tests
Correlation Tests
Goodness-of-Fit Tests

📋 Common Statistical Tests in R

TestPurposeFunction
One-Sample t-TestCompare sample mean with a known value.t.test()
Two-Sample t-TestCompare means of two independent groups.t.test()
Paired t-TestCompare paired observations.t.test()
Chi-Square TestTest association between categorical variables.chisq.test()
ANOVACompare means of multiple groups.aov()
Correlation TestMeasure linear relationship.cor.test()
Wilcoxon TestNon-parametric alternative to the t-test.wilcox.test()
Shapiro-Wilk TestCheck data normality.shapiro.test()

📝 Sample Dataset

Student Marks

marks <- c(
  78, 82, 85, 90,
  88, 92, 79, 84,
  87, 91
)

print(marks)

📊 One-Sample t-Test

A one-sample t-test compares the sample mean against a known or hypothesized population mean.

One-Sample t-Test

t.test(
  marks,
  mu = 80
)

đŸ‘Ĩ Two-Sample t-Test

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

Independent Samples t-Test

groupA <- c(
  82,85,88,90,91
)

groupB <- c(
  75,78,80,81,79
)

t.test(
  groupA,
  groupB
)

🔄 Paired t-Test

A paired t-test compares measurements taken from the same subjects before and after an intervention.

Paired t-Test

before <- c(
  65,70,72,75,80
)

after <- c(
  70,75,76,80,85
)

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

📈 Chi-Square Test

The chi-square test evaluates whether two categorical variables are associated.

Chi-Square Test

survey <- matrix(
  c(
    30,20,
    25,35
  ),
  nrow = 2,
  byrow = TRUE
)

chisq.test(survey)

📊 Analysis of Variance (ANOVA)

ANOVA compares the means of three or more groups.

One-Way ANOVA

scores <- data.frame(
  Marks = c(
    78,82,85,
    88,90,91,
    72,75,77
  ),
  Group = factor(
    c(
      "A","A","A",
      "B","B","B",
      "C","C","C"
    )
  )
)

model <- aov(
  Marks ~ Group,
  data = scores
)

summary(model)

📉 Correlation Test

The correlation test measures the strength and direction of a relationship between two numerical variables.

Pearson Correlation Test

hours <- c(
  2,3,4,5,6
)

marks <- c(
  60,68,75,82,90
)

cor.test(
  hours,
  marks
)

📌 Wilcoxon Rank-Sum Test

The Wilcoxon test is a non-parametric alternative to the t-test when normality assumptions are not met.

Wilcoxon Test

groupA <- c(
  82,85,88,90,91
)

groupB <- c(
  75,78,80,81,79
)

wilcox.test(
  groupA,
  groupB
)

📏 Shapiro-Wilk Normality Test

The Shapiro-Wilk test checks whether data follows a normal distribution.

Normality Test

shapiro.test(
  marks
)

📊 Understanding p-Values

p-ValueInterpretation
p < 0.05Reject the null hypothesis.
p â‰Ĩ 0.05Fail to reject the null hypothesis.

Tip

A statistically significant result indicates evidence against the null hypothesis, but it does not necessarily imply practical or real-world importance.

📈 Confidence Intervals

Most statistical test functions in R also return confidence intervals that estimate the range within which the true population parameter is likely to fall.

Confidence Interval Example

result <- t.test(
  marks,
  mu = 80
)

result$conf.int

📊 Choosing the Right Statistical Test

ScenarioRecommended Test
Compare one sample with a known mean.One-Sample t-Test.
Compare two independent groups.Independent Two-Sample t-Test.
Compare before-and-after measurements.Paired t-Test.
Compare three or more group means.ANOVA.
Analyze categorical data.Chi-Square Test.
Measure relationship between numeric variables.Correlation Test.
Data is not normally distributed.Wilcoxon Test.

🌍 Real-World Example

A teacher wants to determine whether a new teaching method improves student performance by comparing marks before and after the course.

Teaching Method Evaluation

before <- c(
  68,72,75,70,74,
  71,73,69
)

after <- c(
  75,79,81,77,80,
  76,82,74
)

result <- t.test(
  before,
  after,
  paired = TRUE
)

print(result)

🔄 Statistical Testing Workflow

Define Research Question
State Hypotheses
Choose Statistical Test
Perform Test
Interpret p-Value
Draw Conclusion

📋 Common Statistical Test Functions

FunctionDescription
t.test()Performs one-sample, two-sample, and paired t-tests.
chisq.test()Performs the chi-square test.
aov()Performs analysis of variance.
cor.test()Performs a correlation test.
wilcox.test()Performs the Wilcoxon test.
shapiro.test()Tests normality.

âš ī¸ Common Mistakes

MistakeExplanationSolution
Choosing the wrong statistical testMay produce misleading conclusions.Select a test based on the data type, study design, and assumptions.
Ignoring test assumptionsViolations can invalidate results.Check assumptions such as normality and independence before testing.
Misinterpreting the p-valueA small p-value does not measure the size or importance of an effect.Interpret the p-value alongside effect size and confidence intervals.
Using multiple tests without adjustmentIncreases the risk of false-positive results.Apply appropriate corrections when performing multiple comparisons.

💡 Best Practices

  • Clearly define the null and alternative hypotheses before analysis.
  • Verify assumptions before choosing a statistical test.
  • Report the test statistic, p-value, and confidence interval.
  • Interpret statistical significance together with practical significance.
  • Document all analysis steps for reproducibility.

Best Practice

Statistical tests help determine whether observed patterns are likely due to chance or represent meaningful effects. Selecting the appropriate test, checking assumptions, and interpreting results carefully are essential for reliable statistical analysis.

📝 Summary

Statistical tests are essential tools for hypothesis testing and data analysis. In this chapter, you learned how to perform one-sample, two-sample, and paired t-tests, chi-square tests, ANOVA, correlation tests, Wilcoxon tests, and Shapiro-Wilk normality tests using R. You also explored p-values, confidence intervals, selecting appropriate tests, and interpreting results. Mastering statistical tests enables you to make informed, evidence-based decisions from data and forms the foundation for advanced statistical modeling and research.

>>"Statistical tests transform data into evidence, enabling informed conclusions through objective analysis."