r - 多列 t 检验的一个微妙问题
问题描述
我有一个回答多个问题的数据框(下面有 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,因为某种原因无法计算答案?
真挚地
托马斯飞利浦
解决方案
也许,您可以使用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))
推荐阅读
- android - 将联系人的姓名和号码设置为工具栏的标题和副标题
- tensorflow - Keras 置换层与重塑层
- python - Python 随机选择在每个循环迭代中返回相同的结果
- docker - docker 容器上的环境变量不会被覆盖
- python - mognoengine 和 bson 包不能一起工作
- sas - 使用 PROC SQL (Teradata) 获取过去 1 小时内的记录
- javascript - 自动计算器
- linux - 打开 AppImage 什么都不做。我能做些什么?
- python - 将 list[0], list[1], list[2],... 压缩成一个简单的语句
- java - 如何解决“无法应用适配器”错误?