R:图书馆;平行;必要吗?

R:图书馆;平行;必要吗?,r,parallel-processing,R,Parallel Processing,很抱歉再问一次。 我正在做关于样本大小、方差和不同分布的组合因素的模拟研究 现在,我想知道我需要包括这个吗 library(parallel) 在代码的开头 ########################################################################## #to evaluate the same R function on many different sets of data library(parallel) rm(list=l

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

现在,我想知道我需要包括这个吗

 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)
      }
##########################################################################
#在许多不同的数据集上计算相同的R函数
图书馆(平行)
rm(list=ls())#清理工作区

nSims并行删除包(
分离(“package:parallel”,unload=TRUE)
),并在不加载包的情况下运行代码。如果出现错误,说明找不到函数xy,则可能需要该包。
但是,我看不到您的代码中有任何行似乎需要并行包。

有没有“库(并行)”有什么区别?如果您不使用某些东西,就不需要加载它。但是如果您有性能问题,尝试优化代码可能会加快速度(因为循环在R中的速度非常慢),删除“s”,并将其称为包而不是库。谢谢,我根据你的建议修改了答案