本站分享:AI、大数据、数据分析师培训认证考试,包括:Python培训Excel培训Matlab培训SPSS培训SAS培训R语言培训Hadoop培训Amos培训Stata培训Eviews培训

R语言 t检验多核并行运算 v1.2_ r语言 t检验

r语言 cdadata 4016℃
对函数进行了些许修改,使其能对NA数据正确处理,下载点这里

国庆前写了一个关于R语言中t检验的多核计算函数,不幸的是这个函数不仅不能有效的减少计算时间,反而将时间变为原来计算时间的10倍,堪称绝对失败的一个尝试。经过国庆期间的努力,终于修改了一个新的版本,新的版本可以有效的减少计算时间。

函数下载地址:http://s.yunio.com/tmw8rc

R语言 t检验多核并行运算 v1.2

图中的1表示为单核进行计算时,不使用multicore包,后面的一次为1:8核进行计算时间。可以明显发现计算时间的减少。
#These two function are based on the package “multicore”
#It can be reduce your cost efficiently when you run t-test for bulk data 
sc.t.test<-function(data)
{
     if(!is.vector(data) & !is.matrix(data)) stop(“Need a vector or a matrix…”)
     if(length(data) != floor(length(data)/2)*2) stop(“There is odd number…”)
     if(is.vector(data))
     { 
       k<-unlist(t.test(data[1:5],data[6:10])[3])
     }
     if(is.matrix(data))
     {
       k<-array(numeric())
       for(i in 1:length(data[,1]))
       {
         k[i]<-unlist(t.test(data[i,1:5],data[i,6:10])[3])
       }
     }
     return(k)
}
mc.t.test<-function(data,mc.core=parallel::detectCores())
{
p<-array(numeric())
k<-array(numeric())
mc.core<-ifelse(mc.core
for( i in 1:mc.core)
{
    length<- ifelse(i==mc.core,(length(data[,1])-ceiling(length(data[,1])/mc.core)*(i-1)),(ceiling(length(data[,1])/mc.core)))
    start<-ifelse(i==mc.core,ceiling(length(data[,1])/mc.core)*(i-1)+1,(i-1)*length+1)
    end<-ifelse(i==mc.core,length(data[,1]),i*length)
    if (end >= length(data[,1])) end = length(data[,1])
    if (start >= length(data[,1])) start = length(data[,1])
    if(start != end ) {p[i]<-parallel(sc.t.test(data[start:end,]))$pid}else{p[i]<-parallel(sc.t.test(data[start,]))$pid}
    if(end == length(data[,1])) break
}
    result<-unlist(collect(p))
    attributes(result)<-NULL
    return(result)
}

转载请注明:数据分析 » R语言 t检验多核并行运算 v1.2_ r语言 t检验

喜欢 (3)or分享 (0)