Code for Two-Sample t-Test: Comparing School Scores
R
# Generate example data set.seed (123) school1_scores <- rnorm (30, mean = 75, sd = 10) school2_scores <- rnorm (30, mean = 80, sd = 12) # Perform a two-sample t-test t_test_result <- t.test (school1_scores, school2_scores) # Print the result print (t_test_result) |
Output:
Welch Two Sample t-test
data: school1_scores and school2_scores
t = -2.9726, df = 57.974, p-value = 0.004295
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-12.736395 -2.485801
sample estimates:
mean of x mean of y
74.52896 82.14006
- We generate two sets of example data, school1_scores and school2_scores, each representing the exam scores of students from two different schools.
- We perform a two-sample t-test using the t.test function, comparing the means of the two groups.
- The result includes the t-statistic, degrees of freedom, and p-value, which can be used to assess whether there is a significant difference between the means of the two groups.
- The p-value is exceptionally small, smaller than 0.05. This suggests strong evidence against the null hypothesis, indicating that there is a statistically significant difference in the mean scores between the two schools.
Differences Between two-sample, t-test and paired t-test
Statistical tests are essential tools in data analysis, helping researchers make inferences about populations based on sample data. Two common tests used to compare the means of different groups are the two-sample t-test and the paired t-test. Both tests are based on the t-distribution, but they have distinct use cases and assumptions. In this article, we’ll explore the differences between these two tests in R, when to use each one, and how to conduct them in practice.
Contact Us