您好,我是 R 新手,正在尝试运行配对 t.test。
目前已使用pivot_longer从宽格式重塑数据,而不是在单独的列中进行测试,因为我已经有一个时间点列,但不确定如何从这里继续。如何根据时间点和测试运行 t.test?
样本数据集:
id timepoint test score
<chr> <chr> <chr> <dbl>
1 101 Pre testA 35
2 101 Pre testB 37
3 101 Pre testC 41
4 101 Post testA 39
5 101 Post testB 16
6 101 Post testC 33
7 102 Pre testA 27
8 102 Pre testB 21
9 102 Pre testC 25
是否也可以一次性完成所有测试?
我使用pivot_longer之前的数据集示例
id timepoint testA testB testC
<chr> <chr> <dbl> <dbl> <dbl>
1 101 Pre 35 37 41
2 101 Post 39 16 33
3 102 Pre 27 21 25
谢谢!
目前我只知道:t.test(pre_test_scores, post_test_scores,paired = TRUE)
编辑:为了更加清晰,包含了一个可重现的示例数据集
set.seed(123)
id <- sample(100:150, 40)
id_rep <- rep(id, each = 6) # 2 timepoints * 3 subscales = 6
timepoints <- rep(c("pre", "post"), each = 3, times = 40)
subscales <- rep(c("emotional_wellbeing", "physical_wellbeing", "overall_wellbeing"), times = 80)
set.seed(456) # Setting seed for reproducibility of scores
scores <- sample(1:100, 240, replace = TRUE) # Assuming scores are between 1 and 100
data <- data.frame(ID = id_rep, Timepoint = timepoints, Subscale = subscales, Score = scores)
print(data)
ID Timepoint Subscale Score
1 130 pre emotional_wellbeing 35
2 130 pre physical_wellbeing 38
3 130 pre overall_wellbeing 85
4 130 post emotional_wellbeing 27
5 130 post physical_wellbeing 25
6 130 post overall_wellbeing 78
7 114 pre emotional_wellbeing 31
8 114 pre physical_wellbeing 73
9 114 pre overall_wellbeing 79
10 114 post emotional_wellbeing 90
11 114 post physical_wellbeing 83
12 114 post overall_wellbeing 43
13 113 pre emotional_wellbeing 87
14 113 pre physical_wellbeing 8
15 113 pre overall_wellbeing 14
16 113 post emotional_wellbeing 13
17 113 post physical_wellbeing 69
18 113 post overall_wellbeing 52
19 102 pre emotional_wellbeing 30
20 102 pre physical_wellbeing 47
21 102 pre overall_wellbeing 25
22 102 post emotional_wellbeing 38
23 102 post physical_wellbeing 40
24 102 post overall_wellbeing 34
25 141 pre emotional_wellbeing 27
26 141 pre physical_wellbeing 68
27 141 pre overall_wellbeing 28
28 141 post emotional_wellbeing 10
29 141 post physical_wellbeing 31
30 141 post overall_wellbeing 65
31 142 pre emotional_wellbeing 74
32 142 pre physical_wellbeing 25
33 142 pre overall_wellbeing 43
34 142 post emotional_wellbeing 20
35 142 post physical_wellbeing 30
36 142 post overall_wellbeing 75
37 136 pre emotional_wellbeing 88
38 136 pre physical_wellbeing 34
39 136 pre overall_wellbeing 38
40 136 post emotional_wellbeing 94
41 136 post physical_wellbeing 99
42 136 post overall_wellbeing 15
43 148 pre emotional_wellbeing 97
44 148 pre physical_wellbeing 4
45 148 pre overall_wellbeing 84
46 148 post emotional_wellbeing 16
47 148 post physical_wellbeing 25
48 148 post overall_wellbeing 62
49 124 pre emotional_wellbeing 66
50 124 pre physical_wellbeing 87
51 124 pre overall_wellbeing 27
52 124 post emotional_wellbeing 80
53 124 post physical_wellbeing 80
54 124 post overall_wellbeing 88
如果问题想要在配对的基础上使用单个 p 值来测试 pre = post,则首先创建一个宽数据框,然后运行
t.test
或 lm
,如图所示 - 两者都给出相同的 p 值。
library(tidyr)
wide <- data %>%
pivot_wider(id_cols = c(ID, Subscale),
names_from = Timepoint, values_from = Score)
with(wide, t.test(pre, post, paired = TRUE))
# or
summary(lm(post - pre ~ 1, wide))