【问题标题】:two sample t test in R: direction of comparison?R中的两个样本t检验:比较方向?
【发布时间】:2023-03-23 06:28:01
【问题描述】:

假设我有吸烟者和非吸烟者的肺活量数据。所以我们有一个带有数值的变量“lungCap”,以及带有“yes”或“no”值的变量“Smoking”。现在我想看看非吸烟者的容量是否大于吸烟者:

t.test(lungCap~smoking, alt="greater")

测试现在是否计算 “yes” > “no”“no” > “yes”?这是如何确定的?我在 t.test 命令的帮助中找不到它。

【问题讨论】:

  • 在文档中:alternative = "greater" 是 x 的均值大于 y 的替代方案。
  • @phiver - true,但文档没有直接指定t.test() 如何将自变量的值分配为xy,因此不清楚哪个将打印在左侧输出的列(x 侧)与右列(y 侧)。

标签: r statistics t-test


【解决方案1】:

使用基于字符的自变量时,t.test() 将根据自变量中值的字母顺序进行比较。

为了说明,我们将使用 1973 Motor Trend 汽车数据集比较手动变速箱与自动变速箱汽车的每加仑英里数。

我们将创建一个字符变量来表示自动与手动(以说明 OP 中的场景)并运行 t 测试。

我们将检验以下假设:

  • H_null:手动变速箱汽车的 mpg
  • H_alt:手动挡汽车的 mpg 大于自动挡汽车的 mpg。

为了运行测试,我们将加载数据,创建额外的列并执行t.test()

data(mtcars)
mtcars$trans <- ifelse(mtcars$am == 1,"manual","automatic")

t.test(mtcars$mpg ~ mtcars$trans,alt="greater")

...和输出:

> t.test(mtcars$mpg ~ mtcars$trans,alt="greater")

    Welch Two Sample t-test

data:  mtcars$mpg by mtcars$trans
t = -3.7671, df = 18.332, p-value = 0.9993
alternative hypothesis: true difference in means is greater than 0
95 percent confidence interval:
 -10.57662       Inf
sample estimates:
mean in group automatic    mean in group manual 
               17.14737                24.39231 

我们在这里看到的是 t.test() 运行自动 > 手动,因此 p 值为 0.9993。

为了正确运行测试,我们将对其进行修改以使用 alt="less" 参数。

> t.test(mtcars$mpg ~ mtcars$trans,alt="less")

    Welch Two Sample t-test

data:  mtcars$mpg by mtcars$trans
t = -3.7671, df = 18.332, p-value = 0.0006868
alternative hypothesis: true difference in means is less than 0
95 percent confidence interval:
      -Inf -3.913256
sample estimates:
mean in group automatic    mean in group manual 
               17.14737                24.39231 

>

在这里,我们看到报告的 p 值为 0.0006,这意味着我们拒绝原假设,支持替代假设,即自动变速箱汽车的平均每加仑英里数低于手动变速箱汽车。

更改比较顺序

针对 cme​​ts 中关于是否可以更改分组顺序的问题,t.test() 函数没有提供这样做的方法。但是,可以简单地在组名前添加1.2.,以强制t.test() 使用包含1. 的组作为比较中的第一个组。

回到我们的 mtcars 示例,如果我们希望手动变速器成为比较中的第一组,那么我们可以为备择假设 h_alt: mpg(manual) &gt; mpg(automatic) 获得正 t 值,我们可以使用以下代码。

data(mtcars)
mtcars$trans <- ifelse(mtcars$am == 1,"1. manual","2. automatic")
t.test(mtcars$mpg ~ mtcars$trans,alt="greater")

...和输出:

> t.test(mtcars$mpg ~ mtcars$trans,alt="greater")

    Welch Two Sample t-test

data:  mtcars$mpg by mtcars$trans
t = 3.7671, df = 18.332, p-value = 0.0006868
alternative hypothesis: true difference in means between group 1. manual and group 2. automatic is greater than 0
95 percent confidence interval:
 3.913256      Inf
sample estimates:
   mean in group 1. manual mean in group 2. automatic 
                  24.39231                   17.14737 

【讨论】:

  • 原假设应该是mpg of manual transmission cars = mpg of automatic transmission cars
  • @JasonBaik - 选项alt="less" 指定单尾测试。在单尾检验中,备择假设是 mpg(手动)> mpg(自动)。因此,原假设是 mpg(manual)
  • 是否有使用逆字母顺序或指定顺序计算双尾对比度的标志?能够指定它是“a - b”还是“b - a”会很好,因为这会影响 t 值的符号和平均差的置信区间。
  • @KayleSawyer 您是否找到了一种解决方案来颠倒比较组的顺序?我对分成“0”和“1”的组有同样的问题,但我需要将它们分组以供以后分析。我在它们上尝试了 fct_rev(),但它并没有改变平均差或 CI 的符号
  • @Cassandra -- 查看我更新的答案,了解改变比较顺序的技术。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-25
  • 1970-01-01
  • 2020-09-05
  • 1970-01-01
  • 2020-02-13
  • 1970-01-01
  • 2021-10-12
相关资源
最近更新 更多