我正在使用多个分组变量(标记)进行t检验,这些变量只有两个组(0或1)。在完整的数据中有一百万个分组变量,例如n_obs = 1e + 06,nvals = 300,5%NA。
> n_obs = 1e+04 # to simulate grouping matrix
> n_vals = 100
> g = matrix(sample(0:1, n_obs * n_vals, replace=TRUE), n_obs, n_vals)
> row.names(g) = paste("marker", 1:nrow(g), sep="")
> colnames (g) = paste("country", 1:ncol(g), sep="")
> g[1:5,1:2]
country1 country2 country3 country4 country5
marker1 1 1 1 1 0
marker2 1 0 0 0 0
> vals = rnorm (n_vals) ; names(vals) = colnames(g) # to simulate values
> head(vals)
country1 country2 country3 country4 country5 country6
-0.4048584 0.2792725 0.4064460 0.9002677 0.2187961 0.2141666
> res = apply(g, 1, function(x) t.test(vals~ x)) ## applying the t-tests. Quite slow.
> tres = do.call(rbind, lapply(res, tidy)) ## tidying the t-tests. Very slow :(
> head(tres)
estimate estimate1 estimate2 statistic p.value parameter conf.low
marker1 -0.03560203 -0.07373907 -0.03813704 -0.17495425 0.8615063 90.52404 -0.4398452
marker2 0.27284988 0.07194537 -0.20090451 1.33127950 0.1863794 92.20240 -0.1341928
因为对于更大的数据集,整洁是如此缓慢,我想在单独的部分中进行t检验,并逐行循环“g”,以生成t检验的每个组件。
我可以'拆分'第一个标记的值,然后得到每个组的总和:
> mysplit = split( vals, g[1,])
> lapply(mysplit, mean)
$`0`
[1] -0.07373907
$`1`
[1] -0.03813704
如何'循环'遍历'g'的所有行,得到每组的'vals'的总和,然后是标准偏差等?
我正在努力保持功能简单。