loopstidyverse

How to iterate a function for multiple values (Loop function)?


By running the following function, the output would be:

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  

I am looking for a way (loop function) to run the following function for multiple values of csrsquared and parameters and calcultes the corresponding sample size and number of events and then bring all results including the value of R, value of PA, minimum sample size, and outcome events into a table. Suppose

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)

The outcome should be something akin to a table including different values of csrsquared and parameters, and their corresponding estimation for sample size and outcome events.

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

I made the following code but it doesnot work and I need some help to modify it.

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

Solution

  • It looks like something that you are running into is that both tibble and eventually map will get mad about recycling errors. So you need to create some combos. Hopefully this helps!

    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,
                  R = R,
                PA = PA)
       return(dt)
    }
    
      
      R=seq(from = 0, to = 0.5,by=0.1)
      PA=seq(from = 0, to = 10, 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))
    

    Created on 2024-07-02 with reprex v2.1.0