R:图书馆" parallel"必要?

时间:2016-04-21 05:25:47

标签: r parallel-processing

很抱歉再问一遍。 我正在进行关于样本大小,方差和不同分布的组合因素的模拟研究。

现在,我想知道我是否需要包含此

 library(parallel)

在代码的开头?

##########################################################################


#to evaluate the same R function on many different sets of data
library(parallel)

rm(list=ls())    # clean the workspace
nSims<-10000
alpha<-0.05

#set nrow =nsims because wan storing every p-value simulated 
#for gamma distribution with equal skewness
matrix2_equal  <-matrix(0,nrow=nSims,ncol=5)
matrix5_unequal<-matrix(0,nrow=nSims,ncol=5)
matrix8_mann   <-matrix(0,nrow=nSims,ncol=5)

# to ensure the reproducity of the result 
#here we declare the random seed generator
set.seed(1)

## Put the samples sizes into matrix then use a loop for sample sizes
 sample_sizes<-matrix(c(10,10,10,25,25,25,25,50,25,100,50,25,50,100,100,25,100,100),
 nrow=2)

#shape parameter for gamma distribution for equal skewness
#forty five cases for each skewness!!!!
sp1<-matrix(rep(c(16/9),each=45),ncol=1)

scp <- c(1,1.5,2,2.5,3)

##(use expand.grid)to create a data frame 
ss_scp<- expand.grid(sample_sizes[2,],scp)

#create a matrix combining the forty five cases of combination of sample sizes,shape and scale parameter
all <- cbind(rep(sample_sizes[1,], 5),ss_scp[,1],sp1,ss_scp[,2])

# name the column samples 1 and 2 and standard deviation
colnames(all) <- c("m","n","sp","scp")

#set empty vector of length no.of simulation(10000) to store p-value 
equal<-unequal<-mann<-c(rep(0,nrow(all)))

#set nrow =nsims because wan storing every p-value simulated 
#for gamma distribution with equal skewness
matrix2_equal  <-matrix(0,nrow=nSims,ncol=5)
matrix5_unequal<-matrix(0,nrow=nSims,ncol=5)
matrix8_mann   <-matrix(0,nrow=nSims,ncol=5)

    ##for the samples sizes into matrix then use a loop for sample sizes
     # this loop steps through the all_combine matrix
      for(ss in 1:nrow(all))  
      {
       #generate samples from the first column and second column
        m<-all[ss,1]
        n<-all[ss,2]   

           for (sim in 1:nSims)
           {
            #generate 2 random samples from gamma distribution with equal skewness
            gamma1<-rgamma(m,1.777778,scale=all[ss,4])
            gamma2<-rgamma(n,1.777778,scale=1)

            #extract p-value out and store every p-value into matrix
            p<-t.test(gamma1,gamma2,var.equal=TRUE)$p.value 
            q<-t.test(gamma1,gamma2,var.equal=FALSE)$p.value
            r<-wilcox.test(gamma1,gamma2)$p.value 

            matrix2_equal[sim,1]<- p   
            matrix5_unequal[sim,1]<- q 
            matrix8_mann[sim,1] <- r
        }
           ##store the result
          equal[ss]<- sum(matrix2_equal[,1]<alpha)
          unequal[ss]<-sum(matrix5_unequal[,1]<alpha)
          mann[ss]<- sum(matrix8_mann[,1]<alpha)
      }

1 个答案:

答案 0 :(得分:4)

删除并行包(detach("package:parallel", unload=TRUE))并运行代码而不加载包。如果您收到错误,指出无法找到函数xy,则可能需要该软件包。 但是,我在代码中看不到任何似乎需要并行包的行。