[R] R-dvel [robustness Simulation study of 2 sample test on several combination of factors ]
tan sj
sj_style_1125 at outlook.com
Wed Apr 6 17:54:09 CEST 2016
Hi, i think i have figured the purpose of using this index (i-1)*5+j in the previous example that you gave.
It is because that i have to consider the outer loop and inner loop also... so the iterative for i need to minus one because it have ran one times simulation already ,then times the number of sizes of inner loop, then plus the iterative of j....
then for the simulation, i think there will be ((ss-1)*nsds +sim) for the index in the body of for loop...
Is it correct?
Sent from my phone
On Jim Lemon <drjimlemon at gmail.com>, Apr 6, 2016 6:52 PM wrote:
You are running through two loops and putting the output into a
vector. Without this calculation you will overwrite the same elements
of the output vector instead of advancing through it. Try this
example:
# like your 54 element vector of calculations for one condition
testmat1<-rep(0,25)
testmat2<-rep(0,25)
# now try to fill it with the inner loop index
for(i in 1:5) {
for(j in 1:5) {
testmat1[j]<-i+j
}
}
# do it again using a calculation similar to your question
for(i in 1:5) {
for(j in 1:5) {
testmat2[(i-1)*5+j]<-i+j
}
}
testmat1
testmat2
Try out some other things with this. It will give you an idea of how
to index elements of vectors. You could also use a matrix and then
convert it into a vector using as.vector:
testmat3<-matrix(0,nrow=5,ncol=5)
for(i in 1:5) {
for(j in 1:5) {
testmat3[i,j]<-i+j
}
}
as.vector(testmat3)
Jim
On Wed, Apr 6, 2016 at 8:04 PM, tan sj <sj_style_1125 at outlook.com> wrote:
> Hi, i am sorry to interrupt, can I ask about this few problems?
>
> t_equal[(ss-1)*nsds+i]<-t.test(x_norm1,y_norm2,var.equal=TRUE)$p.value
> 1. From above, what do "[(ss-1)*nsds+i]" brings to the code?
>
> 2. what is the purpose of having i (the iterative) here ? I see that dr. have set the sim as the iterative then why need to set i <-1 and set it as i<-i+1?
>
> they are confusing me.....
> I am sorry if these are very simple problem....
>
>
> ________________________________________
> From: Jim Lemon <drjimlemon at gmail.com>
> Sent: Wednesday, April 6, 2016 4:00 AM
> To: tan sj; r-help mailing list
> Subject: Re: [R] 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:
>> Hi, Jim, i read through your example,
>> I tried to write a code modified yours example and my ideas ...
>> In last email, you replied that "results[[(ss-1)*nssds+ssd_index]]<-<your_test>" ,can i know further about this ? Because i am not very understand about this .
>> I am sorry that i am really a new bird in this field...
>> but the code turn out it have some error ...
>> Below shown my attempt...
>>
>> ## 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)
>>
>> 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 simulation(10000) to store p-value
>> t_equal <-c(length=nSims)
>> t_unequal <-c(length=nSims)
>> mann <-c(length=nSims)
>>
>> #set up matrix for storing data from the vectors
>> matrix_Equal<-matrix(t_equal,nrow=nSims)
>> matrix_Unequal<-matrix(t_unequal,nrow=nSims)
>> matrix_mann<-matrix(mann,nrow=nSims)
>>
>> ############Simulations
>>
>> #BULID A FUNCTION for a collection of statement
>> f<-function(m,n,sd)
>> {
>> for (i 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]
>> {
>> 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[i]<-t.test(x_norm1,y_norm2,var.equal=TRUE)$p.value
>> t_unequal[i]<-t.test(x_norm1,y_norm2,var.equal=FALSE)$p.value
>> mann[i] <-wilcox.test(x_norm1,y_norm2)$p.value
>>
>> ##store the result into matrix defined before
>> matrix_Equal<-t_equal
>> matrix_Unequal<-t_unequal
>> matrix_mann<-mann
>>
>> ##print results
>> matrix_Equal
>> matrix_Unequal
>> matrix_mann
>>
>> }
>> }
>> }
>> }
>> }
>> ________________________________________
>> From: Jim Lemon <drjimlemon at gmail.com>
>> Sent: Tuesday, April 5, 2016 2:38 AM
>> To: tan sj
>> Cc: r-help at r-project.org
>> Subject: Re: [R] R-dvel [robustness Simulation study of 2 sample test on several combination of factors ]
>>
>> Hi sst,
>> You could set up your sample sizes as a matrix if you don't want all
>> of the combinations:
>>
>> sample_sizes<-matrix(c(10,10,10,25,25,25,...),nrow=2)
>>
>> and then use one loop for the sample sizes:
>>
>> for(ss in 1:dim(sample_sizes)[2]) {
>> ss1<-sample_sizes[1,ss]
>> ss2<-sample_sizes[2,ss]
>>
>> then step through your SDs:
>>
>> for(ssd in c(4,4.4,5,6,8)) {
>>
>> Jim
>>
>> On Tue, Apr 5, 2016 at 11:15 AM, tan sj <sj_style_1125 at outlook.com> wrote:
>>> hi, i am new in this field.
>>>
>>>
>>> do
>>> favorite<http://stackoverflow.com/questions/36404707/simulation-study-of-2-sample-test-on-different-combination-of-factors#>
>>>
>>>
>>> If I wish to conduct a simulation on the robustness of two sample test by using R language, is that any ways in writing the code?
>>> There are several factors
>>> (sample sizes-(10,10),(10,25),(25,25),(25,50),(25,100),50,25),(50,100), (100,25),(100,100))
>>>
>>> (standard deviation ratio- (1.00, 1.50, 2.00, 2.50, 3.00 and 3.50))
>>> distribution of gamma distribution with unequal skewness and equal skewness
>>>
>>> I wish to test the pooled variance t test and welch t test and mann whitney by using the above combination of factors. But how can I combine them by using for loop or apply function??
>>> I am intending to use apply function but i am stucking. If i use for loop function, can i use for loop with vectors ?
>>> for (a in c(25,50,100)) #first group of sample sizes
>>> { for (b in c(25,50,100)) #second group of sample sizes
>>> { for (d in c(4,4.4,5,6,8)) #different SDs of first sample
>>> the above code is an example that I would like to modified but I found I have different sets of sample sizes.
>>>
>>> So if for loop with vectors, as shown in the code above, will the computer run from part (a) 25, to part(b) 25, then to the part (d) 4? then again the rotation 50->50->4.4?
>>>
>>> ?I hope can hear any news from this website ....please..thanks you.
>>>
>>> Regards
>>> sst
>>>
>>>
>>> [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
[[alternative HTML version deleted]]
More information about the R-help
mailing list