R [伽马分布尺度参数和t检验]

时间:2016-04-17 18:04:30

标签: r for-loop

我已将矩阵中的所有变量组合在一起,我希望逐行进行模拟。 但我发现代码只适用于九个样本,而不是全部(45)。 我试过,循环是迭代的,但由于这两行,所以会出现问题。

 #minus the population mean to ensure the true of null hypo 
            gamma1<-gamma1-16/9*all_combine1[ss,4]
            gamma2<-gamma2-16/9

任何人都可以帮助....请

 #For gamma disribution with equal skewness 1.5

#to evaluate the same R function on many different sets of data
library(parallel)

nSims<-100   #number of simulation
alpha<-0.05   #significance level

#set nrow =nsims because wan storing every p-value simulated
matrix3_equal  <-matrix(0,nrow=nSims,ncol=3)
matrix4_unequal<-matrix(0,nrow=nSims,ncol=3)
matrix5_mann   <-matrix(0,nrow=nSims,ncol=3)
#set empty vector of length to store p-value 
equal3<-c(rep(0,nrow(all_combine1)))
unequal4<-c(rep(0,nrow(all_combine1)))
mann5<-c(rep(0,nrow(all_combine1)))

#for gamma distribution with equal skewness
# 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 both gamma distribution for equal skewness
shp<-rep(16/9,each=45)

#scale parameter for sample 1 
#scale paramter for sample 2 set as constant 1
scp1<-c(1,1.5,2,2.5,3)
scp1<-rep(scp1,9)   

#create a matrix combining the forty five cases of combination of sample sizes,shape and scale parameter
all_combine1 <- cbind(rep(sample_sizes[1,], 5),rep(sample_sizes[2,],5),shp,scp1)

# name the column samples 1 and 2 and standard deviation
colnames(all_combine1) <- c("m", "n","sp(skewness1.5)","scp1")

##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_combine1))  
  {
    #generate samples from the first column and second column
     m<-all_combine1[ss,1]
     n<-all_combine1[ss,2]   

       for (sim in 1:nSims)
       {
        #generate 2 random samples from gamma distribution with equal skewness
        gamma1<-rgamma(m,16/9,all_combine1[ss,4])
        gamma2<-rgamma(n,16/9,1)

      #minus the population mean to ensure the true of null hypo 
        gamma1<-gamma1-16/9*all_combine1[ss,4]
        gamma2<-gamma2-16/9

        #extract p-value out and store every p-value into matrix
        matrix3_equal[sim,1]<-t.test(gamma1,gamma2,var.equal=TRUE)$p.value    
        matrix4_unequal[sim,2]<-t.test(gamma1,gamma2,var.equal=FALSE)$p.value 
        matrix5_mann[sim,3] <-wilcox.test(gamma1,gamma2)$p.value 
    }
       ##store the result
      equal3[ss]<- sum(matrix3_equal[,1]<=alpha)
      unequal4[ss]<-sum(matrix4_unequal[,2]<=alpha)
      mann5[ss]<- sum(matrix5_mann[,3]<=alpha)

}

这是我的结果。很明显,每次循环都没有成功运行.TT

        m   n sp(skewness1.5) scp1 equal3 unequal4 mann5
 [1,]  10  10        1.777778  1.0      9        9     6
 [2,]  10  25        1.777778  1.5     94       93    95
 [3,]  25  25        1.777778  2.0    100      100   100
 [4,]  25  50        1.777778  2.5    100      100   100
 [5,]  25 100        1.777778  3.0    100      100   100
 [6,]  50  25        1.777778  1.0      3        8     6
 [7,]  50 100        1.777778  1.5    100      100   100
 [8,] 100  25        1.777778  2.0    100      100   100
 [9,] 100 100        1.777778  2.5    100      100   100
[10,]  10  10        1.777778  3.0    100      100   100
[11,]  10  25        1.777778  1.0      3        4     6
[12,]  25  25        1.777778  1.5     99       99   100
[13,]  25  50        1.777778  2.0    100      100   100
[14,]  25 100        1.777778  2.5    100      100   100
[15,]  50  25        1.777778  3.0    100      100   100
[16,]  50 100        1.777778  1.0      3        4     1
[17,] 100  25        1.777778  1.5    100      100   100
[18,] 100 100        1.777778  2.0    100      100   100
[19,]  10  10        1.777778  2.5    100      100   100
[20,]  10  25        1.777778  3.0    100      100   100
[21,]  25  25        1.777778  1.0      4        3     5
[22,]  25  50        1.777778  1.5    100       99   100
[23,]  25 100        1.777778  2.0    100      100   100
[24,]  50  25        1.777778  2.5    100      100   100
[25,]  50 100        1.777778  3.0    100      100   100
[26,] 100  25        1.777778  1.0      8        9    10
[27,] 100 100        1.777778  1.5    100      100   100
[28,]  10  10        1.777778  2.0    100      100   100
[29,]  10  25        1.777778  2.5    100      100   100
[30,]  25  25        1.777778  3.0    100      100   100
[31,]  25  50        1.777778  1.0      2        3     2
[32,]  25 100        1.777778  1.5    100      100   100
[33,]  50  25        1.777778  2.0    100      100   100
[34,]  50 100        1.777778  2.5    100      100   100
[35,] 100  25        1.777778  3.0    100      100   100
[36,] 100 100        1.777778  1.0      7        7     5
[37,]  10  10        1.777778  1.5     88       87    90
[38,]  10  25        1.777778  2.0    100      100   100
[39,]  25  25        1.777778  2.5    100      100   100
[40,]  25  50        1.777778  3.0    100      100   100
[41,]  25 100        1.777778  1.0      7        7     6
[42,]  50  25        1.777778  1.5    100      100   100
[43,]  50 100        1.777778  2.0    100      100   100
[44,] 100  25        1.777778  2.5    100      100   100
[45,] 100 100        1.777778  3.0    100      100   100

2 个答案:

答案 0 :(得分:1)

我相信你的错误在于以下几行:

  ##store the result
  equal[ss]<- mean(matrix2_equal[,1]<=alpha)
  unequal[ss]<-mean(matrix5_unequal[,2]<=alpha)
  mann[ss]<- mean(matrix8_mann[,3]<=alpha)

matrix2_equal [,1]&lt; = alpha将返回true或false值,因此mean(matrix2_equal [,1]&lt; = alpha)基本上从模型中返回True的%。这可能是你想要的:

equal[ss]<- mean(matrix2_equal[matrix2_equal[,1]<=alpha, 1])

仅供参考:此问题与您之前发布的帖子有关:R: coding why show 0.00 in result

答案 1 :(得分:1)

@ Dave2e注释掉这两行会得到以下结果。

 m   n scp equal3 unequal4 mann5
 [1,]  10  10 1.0      8        8     9
 [2,]  10  25 1.5     16       36    23
 [3,]  25  25 2.0     83       82    78
 [4,]  25  50 2.5    100      100   100
 [5,]  25 100 3.0    100      100   100
 [6,]  50  25 1.0      3        5     7
 [7,]  50 100 1.5     82       86    79
 [8,] 100  25 2.0     98       92    91
 [9,] 100 100 2.5    100      100   100
[10,]  10  10 3.0     76       72    77
[11,]  10  25 1.0      1        3     3
[12,]  25  25 1.5     44       42    37
[13,]  25  50 2.0     94       96    92
[14,]  25 100 2.5    100      100   100
[15,]  50  25 3.0    100      100   100
[16,]  50 100 1.0      4        4     3
[17,] 100  25 1.5     72       54    56
[18,] 100 100 2.0    100      100   100
[19,]  10  10 2.5     65       60    57
[20,]  10  25 3.0     90       98    95
[21,]  25  25 1.0      2        2     5
[22,]  25  50 1.5     48       61    50
[23,]  25 100 2.0     95       96    93
[24,]  50  25 2.5    100       99    98
[25,]  50 100 3.0    100      100   100
[26,] 100  25 1.0      5        6     2
[27,] 100 100 1.5    100      100    95
[28,]  10  10 2.0     50       49    49
[29,]  10  25 2.5     79       92    85
[30,]  25  25 3.0     99       99    99
[31,]  25  50 1.0      6        3     6
[32,]  25 100 1.5     58       76    54
[33,]  50  25 2.0     94       91    90
[34,]  50 100 2.5    100      100   100
[35,] 100  25 3.0    100      100   100
[36,] 100 100 1.0      3        3     4
[37,]  10  10 1.5     22       20    13
[38,]  10  25 2.0     45       70    55
[39,]  25  25 2.5     97       97    95
[40,]  25  50 3.0    100      100   100
[41,]  25 100 1.0      5        5     5
[42,]  50  25 1.5     62       48    52
[43,]  50 100 2.0    100      100   100
[44,] 100  25 2.5    100      100   100
[45,] 100 100 3.0    100      100   100