2016-04-21 9 views
-2

es tut mir leid, noch einmal zu fragen. Ich mache Simulationsstudie in Bezug auf Kombinationsfaktoren von Stichprobengrößen, Varianzen und unterschiedliche Verteilung.R: Bibliothek "parallel" notwendig?

Nun, ich frage mich, kann ich das

library(parallel) 

am Anfang des Codes enthalten müssen?

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


#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) 
     } 
+0

wird es einen Unterschied mit oder ohne die "Bibliothek (parallel)" haben? –

+0

Wenn Sie etwas nicht verwenden, müssen Sie es nicht laden. Aber wenn Sie ein Performance-Problem haben, könnte der Versuch, Ihren Code zu optimieren, die Dinge beschleunigen (For-Schleifen können in R notorisch langsam sein) – Heroka

Antwort

4

das Paket parallel (detach("package:parallel", unload=TRUE)) entfernen und den Code ausführen, ohne das Paket zu laden. Wenn Sie eine Fehlermeldung erhalten, dass die Funktion xy nicht gefunden wurde, benötigen Sie möglicherweise das Paket. Ich sehe jedoch keine Zeile in Ihrem Code, die das parallele Paket zu benötigen scheint.

+1

Entfernen Sie das 's' und nennen Sie es ein _package_ nicht eine _library_. Die Antwort ist ansonsten genau richtig. –

+0

Danke, ich habe die Antwort nach Ihren Vorschlägen angepasst – basil

+0

Vielen Dank..ich bekomme das ganze Bild schon :) –