Skip to content
Prev 359837 / 398503 Next

R-dvel [robustness Simulation study of 2 sample test on several combination of factors ]

You have quite a few mistakes in your example. The code below works
for me - you can wrap it in a function if you like. I think you will
need a lot more practice before you can write something like this in R
as you are missing close braces and haven't really worked out the
difference between the number of calculations you are doing for each
replication and the number of replications. It takes 5-10 minutes to
run.

 ## 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)

#create vector to combine all std deviations
sds<-c(4,6,8,10,12,14)
# this number is needed below
nsds<-length(sds)
set.seed(8)

#number of simulations
nSims<-10000
#set significance level,alpha for the whole simulatio
alpha<-0.05

#set empty vector of length no.of _calculations_ to store p-values
# Note: you have 54 calculations, not 10000
ncalcs<-dim(sample_sizes)[2]*nsds
t_equal <-c(rep(0,length=ncalcs))
t_unequal <-c(rep(0,length=ncalcs))
mann <-c(rep(0,length=ncalcs))

#set up matrix for storing data from the vectors
# but you do want 10000 replications of each calculation
matrix_Equal<-matrix(rep(NA,ncalcs*nSims),nrow=nSims)
matrix_Unequal<-matrix(rep(NA,ncalcs*nSims),nrow=nSims)
matrix_mann<-matrix(rep(NA,ncalcs*nSims),nrow=nSims)

############Simulations

for (sim in 1:nSims){
 # this loop steps through the sample sizes
 for(ss in 1:dim(sample_sizes)[2]) {
  m<-sample_sizes[1,ss]
  n<-sample_sizes[2,ss]
  # initialize the index for results
  i<-1
  for (sd in sds) {  #first group's standard deviation
   #generate random samples from 2 normal distribution
   x_norm1<-rnorm(m,5,sds)
   y_norm2<-rnorm(n,5,4)
   #extract p-value out and store it in vectors
   t_equal[(ss-1)*nsds+i]<-t.test(x_norm1,y_norm2,var.equal=TRUE)$p.value
   t_unequal[(ss-1)*nsds+i]<-t.test(x_norm1,y_norm2,var.equal=FALSE)$p.value
   mann[(ss-1)*nsds+i] <-wilcox.test(x_norm1,y_norm2)$p.value
   i<-i+1
  }
 }
 #store the current result into matrices by rows
 matrix_Equal[sim,]<-t_equal
 matrix_Unequal[sim,]<-t_unequal
 matrix_mann[sim,]<-mann
}
##print results
matrix_Equal
matrix_Unequal
matrix_mann

Jim
On Wed, Apr 6, 2016 at 12:43 AM, tan sj <sj_style_1125 at outlook.com> wrote:
Message-ID: <CA+8X3fUd185i2__Yx8KpnnhhUXakuC_bjCgYvVhX7F4kFSXF5Q@mail.gmail.com>
In-Reply-To: <KL1PR01MB08871DA5DB29B6DB8C78579CB59E0@KL1PR01MB0887.apcprd01.prod.exchangelabs.com>