-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuture_analysis_old.R
148 lines (126 loc) · 4.4 KB
/
future_analysis_old.R
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
## @knitr future_analysis
# For each row of pred_Data
future_analysis <- function(prediction, newdata, olddata,
arms=NULL,
n_total=nrow(newdata)+nrow(olddata),
parallel=TRUE){
# add_sim_stats()
require(doParallel)
require(foreach)
#prediction <- pred_data[1,]# FUN=future_analysis,
#newdata=df_new_long
#o#lddata=df
df_new <- add_sim_status(newdata, prediction,rx,site)
# add_censor_sample()
df_new <- add_censor_sample(df_new$short, censor_dist)
# combine with the original data
common_names <- intersect(names(df_new), names(olddata))
df_all <- rbind(olddata %>% as.data.frame %>% select(all_of(common_names)),
df_new %>% as.data.frame %>% select(all_of(common_names))
)
n_arms <- length(arms)
if(!is.null(arms)){
df_all <- subset(df_all, rx %in% arms)
#n_arms <- nlevels(olddata$rx)
}
if( min(n_total)<nrow(olddata)*n_arms/nlevels(olddata$rx)){
stop("you are throwing out the existing data!")
}
if(!parallel){
ans <- array(NA, c(length(n_total), 2*(n_arms-1)))
for( row in 1:length(n_total)){
df_subset <- df_all[1:n_total[row],]
df_subset$rx <- droplevels(df_subset$rx)
# consider which arms to use, how many new patients.
# calculate coefs and SE using coxph - but leave open to stan_glm()...
analysis <- coxph(Surv(time,status)~rx+frailty.gaussian(site), data=df_subset)
# loop across rows and then calculate OCs: power, bias, SE, coverage...
se <- analysis$var %>% diag %>% sqrt
ans[row,] <- c(analysis$coefficients, se)
}
}
if(parallel){
#cl<-makeCluster(2)
#registerDoSNOW(cl)
ans <- foreach( row = 1:length(n_total), .packages = c("survival","magrittr"),
.combine=rbind, .export=c("df_all","n_total"), .inorder=TRUE
) %dopar% {
df_subset <- df_all[1:n_total[row],]
df_subset$rx <- droplevels(df_subset$rx)
# consider which arms to use, how many new patients.
# calculate coefs and SE using coxph - but leave open to stan_glm()...
analysis <- coxph(Surv(time,status)~rx+frailty.gaussian(site), data=df_subset)
# loop across rows and then calculate OCs: power, bias, SE, coverage...
se <- analysis$var %>% diag %>% sqrt
c(analysis$coefficients, se)
}
#stopCluster(cl)
}
ans
}
combinations <- function(x){
m <- length(x)
n <- 2^m
ans <- vector("list",n-1)
for(i in 1:(n-1)){
index <- binary(i)
pad <- m-length(index)
index <- c(as.logical(index),rep(FALSE,pad))
ans[[i]] <- x[index]
}
ans
}
#combinations(c("A","B","C"))
binary <- function(x){
if(x<2){
return(x)
}else{
alpha <- x %% 2
x <- x %/% 2
return( c(alpha, Recall(x)))
}
}
#as.logical(binary(4))
power <- function(arms,n_total){
require(doParallel)
require(foreach)
# apply doesn't respect the array structure returned by future_analysis()
#oc_data <- array(NA, dim = c(dim(pred_data)[1], length(n_total), 2*(length(arms)-1)))
# for(row in 1:dim(oc_data)[1])
oc_data_list <- foreach( row = 1:dim(pred_data)[1],
.export=c("future_analysis","pred_data","df_new_long",
#"arms","n_total",
"df","add_sim_status","add_censor_sample",
"censor_dist"),
.inorder=FALSE,
.packages=c("survival","magrittr")) %dopar%
{
future_analysis(
pred_data[row,],newdata=df_new_long, olddata=df,
arms=arms, n_total=n_total
)
}
#stopCluster(cl)
#oc_data <- simplify2array(oc_data_list)
# for(row in 1:dim(oc_data)[1]){
# oc_data[row, ,] <- oc_list[[row]]
#
# # future_analysis(
# # pred_data[row,],newdata=df_new_long, olddata=df,
# # arms=arms, n_total=n_total
# # )
# }
#
if( length(arms)==3){
z1 <- sapply(oc_data_list, FUN = function(x){x[,1]/x[,3]}, simplify="array") #oc_data[,1,]/oc_data[,3,]
z2 <- sapply(oc_data_list, FUN = function(x){x[,2]/x[,4]}, simplify="array")#oc_data[,2,]/oc_data[,4,]
} else{
z1 <- z2 <- sapply(oc_data_list, FUN = function(x){x[,1]/x[,2]}, simplify="array")#oc_data[,1,]/oc_data[,2,]
}
#1-side alpah/2 test
any_result <- (pnorm(z1)<0.025) |(pnorm(z2)<0.025)
power <- apply(any_result,1, mean)
#apply(pnorm(z1)<0.025,2, mean)
#apply(pnorm(z2)<0.025,2, mean)
return(power)
}