[R] use switch or function in connecting different cases.

David L Carlson dcarlson at tamu.edu
Mon Apr 25 18:46:58 CEST 2016


What do you mean by "it didnt update itself?"

You will get the same results on each run since you set the random seed to 1 when you call the function. As a result, each time you will get the same results.

-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352

-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of s ji
Sent: Monday, April 25, 2016 11:17 AM
To: r-help at r-project.org
Subject: Re: [R] use switch or function in connecting different cases.

This is my current work.Now i am trying to use a function to do the normal distribution simulation.

rm(list=ls())

t <- u<- mann<- rep(0, 45)

        Nsimulation<-function(S1,S2,Sds,nSims)
        { 
         set.seed(1)
           for (sim in 1:nSims)
            {
             matrix_t <-matrix(0,nrow=nSims,ncol=3)
             matrix_u<-matrix(0,nrow=nSims,ncol=3)
             matrix_mann <-matrix(0,nrow=nSims,ncol=3)

            #generate random samples from 2 normal distribution
             x<-rnorm(S1,5,Sds)
             y<-rnorm(S2,5,4)
     
            #extract p-value out and store every p-value into matrix
            matrix_t[sim,1]<-t.test(x,y,var.equal=TRUE)$p.value 
            matrix_u[sim,2]<-t.test(x,y,var.equal=FALSE)$p.value
            matrix_mann[sim,3] <-wilcox.test(x,y)$p.value 
             
            print ("sim")
             print(matrix_t)
             print(matrix_u)
            print(matrix_mann) 
            cbind(matrix_t,matrix_u,matrix_mann)
          
            t[sim]<-sum(matrix_t[,1]<0.05)
            u[sim]<-sum(matrix_u[,2]<0.05)
            mann[sim]<-sum(matrix_mann[,3]<0.05)
            print(t)
            print(u)
             }
            return(list(t=t,u=u,mann=mann))
            } 
    
s<-matrix(c(10,10,10,25,25,25,25,50,25,100,50,25,50,100,100,25,100,100),
         nrow=2)
s1<-rep(c(10,10,25,25,25,50,50,100,100),5)
s2<-rep(c(10,25,25,50,100,25,100,25,100),5)
sds<-rep(c(1,1.5,2,2.5,3),9)
all<-cbind(s1,s2,sds)

R<-Nsimulation(S1=s1,S2=s2,Sds=sds,nSims=5)

But the result didnt update itself..Any comment please?

________________________________________
From: R-help <r-help-bounces at r-project.org> on behalf of tan sj <sj_style_1125 at outlook.com>
Sent: Monday, April 25, 2016 1:02 AM
To: r-help at r-project.org
Subject: [R] R: use switch or function in connecting different cases.

HI, I am trying to use switch () function to connect the three distribution (normal ,gamma with equal skewness and gamma with unequal skewness.

But i am losing my ideas since i have

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

several type of setup.

I have finished written 11 codes separately for all of the cases. But i have been told that it can all been done within one code.

can anyone give me a brief idea on it.

I just managed to write till here and it perhaps isnt correct ..

#set up matrix for storing data from simulation

matrix_t  <-matrix(0,nrow=nSims,ncol=3)
matrix_u<-matrix(0,nrow=nSims,ncol=3)
matrix_mann   <-matrix(0,nrow=nSims,ncol=3)

sample_sizes<-

matrix(c(10,10,10,25,25,25,25,50,25,100,50,25,50,100,100,25,100,100),
         nrow=2)
    p1<-p2<-p3<-vector()


    nSims<-10
    alpha<-0.05
    set.seed(1)

        simulation<-function(m,n,sds)
        {
    for (ss in 1:dim(sample_sizes[2])){
      m<-[ss,1]
      n<[ss,2]
    for (sim in 1:nSims)
{
m<-c(10,25,50,100)
n<-c(10,25,50,100)
sds<-c(1,1.5,2,2.5,3)

simulation(m=m,n=n,,sds=sds)

p1 <- t.test(x1,y1,var.equal=TRUE)$p.value
p2<-t.test(x1,y1,var.equal=FALSE)$p.value
p3<-wilcox(x1,y1)$p.value

return (c(p1,p2,p3))

}
}

Thanks in advance. I apologises if this is silly question.

        [[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.
______________________________________________
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.



More information about the R-help mailing list