如何对数据子集运行t检验

时间:2017-10-09 20:44:13

标签: r statistics subset t-test

首先,这个数据集是否为t检验的整洁形式?

https://i.stack.imgur.com/tMK6R.png

其次,我试图进行两个样本t检验,以比较治疗a和b的时间3的方法,结果1'。我该怎么做呢?

示例数据:

df <- structure(list(code = c(100, 100, 100, 101, 101, 101, 102, 102, 
      102, 103, 103, 103), treatment = c("a", "a", "a", "b", "b", "b", 
      "a", "a", "a", "b", "b", "b"), sex = c("f", "f", "f", "m", "m", 
      "m", "f", "f", "f", "f", "f", "f"), time = c(1, 2, 3, 1, 2, 3, 
      1, 2, 3, 1, 2, 3), `outcome 1` = c(21, 23, 33, 44, 45, 47, 22, 
      34, 22, 55, 45, 56), `outcome 2` = c(21, 32, 33, 33, 44, 45, 
      22, 57, 98, 65, 42, 42), `outcome 3` = c(62, 84, 63, 51, 45, 
      74, 85, 34, 96, 86, 45, 47)), .Names = c("code", "treatment", 
      "sex", "time", "outcome 1", "outcome 2", "outcome 3"), 
      class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -12L))

1 个答案:

答案 0 :(得分:0)

首先,您必须定义要测试的子集,然后才能运行t检验。您不必像我已经完成的那样将子集存储在变量中,但它会使t检验输出更清晰。

通常使用t-test问题,我建议使用?t.test提供的帮助,但由于这涉及更复杂的子集,我在此处已经包含了如何执行此操作:

var_a <- df$`outcome 1`[df$treatment=="a" & df$time==3]
var_b <- df$`outcome 1`[df$treatment=="b" & df$time==3]

t.test(var_a,var_b)

输出:

    Welch Two Sample t-test

data:  var_a and var_b
t = -3.3773, df = 1.9245, p-value = 0.08182
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -55.754265   7.754265
sample estimates:
mean of x mean of y 
     27.5      51.5