1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| f_surv_logrank_simulation_Group = function(N, Median_Survival_Time, Lost, Duration_Accrual_Time, Duration_Total_Time){ time = stats::rexp(N, rate = log(2) / Median_Survival_Time) status = rep(1,N) EnrollT = stats::runif(N, min = 0, max = Duration_Accrual_Time) calender_time = time + EnrollT idx = calender_time > Duration_Total_Time status[idx] = 0 time[idx] = Duration_Total_Time - EnrollT[idx] loss = stats::rexp(N, rate = Lost) idx = loss < time status[idx] = 0 time[idx] = loss[idx] return(list(time = time, status = status)) } f_surv_logrank_simulation_Power = function(n_C, Median_Survival_Time_C, Lost_C, n_T, Median_Survival_Time_T, Lost_T, Duration_Accrual_Time, Duration_Total_Time, Simulation_Cycle, Alpha){ df = data.frame(group = factor(c(rep('Control',n_C), rep('Treatment',n_T))), time = rep(0,n_C+n_T), status = rep(0,n_C+n_T)) sum = 0 for (i in 1:Simulation_Cycle) { C = f_surv_logrank_simulation_Group(n_C, Median_Survival_Time_C, Lost_C, Duration_Accrual_Time, Duration_Total_Time) T = f_surv_logrank_simulation_Group(n_T, Median_Survival_Time_T, Lost_T, Duration_Accrual_Time, Duration_Total_Time) df$time = c(C$time, T$time) df$status = c(C$status, T$status) if(f_surv_logrank(df)$pv < Alpha){ sum = sum + 1 } } return(sum/Simulation_Cycle) } f_surv_logrank_simulation_Sample_Size = function(n_C_min, n_C_max, Median_Survival_Time_C, Lost_C, TvsC, Median_Survival_Time_T, Lost_T, Duration_Accrual_Time, Duration_Total_Time, Simulation_Cycle, Alpha, Power, err=0.01){ mid = floor((n_C_min + n_C_max) / 2) while (n_C_min < n_C_max) { mid = floor((n_C_min + n_C_max) / 2) simulation_Power = f_surv_logrank_simulation_Power(mid, Median_Survival_Time_C, Lost_C, as.integer(mid * TvsC), Median_Survival_Time_T, Lost_T, Duration_Accrual_Time, Duration_Total_Time, Simulation_Cycle, Alpha) print(paste("mid:", mid, "simulation_Power:", simulation_Power)) if (abs(simulation_Power - Power) < err) { return(mid) }else if(simulation_Power < Power) { n_C_min = mid + 1 }else { n_C_max = mid - 1 } } return(mid) }
|