【问题标题】:A subtle problem with t-tests over multiple columns多列 t 检验的一个微妙问题
【发布时间】:2021-04-25 15:21:35
【问题描述】:

我有一个回答多个问题的数据框(下面有 2 个问题的可重复示例)

set.seed(1)
df <- data.frame (
          UserId = c(rep("A", 4), rep("B", 4), rep("C", 4), rep("D", 4)),
          Sex = c(rep("Female", 8), rep("Male", 4), rep("No_Response", 4)),
          Answer_Date = as.Date(c("1990-01-01", "1990-02-01", "1990-03-01", "1990-04-01",
                                  "1991-02-01", "1991-03-01", "1991-04-01", "1991-05-01",
                                  "1992-03-01", "1992-04-01", "1992-05-01", "1992-06-01",
                                  "1993-07-10", "1992-08-10", "1993-09-10", "1993-10-10")),
          Q1 = sample(1:10, 16, replace = TRUE),
          Q2 = sample(1:10, 16, replace = TRUE)
      ) %>%
      group_by(UserId) %>%
      mutate(First_Answer_Date = min(Answer_Date)) %>%
      mutate(Last_Answer_Date  = max(Answer_Date)) %>%
      ungroup()

按照中的建议

https://sebastiansauer.github.io/multiple-t-tests-with-dplyr/

我针对真实均值为 0 的原假设对 Q1 和 Q2 运行 t 检验:

questions <- c("Q1", "Q2")
df %>%
  select(questions, Sex) %>%
  filter(Sex != "No_Response") %>%
  gather(key = variable, value = value, -Sex) %>%
  group_by(Sex, variable) %>%
  summarize(value = list(value)) %>%
  spread(Sex, value) %>%
  group_by(variable) %>%
  mutate( p_Female = t.test(unlist(Female))$p.value,
          p_Male   = t.test(unlist(Male)  )$p.value,
          t_Female = t.test(unlist(Female))$statistic,
          t_Male   = t.test(unlist(Male)  )$statistic) %>%
  mutate( Female = length(unlist(Female)),
          Male   = length(unlist(Male))
  )

这给了我

# A tibble: 2 x 7
# Groups:   variable [2]
  variable Female  Male  p_Female p__Male t_Female t_Male
  <chr>     <int> <int>     <dbl>   <dbl>    <dbl>  <dbl>
1 Q1            8     4 0.0000501 0.00137     8.78  11.6 
2 Q2            8     4 0.00217   0.0115      4.71   5.55

到目前为止一切顺利。当我只想在 First_Answer_Date 进行 t 检验时,我的麻烦就开始了。

df %>%
  filter(Answer_Date == First_Answer_Date) %>%
  select(questions, Sex) %>%
  filter(Sex != "No_Response") %>%

    # A tibble: 3 x 3
         Q1    Q2 Sex   
      <int> <int> <chr> 
    1     9     5 Female
    2     2     5 Female
    3     1     9 Male 

现在,只有一个男性和两个女性回复,而在第二季度,两位女性受访者的答案相同。如果我重新运行我的 t 检验代码

df %>%
  filter(Answer_Date == First_Answer_Date) %>%
  select(questions, Sex) %>%
  filter(Sex != "No_Response") %>%
  gather(key = variable, value = value, -Sex) %>%
  group_by(Sex, variable) %>%
  summarize(value = list(value)) %>%
  spread(Sex, value) %>%
  group_by(variable) %>%
  mutate( p_Female = t.test(unlist(Female))$p.value,
          p__Male = t.test(unlist(Male))$p.value,
          t_Female = t.test(unlist(Female))$statistic,
          t_Male = t.test(unlist(Male))$statistic) %>%
  mutate( Female = length(unlist(Female)),
          Male   = length(unlist(Male)))

Error: Problem with `mutate()` input `p_Female`.
x data are essentially constant
i Input `p_Female` is `t.test(unlist(Female))$p.value`.
i The error occurred in group 2: variable = "Q2".

我收到的错误消息是合乎逻辑的,但这是我在实践中可能会遇到的情况 - 一些子集的大小可能为 1 或 0,某些问题的所有受访者可能会给出相同的答案等等。 . 我怎样才能使代码优雅地降级,只需在其输出 tibble 中的那些单元格中放置一个空白或 NA,由于某种原因无法计算答案?

真诚的

托马斯·飞利浦

【问题讨论】:

    标签: r dplyr statistics t-test


    【解决方案1】:

    或许,您可以使用tryCatch 来处理错误:

    library(dplyr)
    library(tidyr)
    
    df %>%
      filter(Answer_Date == First_Answer_Date) %>%
      select(questions, Sex) %>%
      filter(Sex != "No_Response") %>%
      pivot_longer(cols = -Sex, names_to = "variable") %>%
      group_by(Sex, variable) %>%
      summarize(value = list(value)) %>%
      pivot_wider(names_from = Sex, values_from = value) %>%
      group_by(variable) %>%
      mutate( p_Female = tryCatch(t.test(unlist(Female))$p.value, error = function(e) return(NA)),
              p_Male   = tryCatch(t.test(unlist(Male) )$p.value, error = function(e) return(NA)),
              t_Female = tryCatch(t.test(unlist(Female))$statistic, error = function(e) return(NA)),
              t_Male   = tryCatch(t.test(unlist(Male))$statistic,error = function(e) return(NA))) %>%
      ungroup %>%
      mutate( Female = lengths(Female),
              Male   = lengths(Male))
    

    【讨论】:

    • 效果很好,谢谢。我很惊讶你在 return(NA) 周围没有大括号,即error = function(e) {return(NA)},但我检查了有无大括号,它工作得很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-17
    • 2021-12-28
    相关资源
    最近更新 更多