Debian Codegraph @

R/survival-cch.R

raw
1 #' @templateVar class cch
2 #' @template title_desc_tidy
3 #'
4 #' @param x An `cch` object returned from [survival::cch()].
5 #' @param conf.level confidence level for CI
6 #' @template param_unused_dots
7 #'
8 #' @evalRd return_tidy(regression = TRUE)
9 #'
10 #' @examplesIf rlang::is_installed(c("survival", "ggplot2"))
11 #'
12 #' # load libraries for models and data
13 #' library(survival)
14 #'
15 #' # examples come from cch documentation
16 #' subcoh <- nwtco$in.subcohort
17 #' selccoh <- with(nwtco, rel == 1 | subcoh == 1)
18 #' ccoh.data <- nwtco[selccoh, ]
19 #' ccoh.data$subcohort <- subcoh[selccoh]
20 #'
21 #' # central-lab histology
22 #' ccoh.data$histol <- factor(ccoh.data$histol, labels = c("FH", "UH"))
23 #'
24 #' # tumour stage
25 #' ccoh.data$stage <- factor(ccoh.data$stage, labels = c("I", "II", "III", "IV"))
26 #' ccoh.data$age <- ccoh.data$age / 12 # age in years
27 #'
28 #' # fit model
29 #' fit.ccP <- cch(Surv(edrel, rel) ~ stage + histol + age,
30 #' data = ccoh.data,
31 #' subcoh = ~subcohort, id = ~seqno, cohort.size = 4028
32 #' )
33 #'
34 #' # summarize model fit with tidiers + visualization
35 #' tidy(fit.ccP)
36 #'
37 #' # coefficient plot
38 #' library(ggplot2)
39 #'
40 #' ggplot(tidy(fit.ccP), aes(x = estimate, y = term)) +
41 #' geom_point() +
42 #' geom_errorbarh(aes(xmin = conf.low, xmax = conf.high), height = 0) +
43 #' geom_vline(xintercept = 0)
44 #'
45 #' @aliases cch_tidiers
46 #' @export
47 #' @seealso [tidy()], [survival::cch()]
48 #' @family cch tidiers
49 #' @family survival tidiers
50 tidy.cch <- function(x, conf.level = .95, ...) {
51 check_ellipses("exponentiate", "tidy", "cch", ...)
52
53 s <- summary(x)
54 co <- stats::coefficients(s)
55
56 ret <- as_tidy_tibble(
57 co,
58 new_names = c("estimate", "std.error", "statistic", "p.value")
59 )
60
61 # add confidence interval
62 ci <- unrowname(stats::confint(x, level = conf.level))
63 colnames(ci) <- c("conf.low", "conf.high")
64 as_tibble(cbind(ret, ci))
65 }
66
67
68 #' @templateVar class cch
69 #' @template title_desc_glance
70 #'
71 #' @inherit tidy.cch params examples
72 #'
73 #' @evalRd return_glance(
74 #' "score",
75 #' "rscore",
76 #' "p.value",
77 #' "iter",
78 #' n = "number of predictions",
79 #' nevent = "number of events"
80 #' )
81 #'
82 #' @export
83 #' @seealso [glance()], [survival::cch()]
84 #' @family cch tidiers
85 #' @family survival tidiers
86 glance.cch <- function(x, ...) {
87 ret <- purrr::compact(unclass(x)[c(
88 "score",
89 "rscore",
90 "wald.test",
91 "iter",
92 "n",
93 "nevent"
94 )])
95 ret <- as_tibble(ret)
96 rename(ret, p.value = wald.test)
97 }