跨多列进行t测试或整理数据 [英] T-tests across multiple columns or tidy the data
本文介绍了跨多列进行t测试或整理数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
新发布到Stack,因此对于任何问题,我们深表歉意。
我正在学习如何更好地使用R,目前正在考虑使用broom/purr一次运行多个统计测试。我当前数据的示例如下所示:
主题 | PreScoreTestA | PostScoreTestA | PreScoreTestB | PostScoreTestB | PreScoreTestC | PostScoreTestC |
---|---|---|---|---|---|---|
1 | 30 | 40 | 6 | 8 | 12 | 10 |
2 | 15 | 12 | 9 | 13 | 7 | 7 |
3 | 20 | 22 | 11 | 12 | 9 | 10 |
但是涉及很多科目和更多的测试。我希望进行相关t检验,以查看培训计划过程中分数的变化,但不想为每个分数运行测试。
我见过几个使用GROUP BY、NEST和MAP运行多个t测试的示例,但他们的数据格式较长
有没有办法在格式宽泛的情况下实现相同的目标?或者我是否需要使用PIVOT_LONG来更改数据。
提前感谢!
ETA在此处进行了编辑,但提供的结果不正确,因此已删除仍在寻找有关参数和相同长度的帮助
ETA版本2
我确实找到了使用pairwise.t.test(下面的代码)的解决方法。它给出的p值与在各个评估中进行t.test相同。我很好奇为什么它可以用于pairtwe.t.test,而不能用于t.test。如果有人有什么想法,请告诉我!
results <- testb %>%
pivot_longer(-Subject,
names_to = c("time", "test"), values_to = "score",
names_pattern = "(Pre|Post)(.*)") %>%
group_by(test) %>%
nest() %>%
mutate(ttests = map(.x=data, ~tidy(pairwise.t.test(.x$score, .x$time, paired = TRUE, p.adjust.method = "none")))) %>%
unnest(ttests)
推荐答案
是,需要一些透视。假设您没有方向性假设,并且您想为每个测试做一次事后评估,这可能就是您想要的:
df <- as.data.frame(rbind(c(1, 30, 40, 6, 8, 12, 10),
c(2, 15, 12, 9, 13, 7, 7),
c(3, 20, 22, 11, 12, 9, 10)))
names(df) <- c("Subject",
"PrePushup", "PostPushup",
"PreRun", "PostRun",
"PreJump", "PostJump")
df %>%
pivot_longer(-Subject,
names_to = c("time", "test"), values_to = "score",
names_pattern = "(Pre|Post)(.*)") %>%
group_by(test) %>%
nest() %>%
mutate(t_tests = map(data, ~t.test(score ~ time, data = .x, paired = TRUE))) %>%
pull(t_tests) %>%
purrr::set_names(c("Pushup", "Run", "Jump"))
$Pushup
Paired t-test
data: score by time
t = 0.79241, df = 2, p-value = 0.5112
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-13.28958 19.28958
sample estimates:
mean of the differences
3
$Run
Paired t-test
data: score by time
t = 2.6458, df = 2, p-value = 0.1181
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-1.461250 6.127916
sample estimates:
mean of the differences
2.333333
$Jump
Paired t-test
data: score by time
t = -0.37796, df = 2, p-value = 0.7418
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-4.127916 3.461250
sample estimates:
mean of the differences
-0.3333333
这篇关于跨多列进行t测试或整理数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文