【问题标题】:I am receiving an error when running a t-test + tidy() on the output在输出上运行 t-test + tidy() 时收到错误消息
【发布时间】:2021-07-02 11:36:39
【问题描述】:

我正在尝试对下面的数据进行 t 检验,但它返回的错误是:

Error in var(if (is.vector(x) || is.factor(x)) x else as.double(x), na.rm = na.rm) : Calling var(x) on a factor x is defunct. Use something like 'all(duplicated(x)[-1L])' to test for a constant vector.

library(tidyverse)
library(broom)

food_consumption <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-02-18/food_consumption.csv')

food_consumption %>% 
  mutate(vegan = if_else(food_category %in% c("Wheat and Wheat Products", "Rice", "Soybeans", "Nuts inc. Peanut Butter"), "Non-Animal Product", "Animal Product")) %>% 
  select(consumption, co2_emmission, vegan) %>% 
  pivot_longer(!vegan, names_to = "type", values_to = "value") %>%
  mutate(type = as.factor(type),
         vegan = as.factor(vegan)) %>%
  group_by(type) %>% 
  do(test = t.test(value~vegan, data = (.))) %>% 
  tidy(test)

有人知道这里发生了什么吗?以及如何无误地整理 t 检验输出?如果我在末尾排除 tidy(test) 位,则 t 检验会按预期返回两个列表对象,但如果我尝试调用 tidy() 它会返回上述错误。

我正在关注一个运行完全相同代码的教程(除了它使用 gather 而不是 pivot_wider 但两者都产生相同的数据集)。 Timestamped link here.

【问题讨论】:

    标签: r dplyr broom


    【解决方案1】:
    1. group_test - 通过type 变量创建组

    2. map(data, ~t.test(value~vegan, date = .x) %&gt;% tidy - 我们为每个组计算t.test

    3. unnest(test) - 将结果展开为列

          mutate(vegan = if_else(food_category %in% c("Wheat and Wheat Products", "Rice", "Soybeans", "Nuts inc. Peanut Butter"), "Non-Animal Product", "Animal Product")) %>% 
          select(consumption, co2_emmission, vegan) %>% 
          pivot_longer(!vegan, names_to = "type", values_to = "value") %>%
          mutate(type = as.factor(type),
                 vegan = as.factor(vegan)) %>%
          group_nest(type) %>% 
          transmute(type, test = map(data, ~t.test(value~vegan, data = .x) %>% tidy)) %>% 
          unnest(test)```
        
            # A tibble: 2 x 11
              type      estimate estimate1 estimate2 statistic  p.value parameter conf.low conf.high
              <fct>        <dbl>     <dbl>     <dbl>     <dbl>    <dbl>     <dbl>    <dbl>     <dbl>
            1 co2_emmi~    93.7      108.       14.7     15.3  1.25e-47      984.    81.7     106.  
            2 consumpt~     2.56      29.0      26.5      1.01 3.12e- 1     1334.    -2.40      7.53
            # ... with 2 more variables: method <chr>, alternative <chr>
    

    【讨论】:

    • 虽然毫无疑问是正确的,但我觉得这个答案将受益于对 what 正在做什么以及 为什么 会产生预期结果的解释。
    • 添加了解释。也许我的英语不好,无法很好地解释
    • 这真的很有用。我已经接受了另一个答案,因为它对我来说更容易理解。但这仍然非常有用。谢谢!
    • @YuriySaraykin:你的英语很好。谢谢你。 transmute() 对我来说是新的。可能值得为此添加一个项目符号?不过,我现在给我 +1。
    【解决方案2】:

    你可以试试这个。我只更改了代码的最后两行。

    food_consumption %>% 
      mutate(vegan = if_else(food_category %in% c("Wheat and Wheat Products", "Rice", "Soybeans", "Nuts inc. Peanut Butter"), "Non-Animal Product", "Animal Product")) %>% 
      select(consumption, co2_emmission, vegan) %>% 
      pivot_longer(!vegan, names_to = "type", values_to = "value") %>%
      mutate(type = as.factor(type),
             vegan = as.factor(vegan)) %>%
      
      # here what I've changed!
      nest_by(type) %>% 
      summarise(tidy(t.test(value~vegan, data = data)), .groups = "drop")
    
    #> # A tibble: 2 x 11
    #>   type    estimate estimate1 estimate2 statistic  p.value parameter conf.low conf.high method   alternative
    #>   <fct>      <dbl>     <dbl>     <dbl>     <dbl>    <dbl>     <dbl>    <dbl>     <dbl> <chr>    <chr>      
    #> 1 co2_em~    93.7      108.       14.7     15.3  1.25e-47      984.    81.7     106.   Welch T~ two.sided  
    #> 2 consum~     2.56      29.0      26.5      1.01 3.12e- 1     1334.    -2.40      7.53 Welch T~ two.sided 
    

    正如您在nest_by 中看到的那样,我嵌套 将数据框分成两行(每个type 一个)。然后,您有一个包含两列的嵌套数据框。第二列是名为data 的数据帧列表。

    dplyr 版本 > 1.0,summarise 现在更加灵活,您可以直接使用它来进行每行返回多个值的操作。查看?dplyr 了解更多信息。

    因此,您可以像我一样直接在结果上应用tidy

    .groups = "drop" 只是为了从summarise 中删除一条烦人的消息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 2018-02-12
      • 2020-07-08
      • 2015-09-29
      • 2020-02-29
      • 2023-03-13
      • 1970-01-01
      相关资源
      最近更新 更多