📅  最后修改于: 2023-12-03 14:47:11.371000             🧑  作者: Mango
在统计学中,t检验是一种用于检验两个样本均值是否存在显著差异的假设检验方法。在R语言中,我们可以使用t.test函数进行t检验的实现。
t.test(x, y = NULL,
alternative = c("two.sided", "less", "greater"),
mu = 0, paired = FALSE, var.equal = FALSE,
conf.level = 0.95, ...)
下面我们通过一个实例来演示如何使用t.test函数进行t检验。
# 读取数据
data1 <- c(9, 10, 11, 8, 7, 12, 11, 10, 9, 8)
data2 <- c(12, 11, 11, 11, 10, 13, 14, 12, 10, 9)
# 进行双侧t检验
t.test(data1, data2)
# 进行左侧t检验
t.test(data1, data2, alternative = "less")
# 进行右侧t检验
t.test(data1, data2, alternative = "greater")
返回结果如下:
Welch Two Sample t-test
data: data1 and data2
t = -3.314, df = 14.109, p-value = 0.004333
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-4.242177 -1.057823
sample estimates:
mean of x mean of y
9.4 11.3
Welch Two Sample t-test
data: data1 and data2
t = -3.314, df = 14.109, p-value = 0.002166
alternative hypothesis: true difference in means is less than 0
95 percent confidence interval:
-Inf -1.339507
sample estimates:
mean of x mean of y
9.4 11.3
Welch Two Sample t-test
data: data1 and data2
t = -3.314, df = 14.109, p-value = 0.9978
alternative hypothesis: true difference in means is greater than 0
95 percent confidence interval:
-4.255263 Inf
sample estimates:
mean of x mean of y
9.4 11.3
其中,第一个结果表明双侧t检验的结果,第二个结果表明左侧t检验结果,第三个结果表明右侧t检验结果。可以看到,两个样本的均值存在显著差异,因为p-value小于0.05,即在0.05的置信水平下,拒绝了两个样本均值相等的假设。