【问题标题】:Extracting values over loop of t-tests通过 t 检验循环提取值
【发布时间】:2016-05-19 10:17:43
【问题描述】:

我正在尝试迭代约 60 列,目标是执行按案例/控制状态加权的 t 检验,并将输出捕获为列表。这是我迄今为止的尝试——请注意,我的数据框称为生物标记,第 3-59 列代表我感兴趣的变量,由第 2 列加权(称为案例):

tests <- list()
column_biomarkers <- colnames(biomarkers[3:59])

for (i in column_biomarkers){
  tests[[i]] <-  t.test(biomarkers$i[case == 1],biomarkers$i[case == 0],pool.sd=FALSE,na.rm=TRUE)
  }

sapply(tests, function(x) {
  c(x$estimate[1],
    x$estimate[2],
    ci.lower = x$conf.int[1],
    ci.upper = x$conf.int[2],
    p.value = x$p.value)
})

但是,我的这种尝试会导致以下错误:

var(x) 中的错误:'x' 为 NULL

任何建议将不胜感激!我是使用 R 的新手。

样本数据:

structure(list(subject = 1:10, case = c(1L, 0L, 0L, 1L, 1L, 0L, 
0L, 0L, 0L, 1L), biomarker_1 = c(308.29999, 2533.3, 2723.3, 3125.3, 
853, 6442.2998, 1472.5, 170.5, 64.5, 2624.8), biomarker_2 = c(4930.7998, 
2401, 5158.5, 6526, 3774.2, 5753, 1955.2, 1332.2, 1296.8, 5859.2998
), biomarker_3 = c(4810, 3279.5, 7929.5, 8353, 4074.2, 7940.5, 
1545.7, 2189.2, 1488.7, 6352.5)), .Names = c("subject", "case", 
"biomarker_1", "biomarker_2", "biomarker_3"), row.names = c(NA, 
10L), class = "data.frame")

【问题讨论】:

  • 不知道为什么你有一个错误,但所有的tests[[i]] 应该是一样的,因为你没有在t.test() 调用中使用i
  • 哎呀——应该是生物标志物$i ...抱歉,谢谢。我编辑了,上面是产生错误的代码。
  • 你应该这样写:biomarkers[case == 1,i]
  • 感谢 HubertL。如果我按照建议更正代码,则会收到以下错误: if (stderr

标签: r


【解决方案1】:

考虑将数据框分成两个组,并使用mapply() 跨列运行 t 检验(多元应用函数在对象之间逐元素运行操作)。

controldf <- df[df$case==1, 3:ncol(df)]
treatmentdf <- df[df$case==0, 3:ncol(df)]

tfct <- function(v1, v2){
             t.test(v1, v2, pool.sd=FALSE, na.rm=TRUE)
        }

ttests <- mapply(tfct, controldf, treatmentdf)
ttests

#             biomarker_1               biomarker_2              
# statistic   -0.4310577                2.287416                 
# parameter   7.943542                  7.987304                 
# p.value     0.677885                  0.05152236               
# conf.int    Numeric,2                 Numeric,2                
# estimate    Numeric,2                 Numeric,2                
# null.value  0                         0                        
# alternative "two.sided"               "two.sided"              
# method      "Welch Two Sample t-test" "Welch Two Sample t-test"
# data.name   "v1 and v2"               "v1 and v2"    
#      
#             biomarker_3              
# statistic   1.169058                 
# parameter   7.995322                 
# p.value     0.2760513                
# conf.int    Numeric,2                
# estimate    Numeric,2                
# null.value  0                        
# alternative "two.sided"              
# method      "Welch Two Sample t-test"
# data.name   "v1 and v2"     

甚至将结果迁移到数据框:

# Transposed data frame output of results
testdf <- data.frame(t(ttests))
head(testdf)

#              statistic parameter    p.value              conf.int
# biomarker_1 -0.4310577  7.943542   0.677885   -3219.767, 2206.667
# biomarker_2   2.287416  7.987304 0.05152236 -19.24659, 4598.82973
# biomarker_3   1.169058  7.995322  0.2760513   -1785.201, 5455.684
#                       estimate null.value alternative
# biomarker_1   1727.85, 2234.40          0   two.sided
# biomarker_2 5272.575, 2982.783          0   two.sided
# biomarker_3 5897.425, 4062.183          0   two.sided
#                              method data.name
# biomarker_1 Welch Two Sample t-test v1 and v2
# biomarker_2 Welch Two Sample t-test v1 and v2
# biomarker_3 Welch Two Sample t-test v1 and v2    

【讨论】:

  • 谢谢!这很有意义。但是,将其应用于我自己的数据时,出现以下错误: if (stderr
  • 不要在拆分数据框中包含主题或案例列。注意列的子集3: ncol(df)
【解决方案2】:

这是另一种可能的解决方案。

tests <- list()
column_biomarkers <- colnames(biomarkers[3:5])
for (i in column_biomarkers){
  tests[[i]] <-t.test(biomarkers[[i]][biomarkers$case == 1],biomarkers[[i]][biomarkers$case == 0],pool.sd=FALSE,na.rm=TRUE)
}

R 不喜欢 biomarkers$i[biomarkers$case == 1],R 不接受 i 作为有效的列名,因此使用 [[ ]] 表示法似乎有效。

【讨论】:

  • 虽然这可以解决 OP 的问题,但 R 的最佳实践是 vectorized code 而不是迭代编程(即循环遍历向量/矩阵的元素),通常在较低级别的语言中完成。
猜你喜欢
  • 2015-12-25
  • 2011-02-21
  • 2022-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-23
相关资源
最近更新 更多