需要帮助对 R 中 CSV 文档的不同列执行自动未配对 t 检验

Need help to perform an automated unpaired t-test over different columns from CSV document in R

我想在第 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]))
+ }```

output:
[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 

and so on
when I add paired=TRUE to the data:
code:
> 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)
+ }

结果完全一样,就好像他没有包含paired函数一样。 有人可以帮我吗? 非常感谢

您必须更改 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 检验也使用这种形式的索引,尽管它不会引发任何错误。