-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFCT-gg.R
384 lines (332 loc) · 16 KB
/
FCT-gg.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
### FCT-gg.R ---
##----------------------------------------------------------------------
## Author: Brice Ozenne
## Created: mar 4 2020 (11:05)
## Version:
## Last-Updated: Apr 12 2021 (10:40)
## By: Brice Ozenne
## Update #: 299
##----------------------------------------------------------------------
##
### Commentary:
##
### Change Log:
##----------------------------------------------------------------------
##
### Code:
## * ggTiming
ggTiming <- function(data, type.data = "raw", file = NULL, plot = TRUE, txt.size = 20){
require(ggpubr)
type.data <- match.arg(type.data, c("raw","processed"))
if(type.data == "raw"){
Ufile <- unique(data$iFile)
if(is.null(file)){
file <- 1:length(Ufile)
}else if(any(file %in% 1:length(Ufile) == FALSE)){
stop("unknown file selected \n")
}
if("threshold" %in% names(data) && length(unique(data$threshold))>1){
stop("Cannot handle multple thresholds")
}
if("endpoint" %in% names(data) && length(unique(data$endpoint))>1){
stop("Cannot handle multple endpoints")
}
dtW.gg <- data[method %in% c("Gehan","Peron") & Hprojection == 1 & iFile %in% Ufile[file],
.SD, .SDcols = c("n","method","timeAll","timeEstimate")]
dtW.gg[, time := timeAll/timeEstimate]
dtL1.gg <- dtW.gg[, .(n,method,time)]
dtL2.gg <- melt(dtW.gg,
id.vars = c("n","method"),
measure.vars = c("timeAll","timeEstimate"),
value.name = "time",
variable.name = "type")
dtL2.gg[, type := factor(type, levels = c("timeEstimate","timeAll"),
labels = c("estimate","estimate+se"))]
dtL.gg <- rbind(cbind(dtL1.gg[,.(n,method)], type = "relative", dtL1.gg[,.(time)]),
cbind(dtL2.gg))
dtL.gg[, n := factor(n, levels = sort(unique(n)))]
dtL.gg[, scoring.rule := factor(method, levels = c("Gehan","Peron"),
labels = c("Gehan's scoring rule","Peron's scoring rule"))]
dtL.gg[, type := factor(type,
levels = c("estimate","estimate+se","relative"),
labels = c("estimate","estimate with s.e.","ratio"))]
}else if(type.data == "processed"){
dtL.gg <- data
}
gg1 <- ggplot(data = dtL.gg[type == levels(type)[1]], mapping = aes(x = n, y = time))
gg1 <- gg1 + geom_boxplot()
gg1 <- gg1 + facet_grid(type~scoring.rule)
gg1 <- gg1 + xlab("") + ylab("time (s)") + labs(color="")
gg1 <- gg1 + theme(legend.position="bottom",
text = element_text(size=txt.size),
plot.margin = unit(c(0.1,0.1,0,0.35), "cm"))
gg2 <- ggplot(data = dtL.gg[type == levels(type)[2]], mapping = aes(x = n, y = time))
gg2 <- gg2 + geom_boxplot()
gg2 <- gg2 + facet_grid(type~scoring.rule)
gg2 <- gg2 + xlab("") + ylab("time (s)") + labs(color="")
gg2 <- gg2 + theme(legend.position="bottom",
text = element_text(size=txt.size),
plot.margin = unit(c(-0.1,0.1,0.2,0), "cm"))
gg3 <- ggplot(data = dtL.gg[type == levels(type)[3]], mapping = aes(x = n, y = time))
gg3 <- gg3 + geom_hline(yintercept = 1, color = "red", size = 1.5)
gg3 <- gg3 + geom_boxplot()
gg3 <- gg3 + facet_grid(type~scoring.rule)
gg3 <- gg3 + xlab("sample size in each group") + ylab("relative time") + labs(color="")
gg3 <- gg3 + theme(legend.position="bottom",
text = element_text(size=txt.size),
plot.margin = unit(c(-0.1,0.1,0.2,0.5), "cm"))
if(file.exists("tempo.pdf")){
stop("Cannot run the function as a file name tempo.pdf is in the working directory. \n")
}
pdf("tempo.pdf")
gg <- try(ggarrange(plotlist = list(gg1, gg2, gg3), ncol=1, nrow=3, common.legend = TRUE, legend="bottom"), silent = TRUE)
dev.off()
file.remove("tempo.pdf")
if(plot){
print(gg)
}
return(invisible(list(plot = gg,
data = dtL.gg)))
}
## * ggBias
ggBias <- function(data, type.data = "raw", file = NULL, plot = TRUE, expected = NULL, txt.size = 20){
type.data <- match.arg(type.data, c("raw","processed"))
if(type.data == "raw"){
Ufile <- unique(data$iFile)
if(is.null(file)){
file <- 1:length(Ufile)
}else if(any(file %in% 1:length(Ufile) == FALSE)){
stop("unknown file selected \n")
}
if("threshold" %in% names(data) && length(unique(data$threshold))>1){
stop("Cannot handle multple thresholds")
}
if("endpoint" %in% names(data) && length(unique(data$endpoint))>1){
stop("Cannot handle multple endpoints")
}
dtW.gg <- data[Hprojection == 1 & iFile %in% Ufile[file],.(n,method,estimate)]
if(is.numeric(dtW.gg$n)){
max.n <- max(dtW.gg$n)
}else if(is.factor(dtW.gg$n)){
max.n <- tail(levels(dtW.gg$n),1)
}
dtW.gg[, expected := mean(.SD[n==max.n & method == "GS",estimate])]
dtW.gg[, scoring.rule := factor(method, levels = c("GS","Gehan","Peron"), labels = c("no censoring","Gehan's scoring rule","Peron's scoring rule"))]
dtW.gg[, n := factor(n, levels = sort(unique(n)))]
dtW.gg[, sample.size := factor(paste0("sample size = ",n), paste0("sample size = ",unique(n)))]
}else if(type.data == "processed"){
dtW.gg <- data
}
gg <- ggplot(dtW.gg, aes(y = estimate - expected))
gg <- gg + facet_wrap(~sample.size)
gg <- gg + geom_hline(yintercept = 0, color = "red", size = 1.5)
gg <- gg + geom_boxplot(aes(color = scoring.rule))#, outlier.shape = NA)
gg <- gg + xlab("sample size") + ylab("bias") + labs(color="")
gg <- gg + theme(legend.position="bottom",
text = element_text(size=txt.size),
axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank())
if(plot){
print(gg)
}
return(invisible(list(plot = gg,
data = dtW.gg)))
}
## * ggSe
ggSe <- function(data, type.data = "raw", file = NULL, plot = TRUE, expected = NULL, txt.size = 20){
type.data <- match.arg(type.data, c("raw","processed"))
if(type.data == "raw"){
Ufile <- unique(data$iFile)
if(is.null(file)){
file <- 1:length(Ufile)
}else if(any(file %in% 1:length(Ufile) == FALSE)){
stop("unknown file selected \n")
}
if("threshold" %in% names(data) && length(unique(data$threshold))>1){
stop("Cannot handle multple thresholds")
}
if("endpoint" %in% names(data) && length(unique(data$endpoint))>1){
stop("Cannot handle multple endpoints")
}
dtW.gg <- data[iFile %in% Ufile[file],.(n,method,Hprojection,se,estimate)]
dtS.gg <- dtW.gg[,.(rep = .N, model = mean(se), empirical = sd(estimate)),by = c("n","method","Hprojection")]
dtS.gg[, n := factor(n, levels = sort(unique(n)))]
dtS.gg[, method2 := paste0(method," H",Hprojection)]
dtS.gg[, Hprojection := factor(Hprojection, levels = 1:2, labels = c("1st order H-projection","2nd order H-projection"))]
dtS.gg[, scoring.rule := factor(method, levels = c("GS","Gehan","Peron"), labels = c("no censoring","Gehan's scoring rule","Peron's scoring rule"))]
}else if(type.data == "processed"){
dtS.gg <- data
}
gg <- ggplot(dtS.gg, aes(x = empirical, y = model, group = Hprojection, color = n))
gg <- gg + geom_abline(slope = 1)
gg <- gg + geom_point(size = 2) + geom_line(size = 1.25)
gg <- gg + facet_grid(Hprojection~scoring.rule)
gg <- gg + xlab("empirical standard error") + ylab("average estimated standard error")
gg <- gg + labs(colour = "sample size (per group)") + theme(legend.position="bottom", text = element_text(size=txt.size))
if(plot){
print(gg)
}
return(invisible(list(plot = gg,
data = dtS.gg)))
}
## * ggCoverage
ggCoverage <- function(data, by, type.data = "raw", file = NULL, plot = TRUE, expected = NULL, txt.size = 20){
require(ggthemes)
type.data <- match.arg(type.data, c("raw","processed"))
if(type.data == "raw"){
Ufile <- unique(data$iFile)
if(is.null(file)){
file <- 1:length(Ufile)
}else if(any(file %in% 1:length(Ufile) == FALSE)){
stop("unknown file selected \n")
}
keep.cols <- c("n", "method", "Hprojection", "threshold", "endpoint", "estimate", "lower.ci", "upper.ci")
keep.cols <- keep.cols[keep.cols %in% names(data)]
dtW.gg <- data[iFile %in% Ufile[file],.SD,.SDcols = keep.cols]
if(is.null(expected)){
if(is.numeric(dtW.gg$n)){
max.n <- max(dtW.gg$n)
}else if(is.factor(dtW.gg$n)){
max.n <- tail(levels(dtW.gg$n),1)
}
dtW.gg[, expected := mean(.SD[n==max.n & method == "GS",estimate]), by = by]
}else{
Uby <- unique(dtW.gg[[by]])
n.by <- length(Uby)
for(iby in 1:n.by){
dtW.gg[dtW.gg[[by]] == Uby[iby], expected := expected[iby]]
}
}
dtW.gg[, coverage := (lower.ci < expected)*(upper.ci > expected)]
dtS.gg <- dtW.gg[, .(rep = .N, coverage = mean(coverage, na.rm=TRUE)), by = c("n","method","Hprojection",by)]
dtS.gg[, n := factor(n, levels = sort(unique(n)))]
dtS.gg[, method2 := paste0(method," H",Hprojection)]
dtS.gg[, scoring.rule := factor(method, levels = c("GS","Gehan","Peron"), labels = c("no censoring","Gehan's scoring rule","Peron's scoring rule"))]
if(identical(by,"Hprojection")){
dtS.gg[, Hprojection := factor(Hprojection, levels = 1:2, labels = c("1st order H-projection","2nd order H-projection"))]
}else if(identical(by,"threshold")){
dtS.gg[, threshold := paste0("\u03C4=", threshold)]
}else if(identical(by,"endpoint")){
}
}else if(type.data == "processed"){
dtS.gg <- data
}
test.by <- sapply(c("threshold","endpoint","Hprojection"), function(iBy){
length(unique(dtS.gg[[iBy]]))>1
})
if(sum(test.by)>1){
stop("Only one of \"threshold\", \"endpoint\", and \"Hprojection\" can vary \n")
}
if(sum(test.by)>0){
by <- names(test.by)[which(test.by)]
}else{
by <- NULL
}
gg <- ggplot(dtS.gg, aes(x = n, y = coverage, group = scoring.rule, color = scoring.rule))
gg <- gg + geom_hline(yintercept = 0.95, color = "red", size = 1.5)
gg <- gg + geom_point(size = 2) + geom_line(size = 1.5)
if(length(by)>0){
gg <- gg + facet_grid(as.formula(paste0("~",by)))
}
gg <- gg + theme(legend.position="bottom", text = element_text(size=txt.size),
axis.text.x = element_text(angle = 90, hjust = 1))
gg <- gg + xlab("sample size in each group") + ylab("coverage") + labs(colour="")
gg <- gg + scale_colour_colorblind()
if(plot){
print(gg)
}
return(invisible(list(plot = gg,
data = dtS.gg)))
}
## * createTable
createTable <- function(data, by, type.data = "raw", file = NULL, expected = NULL,
print = TRUE, digits = NULL,
label = "", caption = "", trace = TRUE){
require(xtable)
type.data <- match.arg(type.data, c("raw","processed"))
if(type.data == "raw"){
## ** select data
Ufile <- unique(data$iFile)
if(is.null(file)){
file <- 1:length(Ufile)
}else if(any(file %in% 1:length(Ufile) == FALSE)){
stop("unknown file selected \n")
}
keep.col <- c("n", "method", "Hprojection", by, "estimate", "se", "lower.ci", "upper.ci", "timeEstimate", "timeAll")
dt.table <- data[iFile %in% Ufile[file], .SD, .SDcols = keep.col]
if(is.null(expected)){
if(is.numeric(dt.table$n)){
max.n <- max(dt.table$n)
}else if(is.factor(dt.table$n)){
max.n <- tail(levels(droplevels(dt.table$n)),1)
}
dt.table[, expected := mean(.SD[n==max.n & method == "GS",estimate]), by = by]
}else{
Uby <- unique(dt.table[[by]])
n.by <- length(Uby)
for(iBy in 1:n.by)
dt.table[dt.table[[by]] == Uby[iBy], expected := expected[iTh]]
}
## ** summarize
dt.table[, coverage := (lower.ci < expected)*(upper.ci>expected)]
byVar <- c("n"[length(unique(dt.table$n))>1],
"Hprojection"[length(unique(dt.table$Hprojection))>1],
by[length(unique(dt.table[[by]]))>1],
"method"[length(unique(dt.table$method))>1]
)
if("Hprojection" %in% byVar){stop("Cannot handle several Hprojections")}
dtS.table <- dt.table[, .(rep = .N,
seNA = sum(is.na(se)),
bias = mean(estimate - expected),
empirical= sd(estimate),
estimated= mean(se, na.rm = TRUE),
coverage = mean(coverage, na.rm = TRUE)),
by = byVar]
}else if(type.data == "processed"){
dtS.table <- data.table::copy(data)
}
n.method <- length(unique(dtS.table$method))
## ** create table
dtSS.table <- copy(dtS.table)
if(trace){cat("Number of repetitions: ",paste(unique(dtSS.table$rep),collapse = " "),"\n")}
dtSS.table[, rep := NULL]
setkeyv(dtSS.table,c("n",by))
dtSS.table[, seNA := NULL]
dtSS.table[[by]] <- as.character(dtSS.table[[by]])
dtSS.table[duplicated(interaction(dtSS.table$n,dtSS.table[[by]])), c(by) := ""]
dtSS.table$n <- paste0("\\(n=m=\\) ",dtSS.table$n)
dtSS.table$n[duplicated(dtSS.table$n)] <- ""
setnames(dtSS.table, old = "n", new = "sample size")
if(by=="threshold"){
setnames(dtSS.table, old = "threshold", new = "\\(\\tau\\)")
}
dtSS.table[, method := factor(method, levels = c("GS","Gehan","Peron"),
labels = c("Full data","Gehan", "Peron"))]
setnames(dtSS.table, old = "method", new = "scoring rule")
dtSS.table[, bias := gsub("<","\\(<\\)",format.pval(bias, digits = digits, eps = 10^(-digits)), fixed = TRUE)]
dtSS.table[, empirical := format.pval(empirical, digits = digits, eps = 10^(-digits))]
dtSS.table[, estimated := format.pval(estimated, digits = digits, eps = 10^(-digits))]
dtSS.table[, coverage := format.pval(coverage, digits = digits, eps = 10^(-digits))]
setnames(dtSS.table, old = "empirical", new = "empirical \\(\\sigma_{\\hat{\\Delta},\\hat{\\Delta}}\\)")
setnames(dtSS.table, old = "estimated", new = "estimated \\(\\sigma_{\\hat{\\Delta},\\hat{\\Delta}}\\)")
out <- xtable(dtSS.table, type = "latex",
label = label,
caption = caption)
addtorow <- list()
addtorow$pos <- as.list(seq(from = n.method, to = NROW(dtSS.table), by=n.method))
addtorow$command <- rep(c(rep("[2mm]", times = n.method-1),"[4mm]"),
times = length(addtorow$pos)/n.method)
addtorow$pos <- addtorow$pos[-length(addtorow$pos)]
addtorow$command <- addtorow$command[-length(addtorow$command)]
mytable <- capture.output(print(out, add.to.row = addtorow, include.rownames=FALSE, include.colnames = TRUE,
sanitize.colnames.function = identity,
sanitize.text.function = identity,
table.placement = "!h"))
if(print){print(mytable) }
## ** export
return(invisible(list(table = mytable,
data = dtS.table)))
}
######################################################################
### FCT-gg.R ends here