在R编程中做T.test

时间:2013-03-20 11:48:32

标签: r statistics

我在进行t检验时需要一些帮助。我知道如何为单个数据集执行此操作,但我需要帮助进行2个样本t检验。我有一个名为data1的数据集:

2 个答案:

答案 0 :(得分:6)

数据:

data1 <- data.frame(n = 15, mean = 14, sd = 8)
data2 <- data.frame(n = c(17, 5, 8), mean = c(19, 17, 11), sd = c(7, 6, 9))

t检验函数(基于观察数,平均值和标准差):

T.test <- function(n, mean, sd) {
  s <- sum((n - 1) * sd^2) / (sum(n) - 2) # weighted variance
  t <- sqrt(prod(n) / sum(n)) * (diff(mean) / sqrt(s)) # t statistic
  df <- sum(n) - 2  # degrees of freedom
  p <- (1 - pt(abs(t), df)) * 2 # p value
  c(t = t, p = p)
}

将该函数应用于data2的所有行:

apply(data2, 1, function(x) T.test(c(x[1], data1$n),
                                   c(x[2], data1$mean), 
                                   c(x[3], data1$sd)))

输出显示data2中所有行的t值和p值:

         [,1]       [,2]      [,3]
t -1.98618371 -0.8215838 0.8730255
p  0.05621594  0.4220631 0.3925227

答案 1 :(得分:1)

R

中还有一个内置函数
?t.test

描述 对数据向量执行一次和两次样本t检验。

#just an easy example
a = c(12.9, 13.5, 12.8, 15.6, 17.2, 19.2, 12.6, 15.3, 14.4, 11.3)
b = c(12.7, 13.6, 12.0, 15.2, 16.8, 20.0, 12.0, 15.9, 16.0, 11.1)

t.test(a,b, paired=TRUE)

Paired t-test

data: a and b
t = -0.2133, df = 9, p-value = 0.8358
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.5802549 0.4802549
sample estimates:
mean of the differences
-0.05