【问题标题】:How to count number of significant p-values in a simulation?如何计算模拟中重要 p 值的数量?
【发布时间】:2018-11-24 19:50:51
【问题描述】:

假设我进行了一项临床试验,其中有 32 只小鼠感染了某种疾病。我对其中的 16 个进行了治疗,其余 16 个作为对照。在进行实验之前,我正在运行一个模拟。我将从均匀(0,1)分布中得出 32 个观察值。我想计算模拟结束后 p 值

nSims <- 10000 #number of simulated experiments
p <-numeric(nSims) #set up empty container for all simulated p-values
sig<-0
for(i in 1:nSims){ #for each simulated experiment

   #generating 32 observations total from uniform(0,1) distribution
   control.year1 <- runif(16, min = 0, max = 1)
   treat.year1 <- runif(16, min = 0, max = 1)

   #Creating dichotomous variable for those get better/don't get better
   control.respond <- ifelse(control.year1<=0.05,1,0)
   treat.respond <- ifelse(treat.year1<=0.30,1,0)

   #perform t-test
   z <- t.test(control.respond,treat.respond) 
   p[i]<-z$p.value #get the p-value and store it

   # want to count number of significant p-values - not sure how to do it
   significance <- ifelse(z$p.value<= 0.01,sum(sig, 1),0)
}

【问题讨论】:

  • 您应该能够在最后使用sum(p &lt; 0.01) 在for 循环之外计算重要结果的数量。但是您正在对二元结果进行 t 检验?我不确定这是否有意义。
  • 你不应该能够通过分析来解决这个问题吗?
  • 您可能对 R 中的一些内置功率计算器感兴趣 (apropos("^power"))
  • @BenBolker 谢谢,这不是最终的模拟 - 我正在模拟一个为期五年的实验,如果治疗有效,它可能会提前停止,我只是想在实验的第一年获得循环对。
  • @Marius 感谢您的提醒,您说得对,我将使用单边费希尔精确检验。

标签: r loops simulation


【解决方案1】:

此解决方案不是最优雅的,但使用magrittrdplyr 进行数据处理。首先,我创建了一个矩阵来保存您的模拟数据:

library(magrittr)
library(dplyr)

n <- 100
control.years <- as.data.frame(matrix(runif(16*n, min=0, max=1),ncol=16))
treat.years <- as.data.frame(matrix(runif(16*n, min=0, max=1),ncol=16))

然后我创建了一个数据结构来捕获所有 t 检验的 p 值:

for (i in 1:n) { p[i] <- t.test(control.years[i,],treat.years[i,])$p.value }

你可以过滤掉你喜欢的范围内的p值:

> as.data.frame(p) %>% filter(p<0.05)
       p
1 0.03173299
2 0.01652114
3 0.00471807

或者您可以创建一个新变量来告诉您是否重要:

> as.data.frame(p) %>% mutate(sig=ifelse(p<0.05,1,0)) 
             p sig
1   0.65233254   0
2   0.50731231   0
3   0.11657045   0
...
29  0.03173299   1

或者您可以找出有多少重要的 p 值:

> z <- as.data.frame(p) %>% mutate(sig=ifelse(p<0.05,1,0)) 
> table(z$sig)

 0  1 
97  3 

【讨论】:

  • 谢谢,这正是我的想法,感谢您提供不同的选择。
猜你喜欢
  • 1970-01-01
  • 2017-12-20
  • 1970-01-01
  • 2019-04-05
  • 2014-04-13
  • 2017-11-30
  • 2018-09-01
  • 2020-10-15
  • 2021-08-03
相关资源
最近更新 更多