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)
}
wird es einen Unterschied mit oder ohne die "Bibliothek (parallel)" haben? –
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