如何使用数据框中的因子列在 R 中执行配对 t 检验?
当我们在 R 数据框中有一个具有两个级别和一个数字列的因子列时,我们可以对这个数据框应用配对测试,但必须为相同的主题收集数据,否则它将不是配对数据。t.test这里讨论的数据的应用可以通过使用命令 t.test(y1~x1,data=df) 来完成,其中 y1 是数值列,x1 是因子列,这两个列都存储在称为df。
示例
考虑以下数据框 -
x1<-sample(c("Male","Female"),20,replace=TRUE)输出结果y1<-rpois(20,5)
df1<-data.frame(x1,y1)
df1
x1 y11 Female 4
2 Male 4
3 Female 4
4 Male 4
5 Female 6
6 Male 4
7 Female 3
8 Male 4
9 Female 7
10 Male 6
11 Male 2
12 Female 1
13 Male 5
14 Male 8
15 Male 6
16 Male 6
17 Female 3
18 Female 5
19 Male 4
20 Male 5
t.test在 df1 中应用数据 -
示例
t.test(y1~x1,data=df1)输出结果
Welch Two Sample t-testdata: y1 by x1
t = -0.88636, df = 12.897, p-value = 0.3917
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-2.436194 1.019527
sample estimates:
mean in group Female mean in group Male
4.125000 4.833333
示例
x2<-sample(c("Hot","Cold"),20,replace=TRUE)输出结果y2<-sample(0:9,20,replace=TRUE)
df2<-data.frame(x2,y2)
df2
x2 y21 Hot 8
2 Cold 1
3 Hot 5
4 Hot 2
5 Cold 4
6 Cold 0
7 Hot 8
8 Cold 3
9 Cold 9
10 Cold 6
11 Cold 0
12 Cold 9
13 Hot 6
14 Hot 2
15 Cold 3
16 Hot 1
17 Cold 6
18 Hot 7
19 Hot 8
20 Hot 9
t.test在 df2 中应用数据 -
示例
t.test(y2~x2,data=df2)输出结果
Welch Two Sample t-testdata: y2 by x2
t = -1.0627, df = 17.721, p-value = 0.3022
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-4.46872 1.46872
sample estimates:
mean in group Cold mean in group Hot
4.1 5.6
以上是 如何使用数据框中的因子列在 R 中执行配对 t 检验? 的全部内容, 来源链接: utcz.com/z/353625.html