如何针对多个值迭代函数(循环函数)

问题描述 投票:0回答:1

通过运行以下函数,输出将是

library(pmsampsize)

pmsampsize(type = "s", csrsquared = 0.5, parameters = 10, rate = 0.065,
timepoint = 2, meanfup = 2.07)
NB: Assuming 0.05 acceptable difference in apparent & adjusted R-squared 
NB: Assuming 0.05 margin of error in estimation of overall risk at time point = 2  
NB: Events per Predictor Parameter (EPP) assumes overall event rate = 0.065  
 
             Samp_size Shrinkage Parameter CS_Rsq Max_Rsq Nag_Rsq   EPP
Criteria 1        5143     0.900        30  0.051   0.555   0.092 23.07
Criteria 2        1039     0.648        30  0.051   0.555   0.092  4.66
Criteria 3 *      5143     0.900        30  0.051   0.555   0.092 23.07
Final SS          5143     0.900        30  0.051   0.555   0.092 23.07
 
 Minimum sample size required for new model development based on user inputs = 5143, 
 corresponding to 10646 person-time** of follow-up, with 692 outcome events 
 assuming an overall event rate = 0.065 and therefore an EPP = 23.07  

我正在寻找一种方法(循环函数)来针对 csrsquared 和参数的多个值运行以下函数,并计算相应的样本大小和事件数量,然后带来所有结果,包括 R 值、PA 值、最小样本大小和结果事件放入表中。假设

R=seq (from=0, to=0.5, by=0.1)
PA=seq(from=1, to=10, by=1)
pmsampsize(type = "s", csrsquared = R, parameters = PA, rate = 0.065, timepoint = 2, meanfup = 2.07)

结果应该类似于一个表格,其中包括 csrsquared 和参数的不同值,以及它们对样本大小和结果事件的相应估计。

csrsquared (R)     parameters (PA)      Minimum sample size      outcome events
sth like 
R        PA    minimum sample size    outcome events
0        1        
0        2
0        3
0        4

我编写了以下代码,但它不起作用,我需要一些帮助来修改它。

    library(tidyverse)  
      library (pmsampsize) 
      foo = function(R,PA){  
    T=pmsampsize(type = "s", csrsquared = R, parameters = PA, 
 rate = 0.065,timepoint = 2, meanfup = 2.07)
    T$events
    T$sample_size }
    
        df = tibble(  
        R=seq(from = 0, to = 0.5,by=0.1)  
        PA=seq(from = 0, to = 10,by=1) )
loops tidyverse
1个回答
0
投票

看起来你遇到的问题是 tibble 和最终的 map 都会因为回收错误而生气。所以你需要创建一些组合。希望这有帮助!

library(pmsampsize)
library(tidyverse)



foo = function(R,PA){  
 out  = pmsampsize(type = "s", csrsquared = R, parameters = PA, 
               rate = 0.065, timepoint = 2, meanfup = 2.07)
  events = out$events
  sample_size = out$sample_size 
  dt = tibble(events = events,
              sample_size = sample_size)
   return(dt)
}

  
  R=seq(from = 0, to = 0.5,by=0.1)
  PA=seq(from = 0, to = 1,by=1) 

things_to_iterate = expand_grid(R = R, PA = PA)

iterations = map2(things_to_iterate$R, things_to_iterate$PA, \(r, pa) foo(R = r, PA = pa))

创建于 2024-07-02,使用 reprex v2.1.0

© www.soinside.com 2019 - 2024. All rights reserved.