【问题标题】:Need help to perform an automated unpaired t-test over different columns from CSV document in R需要帮助才能对 R 中 CSV 文档的不同列执行自动非配对 t 检验
【发布时间】:2022-10-17 02:28:04
【问题描述】:

我想在第 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]“重量_1” [1]“重量_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)
 }

结果完全一样,好像不包括配对函数一样。有人可以帮我解决这个问题吗?提前谢谢了。

【问题讨论】:

    标签: r t-test


    【解决方案1】:

    您必须更改 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 检验,尽管它不会引发任何错误。

    【讨论】:

    • 我该怎么做?对不起,我是新手。。
    猜你喜欢
    • 1970-01-01
    • 2023-01-31
    • 2015-05-17
    • 2018-09-03
    • 1970-01-01
    • 2014-04-23
    • 1970-01-01
    • 2020-05-13
    • 2015-11-27
    相关资源
    最近更新 更多