如何在 R 中解释 t.test

How to interpret t.test in R

我有一个数据集,其中包含来自属于不同行业组的许多公司的两个变量(x1 和 x2)。我计算了大约 500 家公司的变量“test1”。我们得到以下代码:

 df$test1 <- df$x1 - df$x2

library(broom)
result.test <- df %>% 
  group_by(industry) %>% do(tidy(t.test(.$test1, alt="two.sided", mu=0)))

结果按“行业”分组,但我不清楚 t 检验是如何进行的。是对每个变量“test1”进行t检验,然后在行业组中呈现平均结果,还是为每个行业组确定“test1”的平均值,然后进行t检验?

因此t检验应用于每个行业级别的子集,这里以mtcars为例:

library(broom)
result.test <-
  mtcars %>% 
  group_by(cyl) %>%
  do(tidy(t.test(.$drat, alt="two.sided", mu=0)))

# A tibble: 3 x 9
# Groups:   cyl [3]
    cyl estimate statistic  p.value parameter conf.low conf.high method            alternative
  <dbl>    <dbl>     <dbl>    <dbl>     <dbl>    <dbl>     <dbl> <chr>             <chr>      
1     4     4.07      36.9 5.03e-12        10     3.83      4.32 One Sample t-test two.sided  
2     6     3.59      19.9 1.04e- 6         6     3.15      4.03 One Sample t-test two.sided  
3     8     3.23      32.4 7.93e-14        13     3.01      3.44 One Sample t-test two.sided  

现在,我将只过滤 cyl = 4

mtcars %>% 
  filter(cyl == 4) %>% 
  do(tidy(t.test(.$drat, alt="two.sided", mu=0)))

  estimate statistic  p.value parameter conf.low conf.high method            alternative
     <dbl>     <dbl>    <dbl>     <dbl>    <dbl>     <dbl> <chr>             <chr>      
1     4.07      36.9 5.03e-12        10     3.83      4.32 One Sample t-test two.sided 

我得到了相同的结果,所以这就像对按

分组的变量的每个级别的每个子集应用 t 检验

我们也可以用nest_by

library(dplyr)
library(tidyr)
library(broom)
mtcars %>%
    nest_by(cyl) %>%
    transmute(out = list(tidy(t.test(data$drat, alt = 'two.sided', 
         mu = 0)))) %>% 
    ungroup %>% 
    unnest(out)

-输出

# A tibble: 3 x 9
    cyl estimate statistic  p.value parameter conf.low conf.high method            alternative
  <dbl>    <dbl>     <dbl>    <dbl>     <dbl>    <dbl>     <dbl> <chr>             <chr>      
1     4     4.07      36.9 5.03e-12        10     3.83      4.32 One Sample t-test two.sided  
2     6     3.59      19.9 1.04e- 6         6     3.15      4.03 One Sample t-test two.sided  
3     8     3.23      32.4 7.93e-14        13     3.01      3.44 One Sample t-test two.sided