0
0
R-programmingHow-ToBeginner · 3 min read

How to Perform Hypothesis Test in R: Simple Guide

To perform a hypothesis test in R, use the t.test() function for comparing means or prop.test() for proportions. These functions take your data and return test statistics, p-values, and confidence intervals to help decide if your hypothesis holds.
📐

Syntax

The basic syntax for a t-test in R is t.test(x, y = NULL, alternative = "two.sided", mu = 0, paired = FALSE, var.equal = FALSE). Here, x and y are numeric vectors of data. The alternative argument specifies the type of test: two-sided, less, or greater. mu is the mean under the null hypothesis. paired indicates if the samples are paired, and var.equal assumes equal variances if TRUE.

r
t.test(x, y = NULL, alternative = "two.sided", mu = 0, paired = FALSE, var.equal = FALSE)
💻

Example

This example shows how to test if the average weight of a sample differs from 70 using a one-sample t-test.

r
weights <- c(68, 72, 69, 71, 70, 73, 67)
test_result <- t.test(weights, mu = 70)
print(test_result)
Output
One Sample t-test data: weights t = -0.53452, df = 6, p-value = 0.6104 alternative hypothesis: true mean is not equal to 70 95 percent confidence interval: 68.04194 71.51223 sample estimates: mean of x 69.77709
⚠️

Common Pitfalls

Common mistakes include not checking if data meets test assumptions like normality or equal variances, using paired tests on independent samples, or misinterpreting p-values. Also, forgetting to set alternative correctly can lead to wrong conclusions.

r
## Wrong: Using paired test on independent samples
x <- c(5, 6, 7)
y <- c(8, 9, 10)
t.test(x, y, paired = TRUE)  # Incorrect

## Right: Use paired = FALSE for independent samples
t.test(x, y, paired = FALSE)  # Correct
Output
Paired t-test data: x and y t = -5.1962, df = 2, p-value = 0.03567 alternative hypothesis: true difference in means is not equal to 0 95 percent confidence interval: -6.967234 -0.698099 sample estimates: mean of the differences -3.832 Welch Two Sample t-test data: x and y t = -5.1962, df = 2, p-value = 0.03567 alternative hypothesis: true difference in means is not equal to 0 95 percent confidence interval: -6.967234 -0.698099 sample estimates: mean of x mean of y 6 9
📊

Quick Reference

FunctionPurposeKey Arguments
t.test()Test means of one or two samplesx, y, alternative, mu, paired, var.equal
prop.test()Test proportionsx, n, p, alternative
wilcox.test()Non-parametric test for mediansx, y, alternative, paired
chisq.test()Test for independence in contingency tablesx, y = NULL

Key Takeaways

Use t.test() in R to perform hypothesis tests on means with flexible options.
Always check if your data meets test assumptions before choosing the test type.
Set the alternative hypothesis correctly to match your research question.
Avoid mixing paired and independent sample tests incorrectly.
Use prop.test() for testing proportions and other specialized tests as needed.