我想在第2列和第3栏、第4栏和第5栏、第6栏和第7栏之间进行自动配对t检验。当我使用下面的代码时,我可以执行t检验,但不能执行未配对的t检验。
数据:
patient weight_1 weight_2 BMI_1 BMI_2 chol_1 chol_2 gly_1 gly_2
1 A 86.0 97.0 34.44961 30.61482 86.0 97.0 34.44961 30.61482
2 B 111.0 55.5 33.51045 22.80572 111.0 55.5 33.51045 22.80572
3 C 92.4 70.0 28.51852 25.71166 92.4 70.0 28.51852 25.71166代码:
names <- colnames(dataframe)
for(i in seq(from = 2, to = 8, by = 2)){
print(names[i])
print(names[i+1])
print(t.test(dataframe[i], dataframe[i+1]))
}产出:
1 "weight_1“1 "weight_2”
Welch Two Sample t-test
data: dataframe[i] and dataframe[i + 1]
t = 1.3183, df = 75.892, p-value = 0.1914
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-2.459965 12.090735
sample estimates:
mean of x mean of y
91.50256 86.68718
[1] "BMI_1"
[1] "BMI_2"
Welch Two Sample t-test
data: dataframe[i] and dataframe[i + 1]
t = 1.5851, df = 75.866, p-value = 0.1171
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.3817027 3.3571650
sample estimates:
mean of x mean of y
30.45167 28.96394 诸若此类。当我将paired=TRUE添加到数据时:
names <- colnames(dataframe)
for(i in seq(from = 2, to = 8, by = 2)){
print(names[i])
print(names[i+1])
print(t.test(dataframe[i], dataframe[i+1]), paired=TRUE)
}结果是完全相同的,好像它不包括配对函数。有人能帮我吗?在此之前,非常感谢您。
发布于 2022-01-05 13:04:54
您必须更改t.test中的索引,以清楚地定义要使用以下列:
例如:
df <- data.frame(a = runif(10), b=runif(10), c=runif(10))
t1 <- t.test(df[1], df[2])
t1$p.value
t2 <- t.test(df[1], df[2], paired=T)
t2$p.value
Error in `[.data.frame`(y, yok) : undefined columns selected但
t2 <- t.test(df[,1], df[,2], paired=T)
t2$p.value很管用。所以在你的代码中应该是
print(t.test(dataframe[,i], dataframe[,i+1], paired=TRUE)) 对于配对的t.test。
我建议在配对t检验中也使用这种形式的索引,尽管它不会抛出任何错误。
https://stackoverflow.com/questions/70593199
复制相似问题