2016-04-06 6 views
0

このシミュレーションの目的は、いくつかの要因を組み合わせてテストのタイプ1エラー率を評価することです。R:なぜ結果が0.00で表示されるのですか?

  1. サンプルsizes-(10,10)、(10,25)、(25,25)、(25,50)、(25,100)、50,25)、(50,100)、(100、 25)、(100,100)

  2. 標準偏差の比 - (1.00、1.50、2.00、2.50、3.00および3.50)不等歪度と等しい歪度を有するガンマ分布の

  3. 分布

含まれる2つのサンプル試験は、プールされた分散t検定およびウェルチtテストとマンホイットニーテスト。上記の要素の組み合わせを使用してコードを修正しようとしました。どのように

######################################## 
    #for normal distribution setup 

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

#create vector to combine all std deviations 
sds<-matrix(c(4,4,6,4,8,4,10,4,12,4,14,4),nrow=2) 

sd1<-c(4,6,8,10,12) 
sd2<-c(4,4,4,4,4) 
sds2<-rep(sd2,each=9) 

##(use expand.grid)to create a data frame from combination of data 
ss_sds1<- expand.grid(sample_sizes[2,], sd1) 

#create a matrix combining the fifty four cases of combination of ss and sds 
all_combine <- cbind(rep(sample_sizes[1,], 5), ss_sds1,sds2) 

# name the column by sample samples 1 and 2 and standard deviation 
colnames(all_combine) <- c("m", "n", "sds1","sds2") 

#number of simulations 
nSims<-10000 

#set significance level,alpha for the whole simulation 
alpha<-0.05  

#set up matrix for storing data from simulation 
#set nrow =nsims because wan storing every p-value simulated 
matrix1_equal <-matrix(0,nrow=nSims,ncol=9) 
matrix4_unequal<-matrix(0,nrow=nSims,ncol=9) 
matrix7_mann <-matrix(0,nrow=nSims,ncol=9) 

#set up vector for storing data from the three tests (nrow for all_combine=45) 
equal1 <- unequal4<- mann7 <- rep(0, nrow(all_combine)) 

    # this loop steps through the all_combine matrix 
    for(ss in 1:nrow(all_combine)) 
    { 
    #generate samples from the first column and second column 
    m<-all_combine[ss,1] 
    n<-all_combine[ss,2] 

     for (sim in 1:nSims) 
     { 
     #generate random samples from 2 normal distribution 
     x<-rnorm(m,5,all_combine[ss,3]) 
     y<-rnorm(n,5,4) 

     #extract p-value out and store every p-value into matrix 
     matrix1_equal[sim,1]<-t.test(x,y,var.equal=TRUE)$p.value  
     matrix4_unequal[sim,4]<-t.test(x,y,var.equal=FALSE)$p.value 
     matrix7_mann[sim,7] <-wilcox.test(x,y)$p.value 
     } 

    ##store the result 
    equal1[ss]<- mean(matrix1_equal[,1]<=alpha) 
    unequal4[ss]<-mean(matrix4_unequal[,4]<=alpha) 
    mann7[ss]<- mean(matrix7_mann[,7]<=alpha) 
    } 

    # combine results 
    nresult <- cbind(all_combine, equal1, unequal4, mann7) 

    save.image(file="normal.data") 

私はRで新しいですが、今私は正規分布でコードを完了し、もしそうでなければ使用することにより、ガンマ分布の分布上の2回のシミュレーションに追加する必要がいる...誰もがplsはいくつかのアドバイスを与えることができます通常のdistrから変更します。ガンマdistr?私は今この部分にこだわっています...

HELP !! 上記のコードは私に何回も0.00の結果を与えました。何度もチェックしていますが、間違いはありませんでした。 Please

+0

、そしてどのようにあなたが' t_equal'、 't_unequal'、と' mann'を作成しました。それらはベクトルであるように見えますが、すべてのシミュレーションでそれらを保存したい行列を上書きしています。 –

+0

申し訳ありませんが、nsdsは標準偏差の長さです。そして、t_equal、t_unequal、mannは、データがシミュレートされ、それらに格納されているベクトルです... –

+0

私はちょうど私の質問にコードの残りの部分を編集しました....コメントのおかげで... –

答えて

0

タイプ1のエラーをテストするコードを編集しました。各要素の組み合わせごとに複数のforループをネストするのではなく、これらの組み合わせのすべてを単一の行列に入れて、その行列の各行を使ってシミュレーションを行うことを好みます。これにより、結果をプロットするのがずっと簡単になります。計算を高速化するために、私はずっと少ない数のsimualation(私はnSimsを変更しました)を行ったことに注意してください。最後に、3つの結果マトリックスをさまざまな要因の組み合わせに組み合わせることができます。

あなたは(**ss-1)*nsds+sim**で何をしていたのか分からず、それを変更することを選択しました。

#for normal distribution setup 

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

# get all combinations with one row of the sample_sizes matrix 
all_combn <- expand.grid(sample_sizes[2,], sds) 

# tack on the first row 

all_combn <- cbind(rep(sample_sizes[1,], 6), all_combn) 
# change the column names 
colnames(all_combn) <- c("ss1", "ss2", "sds") 


# to ensure the reproducity of the result 
#(here we declare the random seed generator) 
set.seed(1) 

#number of simulations 
nSims<-500  

# to store your simulations for the three tests 
store_sim <- matrix(0, nrow = nSims, ncol = 3) 
#set significance level,alpha for the whole simulatio 
alpha<-0.05  


#set up vector for storing data from the three tests 

equal <- unequal<- mann <- rep(0, nrow(all_combn)) 


# outer loop run nsims for every combinations of std deviations and ss 


    # this loop steps through the all_combn matrix 
    for(ss in 1:nrow(all_combn)) 
    { 
    m<-all_combn[ss,1] 
    n<-all_combn[ss,2] 

     for (sim in 1:nSims) 
     { 
     #generate random samples from 2 normal distribution 
     x<-rnorm(m,5,all_combn[ss,3]) 
     y<-rnorm(n,5,4) 

     #extract p-value out and store it in vectors 
     store_sim[sim,1]<-t.test(x,y,var.equal=TRUE)$p.value  
     store_sim[sim,2]<-t.test(x,y,var.equal=FALSE)$p.value 
     store_sim[sim,3] <-wilcox.test(x,y)$p.value 

    } 

    ##store the result into matrix defined before 
    equal[ss]<- sum(store_sim[,1]<alpha)/nSims 
    unequal[ss]<- sum(store_sim[,2]<alpha)/nSims 
    mann[ss]<- sum(store_sim[,2]<alpha)/nSims 
    } 


# combine results 

answer <- cbind(all_combn, equal, unequal, mann) 

head(answer) 

    ss1 ss2 sds equal unequal mann 
1 10 10 4 0.070 0.062 0.062 
2 10 25 4 0.046 0.048 0.048 
3 25 25 4 0.048 0.048 0.048 
4 25 50 4 0.038 0.048 0.048 
5 25 100 4 0.058 0.054 0.054 
6 50 25 4 0.048 0.054 0.054 
+0

ありがとう私は非常に理解していないが、あなたのコメントのために本当にありがとう。私はちょうど私が実際に非常にネストされたforループの部分について理解していないことを知っている –

+0

こんにちは、私はちょうどこれに気づいたので、計算の代わりにシミュレーションの数に設定されたp値を格納するための行列の列の?私はこの部分について疑問に思っています.... –

+0

あなたの要因(例えば、 'sample_sizes'と' sds')のすべてのユニークな組み合わせについて、いくつかのシミュレーションをしたいと思うでしょう。タイプ1のエラーレートをテストするには、指定されたアルファよりp値が小さいシミュレーションの数を確認する必要があります。そのため、各シミュレーションの各p値を保存する必要があります。 'store_sim'マトリックスには、3つの列(各テストごとに1つ)と' nSims'行(各シミュレーションごとに1つ)があります。 –

1

これは私の現在のコード.. `nsds`は何

######################################## 
    #for normal distribution setup 

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

#create vector to combine all std deviations 
sds<-matrix(c(4,4,6,4,8,4,10,4,12,4,14,4),nrow=2) 

sd1<-c(4,6,8,10,12) 
sd2<-c(4,4,4,4,4) 
sds2<-rep(sd2,each=9) 

##(use expand.grid)to create a data frame from combination of data 
ss_sds1<- expand.grid(sample_sizes[2,], sd1) 

#create a matrix combining the fifty four cases of combination of ss and sds 
all_combine <- cbind(rep(sample_sizes[1,], 5), ss_sds1,sds2) 

# name the column by sample samples 1 and 2 and standard deviation 
colnames(all_combine) <- c("m", "n", "sds1","sds2") 

#number of simulations 
nSims<-10000 

#set significance level,alpha for the whole simulation 
alpha<-0.05  

#set up matrix for storing data from simulation 
#set nrow =nsims because wan storing every p-value simulated 
matrix1_equal <-matrix(0,nrow=nSims,ncol=9) 
matrix4_unequal<-matrix(0,nrow=nSims,ncol=9) 
matrix7_mann <-matrix(0,nrow=nSims,ncol=9) 

#set up vector for storing data from the three tests (nrow for all_combine=45) 
equal1 <- unequal4<- mann7 <- rep(0, nrow(all_combine)) 

    # this loop steps through the all_combine matrix 
    for(ss in 1:nrow(all_combine)) 
    { 
    #generate samples from the first column and second column 
    m<-all_combine[ss,1] 
    n<-all_combine[ss,2] 

     for (sim in 1:nSims) 
     { 
     #generate random samples from 2 normal distribution 
     x<-rnorm(m,5,all_combine[ss,3]) 
     y<-rnorm(n,5,4) 

     #extract p-value out and store every p-value into matrix 
     matrix1_equal[sim,1]<-t.test(x,y,var.equal=TRUE)$p.value  
     matrix4_unequal[sim,4]<-t.test(x,y,var.equal=FALSE)$p.value 
     matrix7_mann[sim,7] <-wilcox.test(x,y)$p.value 
     } 

    ##store the result 
    equal1[ss]<- mean(matrix1_equal[,1]<=alpha) 
    unequal4[ss]<-mean(matrix4_unequal[,4]<=alpha) 
    mann7[ss]<- mean(matrix7_mann[,7]<=alpha) 
    } 

    # combine results 
    nresult <- cbind(all_combine, equal1, unequal4, mann7) 

    save.image(file="normal.data") 
関連する問題