322 |
331 |
|
#####################a |
323 |
332 |
|
|
324 |
333 |
|
if(length(levels(summary(sfit)$strata)) == 0) pval <- F |
325 |
|
- |
if(!is.null(cut.landmark)) pval <- F |
|
334 |
+ |
#if(!is.null(cut.landmark)) pval <- F |
326 |
335 |
|
|
327 |
336 |
|
if(pval == TRUE) { |
328 |
337 |
|
if (is.null(data)){ |
329 |
|
- |
data = eval(sfit$call$data) |
330 |
|
- |
} |
331 |
|
- |
|
332 |
|
- |
sdiff <- survival::survdiff(as.formula(sfit$call$formula), data = data) |
333 |
|
- |
pvalue <- pchisq(sdiff$chisq,length(sdiff$n) - 1,lower.tail = FALSE) |
334 |
|
- |
|
335 |
|
- |
## cluster option |
336 |
|
- |
if (cluster.option == "cluster" & !is.null(cluster.var)){ |
337 |
|
- |
form.old <- as.character(sfit$call$formula) |
338 |
|
- |
form.new <- paste(form.old[2], form.old[1], " + ", form.old[3], " + cluster(", cluster.var, ")", sep="") |
339 |
|
- |
sdiff <- survival::coxph(as.formula(form.new), data = data, model = T, robust = T) |
340 |
|
- |
pvalue <- summary(sdiff)$robscore["pvalue"] |
341 |
|
- |
} else if (cluster.option == "frailty" & !is.null(cluster.var)){ |
342 |
|
- |
form.old <- as.character(sfit$call$formula) |
343 |
|
- |
form.new <- paste(form.old[2], form.old[1], " + ", form.old[3], " + frailty(", cluster.var, ")", sep="") |
344 |
|
- |
sdiff <- survival::coxph(as.formula(form.new), data =data, model = T) |
345 |
|
- |
pvalue <- summary(sdiff)$logtest["pvalue"] |
|
338 |
+ |
data <- tryCatch(eval(sfit$call$data), error = function(e) e) |
|
339 |
+ |
if ("error" %in% class(data)){ |
|
340 |
+ |
stop("'pval' option requires data object. please input 'data' option") |
|
341 |
+ |
} |
346 |
342 |
|
} |
347 |
343 |
|
|
348 |
|
- |
pvaltxt <- ifelse(pvalue < 0.001,"p < 0.001",paste("p =", round(pvalue, 3))) |
349 |
|
- |
|
350 |
|
- |
if (pval.testname) pvaltxt <- paste0(pvaltxt, " (Log-rank)") |
351 |
|
- |
|
352 |
|
- |
# MOVE P-VALUE LEGEND HERE BELOW [set x and y] |
353 |
|
- |
if (is.null(pval.coord)){ |
354 |
|
- |
p <- p + annotate("text",x = (as.integer(max(sfit$time)/5)), y = 0.1 + ylims[1],label = pvaltxt, size = pval.size) |
355 |
|
- |
} else{ |
356 |
|
- |
p <- p + annotate("text",x = pval.coord[1], y = pval.coord[2], label = pvaltxt, size = pval.size) |
|
344 |
+ |
if (is.null(cut.landmark)){ |
|
345 |
+ |
sdiff <- survival::survdiff(as.formula(form), data = data) |
|
346 |
+ |
pvalue <- pchisq(sdiff$chisq,length(sdiff$n) - 1,lower.tail = FALSE) |
|
347 |
+ |
|
|
348 |
+ |
## cluster option |
|
349 |
+ |
if (cluster.option == "cluster" & !is.null(cluster.var)){ |
|
350 |
+ |
form.old <- as.character(form) |
|
351 |
+ |
form.new <- paste(form.old[2], form.old[1], " + ", form.old[3], " + cluster(", cluster.var, ")", sep="") |
|
352 |
+ |
sdiff <- survival::coxph(as.formula(form.new), data = data, model = T, robust = T) |
|
353 |
+ |
pvalue <- summary(sdiff)$robscore["pvalue"] |
|
354 |
+ |
} else if (cluster.option == "frailty" & !is.null(cluster.var)){ |
|
355 |
+ |
form.old <- as.character(form) |
|
356 |
+ |
form.new <- paste(form.old[2], form.old[1], " + ", form.old[3], " + frailty(", cluster.var, ")", sep="") |
|
357 |
+ |
sdiff <- survival::coxph(as.formula(form.new), data =data, model = T) |
|
358 |
+ |
pvalue <- summary(sdiff)$logtest["pvalue"] |
|
359 |
+ |
} |
|
360 |
+ |
|
|
361 |
+ |
pvaltxt <- ifelse(pvalue < 0.001, "p < 0.001", paste("p =", round(pvalue, 3))) |
|
362 |
+ |
if (pval.testname) pvaltxt <- paste0(pvaltxt, " (Log-rank)") |
|
363 |
+ |
|
|
364 |
+ |
# MOVE P-VALUE LEGEND HERE BELOW [set x and y] |
|
365 |
+ |
if (is.null(pval.coord)){ |
|
366 |
+ |
p <- p + annotate("text",x = (as.integer(max(sfit$time)/5)), y = 0.1 + ylims[1],label = pvaltxt, size = pval.size) |
|
367 |
+ |
} else{ |
|
368 |
+ |
p <- p + annotate("text",x = pval.coord[1], y = pval.coord[2], label = pvaltxt, size = pval.size) |
|
369 |
+ |
} |
|
370 |
+ |
} else { |
|
371 |
+ |
sdiff1 <- survival::survdiff(as.formula(form), data1) |
|
372 |
+ |
sdiff2 <- survival::survdiff(as.formula(form), data[data[[var.time]] >= cut.landmark, ]) |
|
373 |
+ |
pvalue <- sapply(list(sdiff1, sdiff2), function(x){pchisq(x$chisq,length(x$n) - 1,lower.tail = FALSE)}) |
|
374 |
+ |
|
|
375 |
+ |
## cluster option |
|
376 |
+ |
if (cluster.option == "cluster" & !is.null(cluster.var)){ |
|
377 |
+ |
form.old <- as.character(form) |
|
378 |
+ |
form.new <- paste(form.old[2], form.old[1], " + ", form.old[3], " + cluster(", cluster.var, ")", sep="") |
|
379 |
+ |
sdiff1 <- survival::coxph(as.formula(form.new), data = data1, model = T, robust = T) |
|
380 |
+ |
sdiff2 <- survival::coxph(as.formula(form.new), data = data[data[[var.time]] >= cut.landmark, ], model = T, robust = T) |
|
381 |
+ |
pvalue <- sapply(list(sdiff1, sdiff2), function(x){summary(x)$robscore["pvalue"]}) |
|
382 |
+ |
} else if (cluster.option == "frailty" & !is.null(cluster.var)){ |
|
383 |
+ |
form.old <- as.character(form) |
|
384 |
+ |
form.new <- paste(form.old[2], form.old[1], " + ", form.old[3], " + frailty(", cluster.var, ")", sep="") |
|
385 |
+ |
sdiff1 <- survival::coxph(as.formula(form.new), data = data1, model = T) |
|
386 |
+ |
sdiff2 <- survival::coxph(as.formula(form.new), data = data[data[[var.time]] >= cut.landmark, ], model = T) |
|
387 |
+ |
pvalue <- sapply(list(sdiff1, sdiff2), function(x){summary(x)$logtest["pvalue"]}) |
|
388 |
+ |
} |
|
389 |
+ |
|
|
390 |
+ |
pvaltxt <- ifelse(pvalue < 0.001, "p < 0.001", paste("p =", round(pvalue, 3))) |
|
391 |
+ |
|
|
392 |
+ |
if (pval.testname) pvaltxt <- paste0(pvaltxt, " (Log-rank)") |
|
393 |
+ |
|
|
394 |
+ |
if (is.null(pval.coord)){ |
|
395 |
+ |
p <- p + annotate("text",x = c(as.integer(max(sfit$time)/10), as.integer(max(sfit$time)/10) + cut.landmark), y = 0.1 + ylims[1],label = pvaltxt, size = pval.size) |
|
396 |
+ |
} else{ |
|
397 |
+ |
p <- p + annotate("text",x = c(pval.coord[1], pval.coord[1] + cut.landmark), y = pval.coord[2], label = pvaltxt, size = pval.size) |
|
398 |
+ |
} |
357 |
399 |
|
} |
358 |
|
- |
|
|
400 |
+ |
|
359 |
401 |
|
} |
360 |
402 |
|
|
361 |
403 |
|
################################################### |