【问题标题】:R: coding why show 0.00 in resultR:编码为什么在结果中显示 0.00
【发布时间】:2016-04-06 12:29:25
【问题描述】:

我的模拟目的是在多种因素组合下评估测试的类型 1 错误率。

  1. 样本大小-(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 检验和 welch t 检验和 mann whitney 检验。我尝试通过使用上述因素组合来修改代码。

########################################
    #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 的新手,现在我已经完成了正态分布的代码,并且必须通过使用 if else 来添加两个关于伽马分布分布的模拟...谁能提供一些建议如何从正常分布进行更改.到伽马分布?我现在卡在这部分...

帮助!! 上面的代码多次给了我结果 0.00,我已经检查了很多次,但我没有发现任何错误。 请

【问题讨论】:

  • 什么是nsds,以及您是如何创建t_equalt_unequalmann 的。它们似乎是向量,您在每次模拟时都在写入要存储它们的矩阵。
  • 对不起,nsds是标准差比的长度。并且 t_equal ,t_unequal 和 mann 是数据被模拟并存储到其中的向量...
  • 我刚刚将其余代码编辑到我的问题中......感谢您的评论......

标签: r if-statement for-loop gamma-distribution


【解决方案1】:

这是我目前的编码..

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

【讨论】:

    【解决方案2】:

    我编辑了您的代码以测试类型 1 错误。我宁愿将所有这些组合放入一个矩阵中,并对所述矩阵的每一行进行模拟,而不是为每个因素组合使用多个嵌套的 for 循环。这使得绘制结果变得更加容易。为了加快计算速度,请注意我做的模拟要少得多(我更改了nSims),你可能想把它改回来。最后,您可以将三个结果矩阵组合到不同的因素组合中。

    我不知道您对 (**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
    

    【讨论】:

    • 非常感谢,虽然我不是很明白,但真的很感谢你的评论..我只是发现我对嵌套 for 循环的部分不是很了解
    • 嗨,我能问一个简单的问题吗,因为我刚刚注意到这一点,为什么用于存储 p 值的矩阵的列数设置为模拟次数而不是计算次数?我想知道这部分......
    • 对于每个独特的因素组合(例如sample_sizessds),您需要进行大量模拟。要测试类型 1 错误率,您需要查看有多少模拟的 p 值小于您指定的 alpha。因此,您需要存储每个模拟的每个 p 值。 store_sim 矩阵有 3 列(每个测试一个)和 nSims 行(每个模拟一个)。
    • 好吧,我已经明白了...对于 equal[ss]
    • 是的。该部分用于计算类型 1 错误率。第一个循环从all_combn 中获取一行,然后我们使用该行进行nSims 模拟,并将它们存储在store_sim 中。在这些模拟之后,我们使用您提到的行计算错误率并将它们存储到向量中。完成后,从all_combn 中获取下一行数据,我们重复模拟,直到计算出all_combn 中每一行的错误率。
    猜你喜欢
    • 2023-01-17
    • 1970-01-01
    • 2021-12-17
    • 2021-05-28
    • 2018-04-25
    • 2019-03-07
    • 1970-01-01
    • 2017-07-16
    • 1970-01-01
    相关资源
    最近更新 更多