0

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) )
2
  • Welcome to Stack Overflow! Could you please specify what you mean by "it did not work"? Do you get an error, are the results incorrect ...? Side note: I removed all the italic formatting, as they don't increase readability and, given almost everything was italic, it did not seem to emphasise anything. Check the guide on formatting for more info. Commented Jul 2, 2024 at 7:23
  • @ Adriaan, Hi, Thank you so much for your time. I made a code above but it doenot work and I need some help to modify it. Commented Jul 2, 2024 at 7:47

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

2 Comments

thank you so much for your time. It seems I made a mistake in code as the number of PA should be PA=seq(from = 0, to = 10,by=1) . Could you please let me know how I can have a final table including 4 coloums to see the values of R; PA; events and sample size?
I have updated the code a bit! But all you are doing is just modifying what goes in the tibble and the PA vector.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.