Experiment # 9
9.1. Aim: Applying the t-test for independent and dependent samples.
9.2. Description: The t-test is a statistical test used to determine if there is a significant difference
between the means of two groups. It’s commonly used in hypothesis testing and comes in several
forms, each with specific applications.
Types of t-test:
1. Independent Samples t-test (Two-Sample t-test): Compares the means of two unrelated groups
to see if they differ significantly.
2. Paired Samples t-test (Dependent t-test): Compares means from the same group at two
different times or two matched groups to see if there’s a significant change.
Hypotheses in a t-test
For each type, we set up null (H0) and alternative (H1) hypotheses:
• Null Hypothesis (H0): There is no significant difference between the means.
• Alternative Hypothesis (H1): There is a significant difference between the means.
9.3. Formula:
1. Independent Samples t-test (Two-Sample t-test)
The independent samples t-test compares the means of two independent groups. The formula for
the t-statistic in the independent t-test is:
Where: 𝒙𝟏,𝒙𝟐 = Sample means of Group 1 and Group 2
𝒔𝟏𝟐 , 𝒔𝟐𝟐 = Sample variances of Group 1 and Group 2
𝒏𝟏, 𝒏𝟐 = Sample sizes of Group 1 and Group 2
Degrees of freedom (df): df = 𝒏𝟏+ 𝒏𝟐−𝟐
2.Paired Samples t-test (Dependent t-test)
The paired samples t-test is used when the samples are dependent, such as before-and-after
measurements. The formula for the t-statistic in the paired t-test is:
𝑑̅ = Mean of the differences between paired observations (after - before)
𝑠𝑑 = Standard deviation of the differences between the paired observations
n = Number of pairs
•
Assumptions:
The differences between the paired observations are normally distributed.
Degrees of freedom (df): df = n−1
Where n is the number of paired observations.
9.4. Commands and calculation of R:
Independent Samples t-test (Two-Sample t-test)
t.test(group_A, group_B): Conducts a t-test comparing group_A and group_B.
var.equal = TRUE: Assumes equal variances between groups. You can set var.equal = FALSE, if
variances are unequal.
Paired Samples t-test (Dependent Test)
t.test(before, after, paired = TRUE): Conducts a paired t-test comparing before and after
measurements.
paired = TRUE: Specifies that the data are paired (dependent).
Summary table for choosing the right t-test
Code in R:
Suppose you have test scores from two groups of students who studied under different methods,
and you want to test if their mean scores are significantly different.
group_a<-c(90,80,67)
group_b<-c(56,88,23)
t_test_independent<-t.test(group_a,group_b,var.equal=TRUE)
#use var.equal=FALSE if variances are unequal
print(t_test_independent)
Two Sample t-test
data: group_a and group_b
t = 1.1719, df = 4, p-value = 0.3063
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-31.94822 78.61489
sample estimates:
mean of x mean of y
79.00000 55.66667
1. Paired Samples t-test (Dependent t-test): Example Code in R
Suppose you measured students’ test scores before and after a specific training program.
before<-c(40,33,45)
after<-c(83,7,11)
t_test_paired<-t.test(before,after,paired=TRUE)
#print the result
print(t_test_paired)
Paired t-test
data: before and after
t = 0.23183, df = 2, p-value = 0.8382
alternative hypothesis: true mean difference is not equal to 0
95 percent confidence interval:
-99.50168 110.83502
sample estimates:
mean difference
5.666667
Interpreting t-test Results in R
For each t-test, the output includes:
• t-value: Indicates the size of the difference relative to the variation in the data.
• p-value: A p-value < 0.05 (or your chosen significance level) indicates a statistically
significant difference.
• Confidence Interval (CI): Shows the range within which the true mean difference likely
lies.
• Mean Difference: The estimated difference between means (relevant for paired and two-
sample t-tests).
9.6. Problems:
Problem 1: Suppose you are testing the effectiveness of two teaching methods on student
performance. You have two groups of students, each taught with a different method. Test whether
there is a significant difference between the two groups' average scores.
Method1: 78, 82, 84, 88, 90, 85, 87, 89, 90, 80
Method2: 75, 79, 81, 83, 78, 80, 82, 84, 85, 77
# Sample data
method1 <- c(78, 82, 84, 88, 90, 85, 87, 89, 90, 80)
method2 <- c(75, 79, 81, 83, 78, 80, 82, 84, 85, 77)
# Summary statistics
mean(method1) # Average of Method1
mean(method2) # Average of Method2
# Perform two-sample t-test
t.test(method1, method2, var.equal = TRUE)
Two Sample t-test
data: method1 and method2
t = 2.9139, df = 18, p-value = 0.009263
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
1.36709 8.43291
sample estimates:
mean of x mean of y
85.3 80.4
Problem 2: You want to check the effectiveness of a new learning app by measuring the same
students' scores before and after using it. Test if there is a significant improvement in scores after
using the app.
Before: 65, 68, 70, 72, 66, 75, 67, 69, 71, 68
After: 70, 72, 75, 78, 70, 80, 73, 75, 78, 74
# Scores before and after using the app
before <- c(65, 68, 70, 72, 66, 75, 67, 69, 71, 68)
after <- c(70, 72, 75, 78, 70, 80, 73, 75, 78, 74)
# Summary statistics
mean(before)
mean(after)
# Perform paired t-test
t.test(after, before, paired = TRUE)
Paired t-test
data: after and before
t = 17.676, df = 9, p-value = 2.692e-08
alternative hypothesis: true mean difference is not equal to 0
95 percent confidence interval:
4.7089 6.0911
sample estimates:
mean difference
5.4