|
87 | 87 | #' @importFrom ggpubr ggarrange |
88 | 88 | #' @importFrom stats pchisq time as.formula |
89 | 89 | #' @importFrom patchwork inset_element |
90 | | -#' @importFrom survival survfit survdiff coxph Surv cluster frailty |
91 | | -#' @importFrom cmprsk cuminc crr |
| 90 | +#' @importFrom survival survfit survdiff coxph Surv cluster frailty finegray |
| 91 | +#' @importFrom cmprsk cuminc |
92 | 92 | #' @importFrom ggsci scale_color_npg scale_fill_npg scale_color_aaas scale_fill_aaas scale_color_nejm scale_fill_nejm scale_color_lancet scale_fill_lancet scale_color_jama scale_fill_jama scale_color_jco scale_fill_jco scale_color_frontiers scale_fill_frontiers |
93 | 93 | #' @export |
94 | 94 |
|
@@ -818,16 +818,25 @@ jskm <- function(sfit, |
818 | 818 | # w/o Landmark |
819 | 819 | if (is.null(cut.landmark)) { |
820 | 820 |
|
821 | | - # 1) competing risk: Fine-Gray |
| 821 | + # 1) competing risk: Fine-Gray using survival::finegray |
822 | 822 | if (!is.null(status.cmprsk)) { |
823 | | - fg_model <- cmprsk::crr(ftime = data[[time_var]], |
824 | | - fstatus = data[[event_var]], |
825 | | - cov1 = as.matrix(data[[group_var]])) |
826 | | - HR_value <- exp(fg_model$coef[1]) |
827 | | - HR_ci_lower <- exp(fg_model$coef[1] - 1.96 * sqrt(fg_model$var[1,1])) |
828 | | - HR_ci_upper <- exp(fg_model$coef[1] + 1.96 * sqrt(fg_model$var[1,1])) |
| 823 | + # Get unique ID if not exists |
| 824 | + if (!"id" %in% names(data)) { |
| 825 | + data$id <- 1:nrow(data) |
| 826 | + } |
| 827 | + |
| 828 | + # Create finegray dataset |
| 829 | + fg_data <- survival::finegray(as.formula(form), data = data, etype = status.cmprsk) |
| 830 | + |
| 831 | + # Fit Cox model with id |
| 832 | + cox_form <- as.formula(paste("Surv(fgstart, fgstop, fgstatus) ~", group_var)) |
| 833 | + fg_model <- survival::coxph(cox_form, data = fg_data, id = id, weights = fgwt) |
| 834 | + |
| 835 | + HR_value <- summary(fg_model)$coefficients[,"exp(coef)"][1] |
| 836 | + HR_ci_lower <- summary(fg_model)$conf.int[1, "lower .95"] |
| 837 | + HR_ci_upper <- summary(fg_model)$conf.int[1, "upper .95"] |
829 | 838 | test_type <- "Fine-Gray Model" |
830 | | - pval <- 2 * (1 - pnorm(abs(fg_model$coef[1] / sqrt(fg_model$var[1,1])))) |
| 839 | + pval <- summary(fg_model)$coefficients[, "Pr(>|z|)"][1] |
831 | 840 |
|
832 | 841 |
|
833 | 842 | # 2) weights: Weighted cox |
@@ -875,7 +884,7 @@ jskm <- function(sfit, |
875 | 884 | } |
876 | 885 | # HR text |
877 | 886 | hr_txt <- ifelse(HR_value < 0.001, "HR < 0.001", paste("HR =", round(HR_value, 2))) |
878 | | - hr_txt <- paste0(hr_txt, " (95% CI: ", round(HR_ci_lower, 2), " ", round(HR_ci_upper, 2), "; P = ", round(pval, 3), ")") |
| 887 | + hr_txt <- paste0(hr_txt, " (95% CI: ", round(HR_ci_lower, 2), " ", round(HR_ci_upper, 2), "; P = ", ifelse(pval < 0.001, "<0.001", round(pval, 3)), ")") |
879 | 888 |
|
880 | 889 | if ((hr.testname == T) & !is.null(test_type)) { |
881 | 890 | hr_txt <- paste0(hr_txt, " (", test_type, ")") |
|
0 commit comments