R Tutorial
Fundamentals of R
Variables
Input and Output
Decision Making
Control Flow
Functions
Strings
Vectors
Lists
Arrays
Matrices
Factors
DataFrames
Object Oriented Programming
Error Handling
File Handling
Packages in R
Data Interfaces
Data Visualization
Statistics
Machine Learning with R
Hypothesis testing is a critical tool in inferential statistics, allowing one to test an assumption or hypothesis about a population parameter. In R, a variety of functions exist for conducting different types of hypothesis tests.
In this tutorial, we'll cover some of the most commonly used hypothesis tests:
This test checks if the mean of a single sample of data is significantly different from a known or hypothesized population mean.
# Data data <- c(25, 30, 35, 40, 45) # Test if mean of data is different from 30 t.test(data, mu = 30)
This test determines if two independent samples have significantly different means.
# Data group1 <- c(25, 30, 35, 40, 45) group2 <- c(30, 35, 40, 45, 50) # Test t.test(group1, group2)
Used when the samples are dependent; that is, there's a one-to-one relationship between values in the two samples.
# Before and after treatment values for five patients before <- c(120, 128, 130, 123, 115) after <- c(115, 125, 126, 120, 112) # Test t.test(before, after, paired = TRUE)
This test checks the relationship between two categorical variables in a contingency table.
# Sample data in a matrix format data <- matrix(c(30, 10, 10, 50), nrow = 2) # Test chisq.test(data)
Tests if there are significant differences between the means of three or more independent groups.
group1 <- c(25, 30, 35, 40, 45) group2 <- c(35, 40, 45, 50, 55) group3 <- c(45, 50, 55, 60, 65) # Test anova_result <- aov(c(group1, group2, group3) ~ c(rep(1, 5), rep(2, 5), rep(3, 5))) summary(anova_result)
To determine if there's a significant linear relationship between two continuous variables.
x <- c(1, 2, 3, 4, 5) y <- c(5, 4, 3, 2, 1) # Pearson correlation test cor.test(x, y, method = "pearson")
This is a brief overview, and there's much more depth to hypothesis testing in R. For a deeper dive, consider exploring resources like R's documentation, online courses, or statistical textbooks.
Hypothesis testing in R:
# Hypothesis testing in R # Example: One-sample t-test sample_data <- c(23, 25, 28, 22, 27, 30, 21, 26, 29, 24) t_test_result <- t.test(sample_data, mu = 25)
Statistical tests in R:
# Statistical tests in R # Example: Chi-square test observed <- c(20, 30, 25) expected <- c(15, 35, 25) chi_square_result <- chisq.test(observed, p = expected / sum(expected))
Two-sample t-test in R:
# Two-sample t-test in R group1 <- c(25, 28, 30, 22, 27) group2 <- c(20, 24, 26, 21, 23) t_test_result <- t.test(group1, group2)
ANOVA in R for hypothesis testing:
# ANOVA in R for hypothesis testing group1 <- c(25, 28, 30, 22, 27) group2 <- c(20, 24, 26, 21, 23) group3 <- c(18, 21, 23, 19, 20) anova_result <- aov(c(group1, group2, group3) ~ rep(c("Group1", "Group2", "Group3"), each = 5))
Chi-square test in R:
# Chi-square test in R observed <- matrix(c(20, 30, 25, 15, 10, 25), nrow = 2) chi_square_result <- chisq.test(observed)
Wilcoxon test in R:
# Wilcoxon test in R before <- c(25, 28, 30, 22, 27) after <- c(20, 24, 26, 21, 23) wilcox_result <- wilcox.test(before, after, paired = TRUE)
Paired t-test in R:
# Paired t-test in R before <- c(25, 28, 30, 22, 27) after <- c(20, 24, 26, 21, 23) t_test_result <- t.test(before, after, paired = TRUE)
R hypothesis testing examples:
# R hypothesis testing examples # Example 1: One-sample t-test sample_data <- c(23, 25, 28, 22, 27, 30, 21, 26, 29, 24) t_test_result <- t.test(sample_data, mu = 25) # Example 2: Chi-square test observed <- c(20, 30, 25) expected <- c(15, 35, 25) chi_square_result <- chisq.test(observed, p = expected / sum(expected))
Comparing means in R:
# Comparing means in R # Example: Two-sample t-test group1 <- c(25, 28, 30, 22, 27) group2 <- c(20, 24, 26, 21, 23) t_test_result <- t.test(group1, group2)