Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 一百万次t检验_R_Loops_Matrix_Lapply_Binary Matrix - Fatal编程技术网

R 一百万次t检验

R 一百万次t检验,r,loops,matrix,lapply,binary-matrix,R,Loops,Matrix,Lapply,Binary Matrix,我使用多个分组变量(标记)进行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("ma

我使用多个分组变量(标记)进行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
因为对于较大的数据集,tidy的速度非常慢,所以我考虑在单独的部分中进行t-test,并逐行循环“g”,以生成t-test的每个组件

我可以“拆分”第一个标记的值,然后得到每组的总和:

> mysplit = split( vals, g[1,])
> lapply(mysplit, mean)
$`0`
[1] -0.07373907
$`1`
[1] -0.03813704
如何“循环”遍历“g”的所有行,得到每组的“VAL”之和,然后是标准偏差等


为了提高速度,我尽量使函数保持简单。

这个整洁的函数是什么?您可以使用parallel加速t.test应用。但是看起来tidy是你自己的功能,所以可能可以优化。tidy功能来自我可以添加的broom包,实际上我正在使用并行包。我不知道为什么tidy需要这么长时间,但是你想要的所有信息都应该已经在t.test输出的列表中了,你只需要访问它。例如,您可以使用
unlist(lappy(res,function(x)x$p.value))
访问p_值,并将该向量添加到data.frame