【问题标题】:How to interpret t.test in R如何在 R 中解释 t.test
【发布时间】:2021-10-29 09:08:10
【问题描述】:

我有一个数据集,其中包含来自不同行业组的许多公司的两个变量(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检验?

【问题讨论】:

  • 我对你的问题有点不清楚。数据集中只有一个test1变量,所以我不知道你所说的“每个变量test1”是什么意思...?
  • 我的数据集中有 500 家公司。为每个公司计算变量“test1”。我更新了我的问题

标签: r tidy hypothesis-test


【解决方案1】:

我们也可以使用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  

【讨论】:

    【解决方案2】:

    因此 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 检验

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-18
      • 1970-01-01
      • 2013-10-25
      相关资源
      最近更新 更多